diff --git a/NEWS b/NEWS index 7706b79823242..06d31bcf4f2b0 100644 --- a/NEWS +++ b/NEWS @@ -14,6 +14,9 @@ PHP NEWS . array out of bounds, stack overflow handled for segfault handler on windows. (David Carlier) +- Random: + . lcg_value() is now deprecated. (timwolla) + - Standard: . Unserializing the uppercase 'S' tag is now deprecated. (timwolla) diff --git a/UPGRADING b/UPGRADING index e7a1fa340c153..d25eb35383b8b 100644 --- a/UPGRADING +++ b/UPGRADING @@ -434,6 +434,11 @@ PHP 8.4 UPGRADE NOTES 3-parameter signature with a null $row parameter instead. . Added pg_result_memory_size to get the visibility the memory used by a query result. +- Random: + . lcg_value() is deprecated, as the function is broken in multiple ways. + Use \Random\Randomizer::getFloat() instead. + RFC: https://wiki.php.net/rfc/deprecations_php_8_4 + - Reflection: . Calling ReflectionMethod::__construct() with 1 argument is deprecated. Use ReflectionMethod::createFromMethodName() instead. diff --git a/ext/random/random.c b/ext/random/random.c index 5db20eb6db4e5..102ee0ebde8a5 100644 --- a/ext/random/random.c +++ b/ext/random/random.c @@ -27,6 +27,7 @@ #include "php.h" +#include "Zend/zend_attributes.h" #include "Zend/zend_enum.h" #include "Zend/zend_exceptions.h" diff --git a/ext/random/random.stub.php b/ext/random/random.stub.php index 1b40294e60f41..fc3b178e55fff 100644 --- a/ext/random/random.stub.php +++ b/ext/random/random.stub.php @@ -15,6 +15,7 @@ */ const MT_RAND_PHP = UNKNOWN; + #[\Deprecated(since: '8.4', message: "use \\Random\\Randomizer::getFloat() instead")] function lcg_value(): float {} function mt_srand(?int $seed = null, int $mode = MT_RAND_MT19937): void {} diff --git a/ext/random/random_arginfo.h b/ext/random/random_arginfo.h index 82df476ed125a..fcb96380c6f0e 100644 --- a/ext/random/random_arginfo.h +++ b/ext/random/random_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: 35cb16abb3392bd257a43cc675cad4f5af5549c1 */ + * Stub hash: fad14b8b8abaf9f33d7837ba8f6190c5d10ff63f */ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_lcg_value, 0, 0, IS_DOUBLE, 0) ZEND_END_ARG_INFO() @@ -155,7 +155,7 @@ ZEND_METHOD(Random_Randomizer, __serialize); ZEND_METHOD(Random_Randomizer, __unserialize); static const zend_function_entry ext_functions[] = { - ZEND_FE(lcg_value, arginfo_lcg_value) + ZEND_RAW_FENTRY("lcg_value", zif_lcg_value, arginfo_lcg_value, ZEND_ACC_DEPRECATED, NULL, NULL) ZEND_FE(mt_srand, arginfo_mt_srand) ZEND_RAW_FENTRY("srand", zif_mt_srand, arginfo_srand, 0, NULL, NULL) ZEND_FE(rand, arginfo_rand) @@ -247,6 +247,19 @@ static void register_random_symbols(int module_number) { REGISTER_LONG_CONSTANT("MT_RAND_MT19937", MT_RAND_MT19937, CONST_PERSISTENT); REGISTER_LONG_CONSTANT("MT_RAND_PHP", MT_RAND_PHP, CONST_PERSISTENT | CONST_DEPRECATED); + + + zend_attribute *attribute_Deprecated_func_lcg_value_0 = zend_add_function_attribute(zend_hash_str_find_ptr(CG(function_table), "lcg_value", sizeof("lcg_value") - 1), ZSTR_KNOWN(ZEND_STR_DEPRECATED), 2); + zval attribute_Deprecated_func_lcg_value_0_arg0; + zend_string *attribute_Deprecated_func_lcg_value_0_arg0_str = zend_string_init("8.4", strlen("8.4"), 1); + ZVAL_STR(&attribute_Deprecated_func_lcg_value_0_arg0, attribute_Deprecated_func_lcg_value_0_arg0_str); + ZVAL_COPY_VALUE(&attribute_Deprecated_func_lcg_value_0->args[0].value, &attribute_Deprecated_func_lcg_value_0_arg0); + attribute_Deprecated_func_lcg_value_0->args[0].name = ZSTR_KNOWN(ZEND_STR_SINCE); + zval attribute_Deprecated_func_lcg_value_0_arg1; + zend_string *attribute_Deprecated_func_lcg_value_0_arg1_str = zend_string_init("use \\Random\\Randomizer::getFloat() instead", strlen("use \\Random\\Randomizer::getFloat() instead"), 1); + ZVAL_STR(&attribute_Deprecated_func_lcg_value_0_arg1, attribute_Deprecated_func_lcg_value_0_arg1_str); + ZVAL_COPY_VALUE(&attribute_Deprecated_func_lcg_value_0->args[1].value, &attribute_Deprecated_func_lcg_value_0_arg1); + attribute_Deprecated_func_lcg_value_0->args[1].name = ZSTR_KNOWN(ZEND_STR_MESSAGE); } static zend_class_entry *register_class_Random_Engine_Mt19937(zend_class_entry *class_entry_Random_Engine) diff --git a/ext/random/tests/01_functions/lcg_value_basic.phpt b/ext/random/tests/01_functions/lcg_value_basic.phpt index ca76b5c836044..3271efc061e29 100644 --- a/ext/random/tests/01_functions/lcg_value_basic.phpt +++ b/ext/random/tests/01_functions/lcg_value_basic.phpt @@ -8,7 +8,7 @@ echo "MATHS test script started\n"; echo "\n lcg_value tests...\n"; for ($i = 0; $i < 100; $i++) { - $res = lcg_value(); + $res = @lcg_value(); if (!is_float($res) || $res < 0 || $res > 1) { break; diff --git a/ext/random/tests/01_functions/lcg_value_deprecation.phpt b/ext/random/tests/01_functions/lcg_value_deprecation.phpt new file mode 100644 index 0000000000000..850e0201187a9 --- /dev/null +++ b/ext/random/tests/01_functions/lcg_value_deprecation.phpt @@ -0,0 +1,11 @@ +--TEST-- +lcg_value() deprecation +--FILE-- + +--EXPECTF-- +Deprecated: Function lcg_value() is deprecated since 8.4, use \Random\Randomizer::getFloat() instead in %s on line %d +float(%f)