All of lore.kernel.org
 help / color / mirror / Atom feed
From: Dave Jones <davej@redhat.com>
To: Andrew Honig <ahonig@google.com>
Cc: Vince Weaver <vincent.weaver@maine.edu>, trinity@vger.kernel.org
Subject: Re: [patch] fix divide by zero in random/taviso()
Date: Tue, 21 Jan 2014 14:07:03 -0500	[thread overview]
Message-ID: <20140121190703.GA27774@redhat.com> (raw)
In-Reply-To: <CAKB9nXvQmAU-PXjqbezf7G5bWfrjMTD5LjoFnj98K8Tyy5eQ2Q@mail.gmail.com>

On Tue, Jan 21, 2014 at 11:03:43AM -0800, Andrew Honig wrote:
 
 > >  > In random.c the taviso() routine uses "rand() % rand()" which
 > >  > can cause a floating point exception if rand() returns 0.
 > >  > Add some tests to make sure that doesn't happen.
 > >  >
 > >  > Signed-off-by: Vince Weaver <vincent.weaver@maine.edu>
 > >  >
 > >  > diff --git a/random.c b/random.c
 > >  > index 7def9ff..a71fcea 100644
 > >  > --- a/random.c
 > >  > +++ b/random.c
 > >  > @@ -42,6 +42,7 @@ static unsigned long randbits(int limit)
 > >  >  static unsigned long taviso(void)
 > >  >  {
 > >  >      unsigned long r = 0;
 > >  > +    unsigned long temp;
 > >  >
 > >  >      switch (rand() % 4) {
 > >  >      case 0: r = rand() & rand();
 > >  > @@ -51,10 +52,14 @@ static unsigned long taviso(void)
 > >  >  #endif
 > >  >              break;
 > >  >
 > >  > -    case 1: r = rand() % rand();
 > >  > +    case 1: temp = rand();
 > >  > +            r = rand();
 > >
 > 
 > Perhaps, I'm reading this wrong, but this looks backwards to me.  This will
 > perform the mod operation only when temp is 0.  Shouldn't this be
 > if (temp) r %= temp;

clang seems to agree.

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 ?

	Dave

  parent reply	other threads:[~2014-01-21 19:07 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 [this message]
2014-01-21 19:47         ` Vince Weaver

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=20140121190703.GA27774@redhat.com \
    --to=davej@redhat.com \
    --cc=ahonig@google.com \
    --cc=trinity@vger.kernel.org \
    --cc=vincent.weaver@maine.edu \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.