mirror of
https://github.com/php/php-src.git
synced 2025-08-15 21:48:51 +02:00
random: split Randomizer::getInt() without argument to Randomizer::nextInt()
Since argument overloading is not safe for reflection, the method needed to be split appropriately. Co-authored-by: Tim Düsterhus <timwolla@googlemail.com> Closes GH-9057.
This commit is contained in:
parent
59d257d1ae
commit
4e92c74654
7 changed files with 55 additions and 34 deletions
|
@ -91,26 +91,34 @@ PHP_METHOD(Random_Randomizer, __construct)
|
|||
}
|
||||
/* }}} */
|
||||
|
||||
/* {{{ Generate positive random number */
|
||||
PHP_METHOD(Random_Randomizer, nextInt)
|
||||
{
|
||||
php_random_randomizer *randomizer = Z_RANDOM_RANDOMIZER_P(ZEND_THIS);
|
||||
uint64_t result;
|
||||
|
||||
ZEND_PARSE_PARAMETERS_NONE();
|
||||
|
||||
result = randomizer->algo->generate(randomizer->status);
|
||||
if (randomizer->status->last_generated_size > sizeof(zend_long)) {
|
||||
zend_throw_exception(spl_ce_RuntimeException, "Generated value exceeds size of int", 0);
|
||||
RETURN_THROWS();
|
||||
}
|
||||
if (EG(exception)) {
|
||||
zend_throw_exception(spl_ce_RuntimeException, "Random number generation failed", 0);
|
||||
RETURN_THROWS();
|
||||
}
|
||||
|
||||
RETURN_LONG((zend_long) (result >> 1));
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
/* {{{ Generate random number in range */
|
||||
PHP_METHOD(Random_Randomizer, getInt)
|
||||
{
|
||||
php_random_randomizer *randomizer = Z_RANDOM_RANDOMIZER_P(ZEND_THIS);
|
||||
uint64_t result;
|
||||
zend_long min, max;
|
||||
int argc = ZEND_NUM_ARGS();
|
||||
|
||||
if (argc == 0) {
|
||||
result = randomizer->algo->generate(randomizer->status);
|
||||
if (randomizer->status->last_generated_size > sizeof(zend_long)) {
|
||||
zend_throw_exception(spl_ce_RuntimeException, "Generated value exceeds size of int", 0);
|
||||
RETURN_THROWS();
|
||||
}
|
||||
if (EG(exception)) {
|
||||
zend_throw_exception(spl_ce_RuntimeException, "Random number generation failed", 0);
|
||||
RETURN_THROWS();
|
||||
}
|
||||
RETURN_LONG((zend_long) (result >> 1));
|
||||
}
|
||||
|
||||
ZEND_PARSE_PARAMETERS_START(2, 2)
|
||||
Z_PARAM_LONG(min)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue