[PATCH 04/12] Remove static from rand_single_bit()

[email protected] Fri, 6 Mar 2015 14:30:39 -0800
Newsgroups org.kernel.vger.trinity
Message-ID <[email protected]>
From: Tyson Smith <[email protected]>

Added a check to verify input value is between 0 and 32 or 64 bits
depending on __WORDSIZE.
---
 include/random.h | 1 +
 random.c         | 8 +++++++-
 2 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/include/random.h b/include/random.h
index f070568..69ccfa3 100644
--- a/include/random.h
+++ b/include/random.h
@@ -24,4 +24,5 @@ unsigned int rand_bool(void);
 unsigned int rand32(void);
 u64 rand64(void);
 unsigned int rand_range(unsigned int min, unsigned int max);
+unsigned long rand_single_bit(unsigned char size);
 unsigned long set_rand_bitmask(unsigned int num, const unsigned long *values);
diff --git a/random.c b/random.c
index adee25e..063912f 100644
--- a/random.c
+++ b/random.c
@@ -69,8 +69,14 @@ unsigned int rand_bool(void)
 	return rand() % 2;
 }
 
-static unsigned int rand_single_bit(unsigned char size)
+/*
+ * Pick a random power of two between 2^0 and 2^(__WORDSIZE-1)
+ */
+unsigned long rand_single_bit(unsigned char size)
 {
+	if (size > __WORDSIZE)
+		size = __WORDSIZE;
+
 	return (1UL << (rand() % size));
 }
 
-- 
1.9.1