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

Conversation

TimWolla
Copy link
Member

@TimWolla TimWolla commented Aug 4, 2022

  • Retry if the CSPRNG generates a zero seed.
  • Throw if the user passes a zero seed.

@zeriyoshi
Copy link
Contributor

Closes: #9249

Copy link
Member

@cmb69 cmb69 left a comment

Choose a reason for hiding this comment

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

Thank you!

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).

- Retry if the CSPRNG generates a zero state.
- Throw ValueError if the user passes a zero state.

Fixes phpGH-9249
@TimWolla TimWolla force-pushed the xoshiro256starstar-nul branch from 87312a0 to 26de776 Compare August 5, 2022 11:31
@TimWolla TimWolla merged commit 1cd2d73 into php:master Aug 5, 2022
@TimWolla TimWolla deleted the xoshiro256starstar-nul branch August 5, 2022 12:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants