[PATCH] random: make error check in random_read_iter() more precise and readable

From: bard
Date: Tue Jun 17 2025 - 09:20:42 EST


random: make error check in random_read_iter() more precise and readable

Currently, wait_for_random_bytes() only returns 0 on success or a negative error.
The check `if (ret != 0)` is functionally correct today, but fragile:
it would treat any unexpected positive value (should it ever be returned in the future)
as an error, which could cause incorrect behavior.

This patch replaces the check with `if (ret < 0)`, which makes the intent explicit:
only true error codes should trigger an early return. This improves code robustness
and makes the logic more readable to future maintainers by more clearly expressing
the expectations around `wait_for_random_bytes()`.

No functional change in current behavior.

Thanks a lot :)
Bar D.
From 681e7abc5205aa25d03eab6d6d45527308e4b477 Mon Sep 17 00:00:00 2001
From: Bar D <bardr4@xxxxxxxxx>
Date: Tue, 17 Jun 2025 15:55:46 +0300
Subject: [PATCH] random: make error check in random_read_iter() more precise
and readable

Currently, wait_for_random_bytes() only returns 0 on success or a negative error.
The check `if (ret != 0)` is functionally correct today, but fragile:
it would treat any unexpected positive value (should it ever be returned in the future)
as an error, which could cause incorrect behavior.

This patch replaces the check with `if (ret < 0)`, which makes the intent explicit:
only true error codes should trigger an early return. This improves code robustness
and makes the logic more readable to future maintainers by more clearly expressing
the expectations around `wait_for_random_bytes()`.

No functional change in current behavior.

Signed-off-by: Bar D <bardr4@xxxxxxxxx>
---
drivers/char/random.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/char/random.c b/drivers/char/random.c
index b8b24b6ed3fe..cf4e0d85032d 100644
--- a/drivers/char/random.c
+++ b/drivers/char/random.c
@@ -1489,7 +1489,7 @@ static ssize_t random_read_iter(struct kiocb *kiocb, struct iov_iter *iter)
return -EAGAIN;

ret = wait_for_random_bytes();
- if (ret != 0)
+ if (ret < 0)
return ret;
return get_random_bytes_user(iter);
}
--
2.34.1