Skip to content
Merged
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
random: Simplify control flow for handling /dev/urandom errors
The only way the previous `if (read_bytes < size)` branch could be taken is
when the loop was exited by the `break;` statement. We can just merge this into
the loop to make the code more obvious.
  • Loading branch information
TimWolla committed Jan 20, 2023
commit 19eafef150caaba4c55c73b72aac975cd8b874f2
19 changes: 8 additions & 11 deletions ext/random/random.c
Original file line number Diff line number Diff line change
Expand Up @@ -596,20 +596,17 @@ PHPAPI int php_random_bytes(void *bytes, size_t size, bool should_throw)
for (read_bytes = 0; read_bytes < size; read_bytes += (size_t) n) {
errno = 0;
n = read(fd, bytes + read_bytes, size - read_bytes);
if (n <= 0) {
break;
}
}

if (read_bytes < size) {
if (should_throw) {
if (errno != 0) {
zend_throw_exception_ex(random_ce_Random_RandomException, 0, "Could not gather sufficient random data: %s", strerror(errno));
} else {
zend_throw_exception_ex(random_ce_Random_RandomException, 0, "Could not gather sufficient random data");
if (n <= 0) {
if (should_throw) {
if (errno != 0) {
zend_throw_exception_ex(random_ce_Random_RandomException, 0, "Could not gather sufficient random data: %s", strerror(errno));
} else {
zend_throw_exception_ex(random_ce_Random_RandomException, 0, "Could not gather sufficient random data");
}
}
return FAILURE;
}
return FAILURE;
}
}
#endif
Expand Down