Skip to content

Commit a7998fd

Browse files
authoredJan 23, 2023
random: Simplify control flow for handling /dev/urandom errors (#10392)
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.
1 parent 56dc2eb commit a7998fd

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed
 

‎ext/random/random.c

+8-11
Original file line numberDiff line numberDiff line change
@@ -594,20 +594,17 @@ PHPAPI int php_random_bytes(void *bytes, size_t size, bool should_throw)
594594
for (read_bytes = 0; read_bytes < size; read_bytes += (size_t) n) {
595595
errno = 0;
596596
n = read(fd, bytes + read_bytes, size - read_bytes);
597-
if (n <= 0) {
598-
break;
599-
}
600-
}
601597

602-
if (read_bytes < size) {
603-
if (should_throw) {
604-
if (errno != 0) {
605-
zend_throw_exception_ex(random_ce_Random_RandomException, 0, "Could not gather sufficient random data: %s", strerror(errno));
606-
} else {
607-
zend_throw_exception_ex(random_ce_Random_RandomException, 0, "Could not gather sufficient random data");
598+
if (n <= 0) {
599+
if (should_throw) {
600+
if (errno != 0) {
601+
zend_throw_exception_ex(random_ce_Random_RandomException, 0, "Could not gather sufficient random data: %s", strerror(errno));
602+
} else {
603+
zend_throw_exception_ex(random_ce_Random_RandomException, 0, "Could not gather sufficient random data");
604+
}
608605
}
606+
return FAILURE;
609607
}
610-
return FAILURE;
611608
}
612609
}
613610
#endif

0 commit comments

Comments
 (0)