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

Vince Weaver <[email protected]>
Newsgroups org.kernel.vger.trinity
Message-ID <[email protected]>
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 <[email protected]>

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;
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.