From: tyson.w.smith@gmail.com
To: davej@codemonkey.org.uk
Cc: trinity@vger.kernel.org, tysmith@motorola.com,
Tyson Smith <tyson.w.smith@gmail.com>
Subject: [PATCH 6/7] fix undefined behavior
Date: Mon, 16 Mar 2015 13:26:47 -0700 [thread overview]
Message-ID: <1426537607-55003-1-git-send-email-tyson.w.smith@gmail.com> (raw)
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
reply other threads:[~2015-03-16 20:26 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1426537607-55003-1-git-send-email-tyson.w.smith@gmail.com \
--to=tyson.w.smith@gmail.com \
--cc=davej@codemonkey.org.uk \
--cc=trinity@vger.kernel.org \
--cc=tysmith@motorola.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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 read-only IMAP folder(s) and NNTP newsgroup(s).