Bug 8481 - java.Random.nextInt(int) may return negative
java.Random.nextInt(int) may return negative
Status: RESOLVED FIXED
Product: gcc
Classification: Unclassified
Component: libgcj
3.2
: P3 normal
: ---
Assigned To: Tom Tromey
:
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2002-11-06 09:46 UTC by svens
Modified: 2003-07-25 17:33 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments
Test.java (262 bytes, application/octet-stream)
2003-05-21 15:17 UTC, svens
Details

Note You need to log in before you can comment on or make changes to this bug.
Description svens 2002-11-06 09:46:02 UTC
java.Random.nextInt(int) has a typo that makes it return negative in some (apparantly rare) cases. To get the next random number, it calls the protected method java.Random.next(int) with the parameter 32, but it should be 31 (both to be correct and according to the specification at http://java.sun.com/j2se/1.4.1/docs/api/java/util/Random.html#nextInt(int). 
(Moreover, even if you are lucky and never exploit the bug, it does not yield the same sequence of random numbers for a given seed as Sun's implementation.)

Release:
gcc 3.2 (or CVS from 2002-11-06)

Environment:
The bug is independent of envionment.

How-To-Repeat:
The attached file exploits the bug and prints:
i=867384, x=-222
Comment 1 svens 2002-11-06 09:46:02 UTC
Fix:
Here's a patch against a recent CVS:

Index: libjava/java/util/Random.java
===================================================================
RCS file: /cvsroot/gcc/gcc/libjava/java/util/Random.java,v
retrieving revision 1.7
diff -u -r1.7 Random.java
--- libjava/java/util/Random.java       18 Jun 2002 15:40:00 -0000      1.7
+++ libjava/java/util/Random.java       6 Nov 2002 16:55:16 -0000
@@ -296,7 +296,7 @@
     int bits, val;
     do
       {
-        bits = next(32);
+        bits = next(31);
         val = bits % n;
       }
     while (bits - val + (n - 1) < 0);
Comment 2 Tom Tromey 2002-11-06 20:22:56 UTC
Responsible-Changed-From-To: unassigned->tromey
Responsible-Changed-Why: I'm handling this
Comment 3 Tom Tromey 2002-11-06 20:22:56 UTC
State-Changed-From-To: open->closed
State-Changed-Why: Thanks for the report and the patch.
    I'm checking in your fix on the trunk.
    I'm also putting it into Classpath.
    I've added your test to Mauve.
Comment 4 Tom Tromey 2002-11-07 04:38:21 UTC
From: tromey@gcc.gnu.org
To: gcc-gnats@gcc.gnu.org
Cc:  
Subject: libgcj/8481
Date: 7 Nov 2002 04:38:21 -0000

 CVSROOT:	/cvs/gcc
 Module name:	gcc
 Changes by:	tromey@gcc.gnu.org	2002-11-06 20:38:21
 
 Modified files:
 	libjava        : ChangeLog 
 	libjava/java/util: Random.java 
 
 Log message:
 	From svens@it.uu.se.  For PR libgcj/8481.
 	* java/util/Random.java (nextInt(int)): Only use 31 bits.
 
 Patches:
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libjava/ChangeLog.diff?cvsroot=gcc&r1=1.1508&r2=1.1509
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libjava/java/util/Random.java.diff?cvsroot=gcc&r1=1.7&r2=1.8