libgcj/8481: java.Random.nextInt(int) may return negative
Wed Nov 6 09:46:00 GMT 2002

>Number:         8481
>Category:       libgcj
>Synopsis:       java.Random.nextInt(int) may return negative
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    unassigned
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Wed Nov 06 09:46:02 PST 2002
>Originator:     Sven Sandberg
>Release:        gcc 3.2 (or CVS from 2002-11-06)
The bug is independent of envionment.
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 with the parameter 32, but it should be 31 (both to be correct and according to the specification at 
(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.)
The attached file exploits the bug and prints:
i=867384, x=-222
Here's a patch against a recent CVS:

Index: libjava/java/util/
RCS file: /cvsroot/gcc/gcc/libjava/java/util/,v
retrieving revision 1.7
diff -u -r1.7
--- libjava/java/util/       18 Jun 2002 15:40:00 -0000      1.7
+++ libjava/java/util/       6 Nov 2002 16:55:16 -0000
@@ -296,7 +296,7 @@
     int bits, val;
-        bits = next(32);
+        bits = next(31);
         val = bits % n;
     while (bits - val + (n - 1) < 0);
Content-Type: application/octet-stream; name=""
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=""


More information about the Gcc-bugs mailing list