sniper Tue Nov 18 23:44:07 2003 EDT
Modified files:
/php-src/ext/gmp config.m4 gmp.c php_gmp.h
Log:
- Fixed bug #26267 (gmp_random() leaks memory and does not produce random numbers)
# ..and mpz_random() is obsolete according to the GNU MP manual.
Index: php-src/ext/gmp/config.m4
diff -u php-src/ext/gmp/config.m4:1.9 php-src/ext/gmp/config.m4:1.10
--- php-src/ext/gmp/config.m4:1.9 Tue Sep 30 22:53:08 2003
+++ php-src/ext/gmp/config.m4 Tue Nov 18 23:44:06 2003
@@ -1,9 +1,9 @@
dnl
-dnl $Id: config.m4,v 1.9 2003/10/01 02:53:08 sniper Exp $
+dnl $Id: config.m4,v 1.10 2003/11/19 04:44:06 sniper Exp $
dnl
PHP_ARG_WITH(gmp, for GNU MP support,
-[ --with-gmp Include GNU MP support])
+[ --with-gmp[=DIR] Include GNU MP support])
if test "$PHP_GMP" != "no"; then
@@ -14,6 +14,18 @@
if test -z "$GMP_DIR"; then
AC_MSG_ERROR(Unable to locate gmp.h)
fi
+
+ PHP_CHECK_LIBRARY(gmp, __gmp_randinit_lc_2exp_size,
+ [],[
+ PHP_CHECK_LIBRARY(gmp, gmp_randinit_lc_2exp_size,
+ [],[
+ AC_MSG_ERROR([GNU MP Library version 4.1.2 or greater required.])
+ ],[
+ -L$GMP_DIR/lib
+ ])
+ ],[
+ -L$GMP_DIR/lib
+ ])
PHP_ADD_LIBRARY_WITH_PATH(gmp, $GMP_DIR/lib, GMP_SHARED_LIBADD)
PHP_ADD_INCLUDE($GMP_DIR/include)
Index: php-src/ext/gmp/gmp.c
diff -u php-src/ext/gmp/gmp.c:1.37 php-src/ext/gmp/gmp.c:1.38
--- php-src/ext/gmp/gmp.c:1.37 Tue Nov 18 05:28:13 2003
+++ php-src/ext/gmp/gmp.c Tue Nov 18 23:44:06 2003
@@ -28,9 +28,12 @@
#if HAVE_GMP
#include <gmp.h>
-/* If you declare any globals in php_gmp.h uncomment this:
-ZEND_DECLARE_MODULE_GLOBALS(gmp)
-*/
+
+/* Needed for gmp_random() */
+#include "ext/standard/php_rand.h"
+#include "ext/standard/php_lcg.h"
+#include <gmp-mparam.h>
+#define GMP_ABS(x) ((x) >= 0 ? (x) : -(x))
/* True global resources - no need for thread safety here */
static int le_gmp;
@@ -90,13 +93,15 @@
ZEND_MODULE_STARTUP_N(gmp),
ZEND_MODULE_SHUTDOWN_N(gmp),
NULL,
- NULL,
+ ZEND_MODULE_DEACTIVATE_N(gmp),
ZEND_MODULE_INFO_N(gmp),
NO_VERSION_YET,
STANDARD_MODULE_PROPERTIES
};
/* }}} */
+ZEND_DECLARE_MODULE_GLOBALS(gmp)
+
#ifdef COMPILE_DL_GMP
ZEND_GET_MODULE(gmp)
# ifdef PHP_WIN32
@@ -136,10 +141,20 @@
}
/* }}} */
+/* {{{ php_gmp_init_globals
+ */
+static void php_gmp_init_globals(zend_gmp_globals *gmp_globals)
+{
+ gmp_globals->rand_initialized = 0;
+}
+/* }}} */
+
/* {{{ ZEND_MINIT_FUNCTION
*/
ZEND_MODULE_STARTUP_D(gmp)
{
+ ZEND_INIT_MODULE_GLOBALS(gmp, php_gmp_init_globals, NULL);
+
le_gmp = zend_register_list_destructors_ex(_php_gmpnum_free, NULL,
GMP_RESOURCE_NAME, module_number);
REGISTER_LONG_CONSTANT("GMP_ROUND_ZERO", GMP_ROUND_ZERO, CONST_CS |
CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("GMP_ROUND_PLUSINF", GMP_ROUND_PLUSINF, CONST_CS |
CONST_PERSISTENT);
@@ -151,6 +166,19 @@
}
/* }}} */
+/* {{{ ZEND_RSHUTDOWN_FUNCTION
+ */
+ZEND_MODULE_DEACTIVATE_D(gmp)
+{
+ if (GMPG(rand_initialized)) {
+ gmp_randclear(GMPG(rand_state));
+ GMPG(rand_initialized) = 0;
+ }
+
+ return SUCCESS;
+}
+/* }}} */
+
/* {{{ ZEND_MSHUTDOWN_FUNCTION
*/
ZEND_MODULE_SHUTDOWN_D(gmp)
@@ -1042,7 +1070,17 @@
}
INIT_GMP_NUM(gmpnum_result);
- mpz_random(*gmpnum_result, limiter);
+
+ if (!GMPG(rand_initialized)) {
+ /* Initialize */
+ gmp_randinit_lc_2exp_size(GMPG(rand_state), 32L);
+
+ /* Seed */
+ gmp_randseed_ui(GMPG(rand_state), GENERATE_SEED());
+
+ GMPG(rand_initialized) = 1;
+ }
+ mpz_urandomb(*gmpnum_result, GMPG(rand_state), GMP_ABS (limiter) *
BITS_PER_MP_LIMB);
ZEND_REGISTER_RESOURCE(return_value, gmpnum_result, le_gmp);
}
Index: php-src/ext/gmp/php_gmp.h
diff -u php-src/ext/gmp/php_gmp.h:1.9 php-src/ext/gmp/php_gmp.h:1.10
--- php-src/ext/gmp/php_gmp.h:1.9 Tue Jun 10 16:03:29 2003
+++ php-src/ext/gmp/php_gmp.h Tue Nov 18 23:44:06 2003
@@ -19,11 +19,10 @@
#ifndef PHP_GMP_H
#define PHP_GMP_H
-/* You should tweak config.m4 so this symbol (or some else suitable)
- gets defined.
-*/
#if HAVE_GMP
+#include <gmp.h>
+
extern zend_module_entry gmp_module_entry;
#define phpext_gmp_ptr &gmp_module_entry
@@ -35,6 +34,7 @@
ZEND_MODULE_STARTUP_D(gmp);
ZEND_MODULE_SHUTDOWN_D(gmp);
+ZEND_MODULE_DEACTIVATE_D(gmp);
ZEND_MODULE_INFO_D(gmp);
ZEND_FUNCTION(gmp_init);
@@ -76,24 +76,13 @@
ZEND_FUNCTION(gmp_popcount);
ZEND_FUNCTION(gmp_hamdist);
-/*
- Declare any global variables you may need between the BEGIN
- and END macros here:
-
ZEND_BEGIN_MODULE_GLOBALS(gmp)
- int global_variable;
+ zend_bool rand_initialized;
+ gmp_randstate_t rand_state;
ZEND_END_MODULE_GLOBALS(gmp)
-*/
-
-/* In every function that needs to use variables in php_gmp_globals,
- do call GMPLS_FETCH(); after declaring other variables used by
- that function, and always refer to them as GMPG(variable).
- You are encouraged to rename these macros something shorter, see
- examples in any other php module directory.
-*/
#ifdef ZTS
-#define GMPG(v) TSRMG(gmp_globals_id, php_gmp_globals *, v)
+#define GMPG(v) TSRMG(gmp_globals_id, zend_gmp_globals *, v)
#else
#define GMPG(v) (gmp_globals.v)
#endif
--
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php