From 19eafef150caaba4c55c73b72aac975cd8b874f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20D=C3=BCsterhus?= Date: Sat, 21 Jan 2023 00:59:03 +0100 Subject: [PATCH] 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. --- ext/random/random.c | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/ext/random/random.c b/ext/random/random.c index 30ae6c3baa7bc..243f0fb8bc268 100644 --- a/ext/random/random.c +++ b/ext/random/random.c @@ -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