trinity.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Vince Weaver <vincent.weaver@maine.edu>
To: Dave Jones <davej@redhat.com>
Cc: Andrew Honig <ahonig@google.com>, trinity@vger.kernel.org
Subject: Re: [patch] fix divide by zero in random/taviso()
Date: Tue, 21 Jan 2014 14:47:57 -0500 (EST)	[thread overview]
Message-ID: <alpine.DEB.2.10.1401211441570.5901@vincent-weaver-1.um.maine.edu> (raw)
In-Reply-To: <20140121190703.GA27774@redhat.com>

On Tue, 21 Jan 2014, Dave Jones wrote:

> random.c:57:16: warning: Division by zero
>                 if (!temp) r %= temp;
>                            ~~^~~~~~~
> random.c:62:26: warning: Division by zero
>                 if (!temp) r |= rand() % temp;
>                                 ~~~~~~~^~~~~~
> 
> Vince, want to send a follow-up fix ?

Yes, brown-paper bag time.  Never trust "it compiles and runs" as 
testing for a bug that only shows up 1 in 4 billion times anyway.

I've copied the code into a test harness and actually ran it with code 
that returns zero more often to verify it works (after hacking things 
so gcc didn't "helpfully" optimize the zero generating code away).

---

The previous fix to the divide-by-zero bug in random/taviso()
(fea56c28830f "fix divide by zero in random/taviso()")
had the zero-checks inverted.  This meant the old div-by-zero bug
was still there, and worse, it meant that the number being generated
always had 32-bits of zeros in the bottom half on a 64-bit machine.

Signed-off-by: Vince Weaver <vincent.weaver@maine.edu>

diff --git a/random.c b/random.c
index 75fa868..38ed22f 100644
--- a/random.c
+++ b/random.c
@@ -54,12 +54,12 @@ static unsigned long taviso(void)
 
 	case 1:	temp = rand();
 		r = rand();
-		if (!temp) r %= temp;
+		if (temp) r %= temp;
 #if __WORDSIZE == 64
 		r <<= 32;
 
 		temp = rand();
-		if (!temp) r |= rand() % temp;
+		if (temp) r |= rand() % temp;
 #endif
 		break;
 

      reply	other threads:[~2014-01-21 19:47 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-01-17 20:21 divide by zero in random/taviso() Vince Weaver
2014-01-17 20:41 ` [patch] fix " Vince Weaver
2014-01-17 21:03   ` Dave Jones
     [not found]     ` <CAKB9nXvQmAU-PXjqbezf7G5bWfrjMTD5LjoFnj98K8Tyy5eQ2Q@mail.gmail.com>
2014-01-21 19:07       ` Dave Jones
2014-01-21 19:47         ` Vince Weaver [this message]

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=alpine.DEB.2.10.1401211441570.5901@vincent-weaver-1.um.maine.edu \
    --to=vincent.weaver@maine.edu \
    --cc=ahonig@google.com \
    --cc=davej@redhat.com \
    --cc=trinity@vger.kernel.org \
    /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).