rand: fix modulo bias in rmalloc

Signed-off-by: Leah Rowe <leah@libreboot.org>
This commit is contained in:
Leah Rowe
2026-03-26 08:56:15 +00:00
parent dbc99be9a0
commit cf16d07df9
+8 -2
View File
@@ -81,7 +81,7 @@ win_lottery(char **buf) /* are u lucky? */
char *s1 = rmalloc(&size);
char *s2 = rmalloc(&size);
if (scmp(s1, s2, BUFSIZ + 2, &rval) >= 0 &&
if (scmp(s1, s2, BUFSIZ + 1, &rval) >= 0 &&
rval == 0)
rval = 1; /* winner! */
else
@@ -98,10 +98,16 @@ win_lottery(char **buf) /* are u lucky? */
void *
rmalloc(size_t *rval)
{
/* clamp rand to prevent modulo bias */
size_t limit = SIZE_MAX - (SIZE_MAX % BUFSIZ);
if (if_err(rval == NULL, EFAULT))
return NULL;
rset(rval, sizeof(*rval));
do {
rset(rval, sizeof(*rval));
} while (*rval >= limit);
return mkrstr(*rval %= BUFSIZ);
}