From: Tyson Smith <tysmith@xxxxxxxxxxxx> 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 -- To unsubscribe from this list: send the line "unsubscribe trinity" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html