cataphract Thu, 25 Nov 2010 16:44:20 +0000 Revision: http://svn.php.net/viewvc?view=revision&revision=305754
Log: - Fixed bug #53403 (use of unitialized values). Fixes the fix for bug #46587. - Added test for bug #46587. Bugs: http://bugs.php.net/53403 (Assigned) mt_rand() without min/max uses invalid min/max values http://bugs.php.net/46587 (Closed) mt_/rand produce out of range numbers when min = 0 and max > get_randmax Changed paths: U php/php-src/branches/PHP_5_3/ext/standard/rand.c A php/php-src/branches/PHP_5_3/ext/standard/tests/general_functions/bug46587.phpt U php/php-src/trunk/ext/standard/rand.c A php/php-src/trunk/ext/standard/tests/general_functions/bug46587.phpt Modified: php/php-src/branches/PHP_5_3/ext/standard/rand.c =================================================================== --- php/php-src/branches/PHP_5_3/ext/standard/rand.c 2010-11-25 16:36:13 UTC (rev 305753) +++ php/php-src/branches/PHP_5_3/ext/standard/rand.c 2010-11-25 16:44:20 UTC (rev 305754) @@ -315,18 +315,19 @@ long number; int argc = ZEND_NUM_ARGS(); - if (argc != 0 && zend_parse_parameters(argc TSRMLS_CC, "ll", &min, &max) == FAILURE) - return; + if (argc != 0) { + if (zend_parse_parameters(argc TSRMLS_CC, "ll", &min, &max) == FAILURE) { + return; + } else if (max < min) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "max(%ld) is smaller than min(%ld)", max, min); + RETURN_FALSE; + } + } if (!BG(mt_rand_is_seeded)) { php_mt_srand(GENERATE_SEED() TSRMLS_CC); } - if (max < min) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "max(%ld) is smaller than min(%ld)", max, min); - RETURN_FALSE; - } - /* * Melo: hmms.. randomMT() returns 32 random bits... * Yet, the previous php_rand only returns 31 at most. Added: php/php-src/branches/PHP_5_3/ext/standard/tests/general_functions/bug46587.phpt =================================================================== --- php/php-src/branches/PHP_5_3/ext/standard/tests/general_functions/bug46587.phpt (rev 0) +++ php/php-src/branches/PHP_5_3/ext/standard/tests/general_functions/bug46587.phpt 2010-11-25 16:44:20 UTC (rev 305754) @@ -0,0 +1,16 @@ +--TEST-- +Bug #46587 (mt_rand() does not check that max is greater than min). +--FILE-- +<?php + +var_dump(mt_rand(3,8)); +var_dump(mt_rand(8,3)); + +echo "Done.\n"; +?> +--EXPECTF-- +int(%d) + +Warning: mt_rand(): max(3) is smaller than min(8) in %s on line %d +bool(false) +Done. Modified: php/php-src/trunk/ext/standard/rand.c =================================================================== --- php/php-src/trunk/ext/standard/rand.c 2010-11-25 16:36:13 UTC (rev 305753) +++ php/php-src/trunk/ext/standard/rand.c 2010-11-25 16:44:20 UTC (rev 305754) @@ -315,18 +315,19 @@ long number; int argc = ZEND_NUM_ARGS(); - if (argc != 0 && zend_parse_parameters(argc TSRMLS_CC, "ll", &min, &max) == FAILURE) - return; + if (argc != 0) { + if (zend_parse_parameters(argc TSRMLS_CC, "ll", &min, &max) == FAILURE) { + return; + } else if (max < min) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "max(%ld) is smaller than min(%ld)", max, min); + RETURN_FALSE; + } + } if (!BG(mt_rand_is_seeded)) { php_mt_srand(GENERATE_SEED() TSRMLS_CC); } - if (max < min) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "max(%ld) is smaller than min(%ld)", max, min); - RETURN_FALSE; - } - /* * Melo: hmms.. randomMT() returns 32 random bits... * Yet, the previous php_rand only returns 31 at most. Added: php/php-src/trunk/ext/standard/tests/general_functions/bug46587.phpt =================================================================== --- php/php-src/trunk/ext/standard/tests/general_functions/bug46587.phpt (rev 0) +++ php/php-src/trunk/ext/standard/tests/general_functions/bug46587.phpt 2010-11-25 16:44:20 UTC (rev 305754) @@ -0,0 +1,16 @@ +--TEST-- +Bug #46587 (mt_rand() does not check that max is greater than min). +--FILE-- +<?php + +var_dump(mt_rand(3,8)); +var_dump(mt_rand(8,3)); + +echo "Done.\n"; +?> +--EXPECTF-- +int(%d) + +Warning: mt_rand(): max(3) is smaller than min(8) in %s on line %d +bool(false) +Done.
-- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php