From: Mark Wooding Date: Sat, 23 Dec 2023 14:18:18 +0000 (+0000) Subject: rand/rand.c: Rearrange some comparisons to avoid arithmetic overflow. X-Git-Url: https://git.distorted.org.uk/~mdw/catacomb/commitdiff_plain/4199ce23fd3df23daccbf961b2ff1a2d42f89519 rand/rand.c: Rearrange some comparisons to avoid arithmetic overflow. --- diff --git a/rand/rand.c b/rand/rand.c index 90e81930..0d0650eb 100644 --- a/rand/rand.c +++ b/rand/rand.c @@ -413,7 +413,7 @@ void rand_get(rand_pool *r, void *p, size_t sz) if (!sz) return; for (;;) { - if (r->o + sz <= RAND_BUFSZ) { + if (sz <= RAND_BUFSZ - r->o) { memcpy(o, r->buf + r->o, sz); r->o += sz; break; @@ -475,7 +475,7 @@ void rand_getgood(rand_pool *r, void *p, size_t sz) chunk = r->obits / 8; } - if (chunk + r->o <= RAND_BUFSZ) { + if (chunk <= RAND_BUFSZ - r->o) { memcpy(o, r->buf + r->o, chunk); r->o += chunk; } else {