Skip to content

Handle zero seed in Xoshiro256** #9250

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ PHP NEWS
PcgOneseq128XslRr64::__construct()). (timwolla)
. Fixed bug GH-9190, GH-9191 (undefined behavior for MT_RAND_PHP when
handling large ranges). (timwolla)
. Fixed bug GH-9249 (Xoshiro256StarStar does not reject the invalid
all-zero state). (timwolla)
. Removed redundant RuntimeExceptions from Randomizer methods. The
exceptions thrown by the engines will be exposed directly. (timwolla)
. Added extension specific Exceptions/Errors (RandomException, RandomError,
Expand Down
16 changes: 12 additions & 4 deletions ext/random/engine_xoshiro256starstar.c
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,12 @@ PHP_METHOD(Random_Engine_Xoshiro256StarStar, __construct)
ZEND_PARSE_PARAMETERS_END();

if (seed_is_null) {
if (php_random_bytes_throw(&state->state, 32) == FAILURE) {
zend_throw_exception(random_ce_Random_RandomException, "Failed to generate a random seed", 0);
RETURN_THROWS();
}
do {
if (php_random_bytes_throw(&state->state, 32) == FAILURE) {
zend_throw_exception(random_ce_Random_RandomException, "Failed to generate a random seed", 0);
RETURN_THROWS();
}
} while (UNEXPECTED(state->state[0] == 0 && state->state[1] == 0 && state->state[2] == 0 && state->state[3] == 0));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not too happy about hard-coding the number of elements here, but can't come up with a more elegant solution, so I guess it's okay.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it also looked pretty verbose to me. But on the other hand it's not like the number of elements is ever going to change. Any kind of loop would make the code more complex for an extraordinarily uncommon case.

I thought about ORing all the elements together, but that would be less clear (interestingly gcc already optimizes the condition to (state->state[0] | state->state[1] | state->state[2] | state->state[3]) == 0).

} else {
if (str_seed) {
/* char (byte: 8 bit) * 32 = 256 bits */
Expand All @@ -222,6 +224,12 @@ PHP_METHOD(Random_Engine_Xoshiro256StarStar, __construct)
t[i] += ((uint64_t) (unsigned char) ZSTR_VAL(str_seed)[(i * 8) + j]) << (j * 8);
}
}

if (UNEXPECTED(t[0] == 0 && t[1] == 0 && t[2] == 0 && t[3] == 0)) {
zend_argument_value_error(1, "must not consist entirely of NUL bytes");
RETURN_THROWS();
}

seed256(engine->status, t[0], t[1], t[2], t[3]);
} else {
zend_argument_value_error(1, "must be a 32 byte (256 bit) string");
Expand Down
7 changes: 7 additions & 0 deletions ext/random/tests/02_engine/xoshiro256starstar_seed.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ try {
echo $e->getMessage() . PHP_EOL;
}

try {
$engine = new Random\Engine\Xoshiro256StarStar(\str_repeat("\x00", 32));
} catch (\Throwable $e) {
echo $e->getMessage() . PHP_EOL;
}

$engine = new \Random\Engine\Xoshiro256StarStar("\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08");

\var_dump($engine);
Expand All @@ -31,6 +37,7 @@ for ($i = 0; $i < 1000; $i++) {
--EXPECTF--
Random\Engine\Xoshiro256StarStar::__construct(): Argument #1 ($seed) must be of type string|int|null, float given
Random\Engine\Xoshiro256StarStar::__construct(): Argument #1 ($seed) must be a 32 byte (256 bit) string
Random\Engine\Xoshiro256StarStar::__construct(): Argument #1 ($seed) must not consist entirely of NUL bytes
object(Random\Engine\Xoshiro256StarStar)#%d (%d) {
["__states"]=>
array(4) {
Expand Down