-
Notifications
You must be signed in to change notification settings - Fork 7.9k
fix random_bytes(0) and Random\Randomizer::getBytes(0) #12216
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
base: master
Are you sure you want to change the base?
Conversation
divinity76
commented
Sep 15, 2023
•
edited
Loading
edited
Reference to the old bug tracker for prior discussion: https://bugs.php.net/bug.php?id=80214 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Randomizer::getBytes()
should be adjusted for consistency. I agree that retrieving zero random bytes is not per se wrong, but I understand the concerns in the previous bug. I'll give this some thought, this is a preliminary technical review.
This comment was marked as outdated.
This comment was marked as outdated.
added 0 support for Randomizer, please re-check the code when convenient |
for consistency, I would prefer to actually support this, but TimWolla disagrees, and i cba fighting for it (it's an edge-case after all): php#12216 (comment)
for consistency, I would prefer to actually support this, but TimWolla disagrees, and i cba fighting for it (it's an edge-case after all): php#12216 (comment)
8232c5c
to
326094a
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've now tested the branch locally and looked through the related bug report and have a question about this:
What should the behavior of
<?php
use Random\Engine;
use Random\Randomizer;
final class ThrowingEngine implements Engine
{
public function generate(): string
{
throw new Exception('Error');
}
}
$randomizer = new Randomizer(new ThrowingEngine());
var_dump($randomizer->getBytes(0));
be? And likely what should the behavior of random_bytes(0)
be if the CSPRNG fails (both getrandom() and /dev/urandom) unavailable? It would probably be a good thing if random_bytes(0)
would allow to test if the CSPRNG is functional.
On the other hand, Randomizer::shuffleBytes()
and Randomizer::shuffleArray()
do not currently make any calls to the engine for an empty string and empty array respectively.
Unfortunately the last commenter on the bug report just said that:
I can think of at least three potential issues if php_random_bytes(0) always succeeded
But didn't spell out what issues they were thinking of.
This probably does not require a full-blown RFC, but a discussion on the PHP Internals mailing list would probably be appropriate to decide on the desired semantics.
In fact they don't do for single-element strings or arrays either. |
One more question: Should zero also be legal for |
Co-authored-by: Tim Düsterhus <[email protected]>