summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorache <ache@FreeBSD.org>2008-07-22 12:43:09 +0000
committerache <ache@FreeBSD.org>2008-07-22 12:43:09 +0000
commit1cdd160d5093e9601fbfb3d2d7f65fd3b87df1a7 (patch)
tree5ffd58b79b3f9d9258ee560622881668e81198dc
parente1bf8eba273f6a33d0c6eed3154db0a8c0cad3b2 (diff)
downloadFreeBSD-src-1cdd160d5093e9601fbfb3d2d7f65fd3b87df1a7.zip
FreeBSD-src-1cdd160d5093e9601fbfb3d2d7f65fd3b87df1a7.tar.gz
In arc4random_uniform() detect simple "power of two" case and
return just (arc4random() % upper_bound)
-rw-r--r--lib/libc/gen/arc4random.c6
1 files changed, 5 insertions, 1 deletions
diff --git a/lib/libc/gen/arc4random.c b/lib/libc/gen/arc4random.c
index 1c7dead..56e457b 100644
--- a/lib/libc/gen/arc4random.c
+++ b/lib/libc/gen/arc4random.c
@@ -256,7 +256,11 @@ arc4random_uniform(u_int32_t upper_bound)
u_int32_t r, min;
if (upper_bound < 2)
- return 0;
+ return (0);
+
+ /* Detect simple power of two case */
+ if ((upper_bound & -upper_bound) == upper_bound)
+ return (arc4random() % upper_bound);
#if (ULONG_MAX > 0xffffffffUL)
min = 0x100000000UL % upper_bound;
OpenPOWER on IntegriCloud