trinity.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 6/7] fix undefined behavior
@ 2015-03-16 20:26 tyson.w.smith
  0 siblings, 0 replies; only message in thread
From: tyson.w.smith @ 2015-03-16 20:26 UTC (permalink / raw)
  To: davej; +Cc: trinity, tysmith, Tyson Smith

From: Tyson Smith <tyson.w.smith@gmail.com>

---
 include/random.h      | 6 ++++--
 interesting-numbers.c | 2 +-
 2 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/include/random.h b/include/random.h
index e30dd9f..4e95996 100644
--- a/include/random.h
+++ b/include/random.h
@@ -7,8 +7,10 @@
 #define ONE_IN(x)				((rand() % x) == 0)	// limit of RAND_MAX-1
 
 #if RAND_MAX == 0x7FFFFFFF
-#define RAND_32()				((rand() << 1) | (rand() & 1))
-#define RAND_64()				(((0ULL | rand()) << 33) | ((0ULL | rand()) << 2) | (rand() & 0x3))
+#define RAND_32()				(((unsigned int)rand() << 1) | (rand() & 1))
+#define RAND_64()				(((unsigned long long)rand() << 33) | \
+								 ((unsigned long long)rand() << 2) | \
+								 (rand() & 0x3))
 #else
 #error "Unexpected RAND_MAX value. Please add support."
 #endif
diff --git a/interesting-numbers.c b/interesting-numbers.c
index 66728dd..2fecf3a 100644
--- a/interesting-numbers.c
+++ b/interesting-numbers.c
@@ -46,7 +46,7 @@ static unsigned int get_interesting_32bit_value(void)
 	switch (rand() % 10) {
 	case 0: return 0x80000000 >> (rand() & 0x1f);	// 2^n (1 -> 0x10000)
 	case 1: return rand();							// 0 -> RAND_MAX (likely 0x7fffffff)
-	case 2: return 0xff << (rand() % 25);
+	case 2: return 0xffUL << (rand() % 25);
 	case 3: return 0xffff0000;
 	case 4: return 0xffffe000;
 	case 5: return 0xffffff00 | RAND_BYTE();
-- 
1.9.1

^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2015-03-16 20:26 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-03-16 20:26 [PATCH 6/7] fix undefined behavior tyson.w.smith

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).