Re: [patch] fix divide by zero in random/taviso()

Dave Jones <[email protected]>
Newsgroups org.kernel.vger.trinity
Message-ID <[email protected]>
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 <[email protected]>
 > >  >
 > >  > 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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.