PcgOneseq128XslRr64::jump(): Throw ValueError for negative $advance (#9213)

* PCG64: $advance must be non-negative

Closes GH-9212
This commit is contained in:
Anton Smirnov 2022-08-01 15:47:14 +03:00 committed by GitHub
parent fac37347ce
commit 50bd8ba51c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 0 deletions

View File

@ -186,6 +186,11 @@ PHP_METHOD(Random_Engine_PcgOneseq128XslRr64, jump)
Z_PARAM_LONG(advance);
ZEND_PARSE_PARAMETERS_END();
if (UNEXPECTED(advance < 0)) {
zend_argument_value_error(1, "must be greater than or equal to 0");
RETURN_THROWS();
}
php_random_pcgoneseq128xslrr64_advance(state, advance);
}
/* }}} */

View File

@ -0,0 +1,23 @@
--TEST--
Random: Engine: PcgOneseq128XslRr64: Jump with negative $advance
--FILE--
<?php
$engine = new \Random\Engine\PcgOneseq128XslRr64(1234);
$referenceEngine = new \Random\Engine\PcgOneseq128XslRr64(1234);
try {
$engine->jump(-1);
} catch (ValueError $e) {
echo $e->getMessage(), PHP_EOL;
}
if ($engine->generate() !== $referenceEngine->generate()) {
die('failure: state changed');
}
die('success');
?>
--EXPECT--
Random\Engine\PcgOneseq128XslRr64::jump(): Argument #1 ($advance) must be greater than or equal to 0
success