summaryrefslogtreecommitdiff
path: root/util/libreboot-utils/lib
diff options
context:
space:
mode:
authorLeah Rowe <leah@libreboot.org>2026-03-26 11:41:37 +0000
committerLeah Rowe <leah@libreboot.org>2026-03-26 13:09:43 +0000
commitd1ba9bae0358d87b8dc73d89b7df956722c26997 (patch)
tree4fb95f0bb88a5ff6aaee865c4cd05565a85c83a9 /util/libreboot-utils/lib
parent10ecf32e33ca6a099dd9fe206e070584eb62b629 (diff)
further cleanupHEADmaster
Signed-off-by: Leah Rowe <leah@libreboot.org>
Diffstat (limited to 'util/libreboot-utils/lib')
-rw-r--r--util/libreboot-utils/lib/rand.c60
1 files changed, 9 insertions, 51 deletions
diff --git a/util/libreboot-utils/lib/rand.c b/util/libreboot-utils/lib/rand.c
index 58cb211e..3155eec3 100644
--- a/util/libreboot-utils/lib/rand.c
+++ b/util/libreboot-utils/lib/rand.c
@@ -72,62 +72,20 @@
* or your program dies.
*/
-#define MAX_ALLOC (2 << 16)
-
-int
-win_lottery(void) /* are u lucky? */
+void *
+rmalloc(size_t *rval)
{
- size_t size = rsize();
- size_t size2 = rsize();
- char *s = NULL;
-
- if (size &&
- size == size2 &&
- size <= MAX_ALLOC << 1) {
-
- if (!memcmp(s = mkrbuf(size << 1),
- s + size, size))
- size2 = 1; /* winner! */
- else
- size2 = 0;
- } else {
- return 0;
- }
-
- free_if_null(&s);
- return (int)size2;
+ return if_err(rval == NULL, EFAULT) ?
+ NULL : mkrstr(*rval = rsize(BUFSIZ));
}
size_t
-rsize(void)
-{
- size_t rval = 0;
-
- /* clamp rand to prevent modulo bias */
- size_t limit = SIZE_MAX - (SIZE_MAX % MAX_ALLOC);
-
- do {
- rset(&rval, sizeof(rval));
- } while (rval >= limit);
-
- return rval % MAX_ALLOC;
-}
-
-
-void *
-rmalloc(size_t *rval)
+rsize(size_t n)
{
- /* clamp rand to prevent modulo bias */
- size_t limit = SIZE_MAX - (SIZE_MAX % MAX_ALLOC);
-
- if (if_err(rval == NULL, EFAULT))
- return NULL;
-
- do {
- rset(rval, sizeof(*rval));
- } while (*rval >= limit || *rval == 0);
+ size_t rval = SIZE_MAX;
+ for (; rval >= SIZE_MAX - (SIZE_MAX % n); rset(&rval, sizeof(rval)));
- return mkrstr(*rval %= MAX_ALLOC);
+ return rval % n;
}
char *
@@ -157,7 +115,7 @@ mkrstr(size_t n) /* emulates spkmodem-decode */
void *
mkrbuf(size_t n)
{
- void *buf;
+ void *buf = "";
if (n == 0)
err_no_cleanup(0, EPERM, "mkrbuf: zero-byte request");