]> git.ipfire.org Git - thirdparty/bird.git/commitdiff
Fix brandom() to return u32 312-brandom
authorMaria Matejka <mq@ucw.cz>
Tue, 12 May 2026 12:54:06 +0000 (14:54 +0200)
committerMaria Matejka <mq@ucw.cz>
Tue, 12 May 2026 12:54:06 +0000 (14:54 +0200)
I missed that random() returns only positive results (31 bits), while
jrand() returns 32 bits but half negative. Rectifying that to return
always unsigned 32 bits.

Reported-By: Ondrej Zajicek <santiago@crfreenet.org>
Issue: #312

lib/birdlib.h
sysdep/unix/random.c

index 6481c905be446843232d4ee3f4dfb72276423cd9..807efa22be9126685a027f266eae840c545dac8a 100644 (file)
@@ -215,8 +215,8 @@ asm(
 
 /* Pseudorandom numbers */
 
-long brandom(void);
-u32 random_u32(void);
+u32 brandom(void);
+#define random_u32 brandom
 void random_init(void);
 void random_init_thread(void);
 void random_bytes(void *buf, size_t size);
index d0f2d49c72fe0bb2a4e476c5350ffd959f7e58de..481b69f6576dc5b46aeced2a89b9a977bd5dd38a 100644 (file)
 
 _Thread_local unsigned short random_state[3];
 
-long
-brandom(void)
-{
-  return jrand48(random_state);
-}
-
 u32
-random_u32(void)
+brandom(void)
 {
-  long int rand_low, rand_high;
-
-  rand_low = brandom();
-  rand_high = brandom();
-  return (rand_low & 0xffff) | ((rand_high & 0xffff) << 16);
+  return (u32) jrand48(random_state);
 }