public inbox for gcc-prs@sourceware.org
help / color / mirror / Atom feed
* libgcj/8481: java.Random.nextInt(int) may return negative
@ 2002-11-06  9:46 svens
  0 siblings, 0 replies; 2+ messages in thread
From: svens @ 2002-11-06  9:46 UTC (permalink / raw)
  To: gcc-gnats


>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
>Closed-Date:
>Last-Modified:
>Originator:     Sven Sandberg
>Release:        gcc 3.2 (or CVS from 2002-11-06)
>Organization:
>Environment:
The bug is independent of envionment.
>Description:
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.)
>How-To-Repeat:
The attached file exploits the bug and prints:
i=867384, x=-222
>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);
>Release-Note:
>Audit-Trail:
>Unformatted:
----gnatsweb-attachment----
Content-Type: application/octet-stream; name="Test.java"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="Test.java"

aW1wb3J0IGphdmEudXRpbC5SYW5kb207CnB1YmxpYyBjbGFzcyBUZXN0IHsKICAgIHB1YmxpYyBz
dGF0aWMgdm9pZCBtYWluKFN0cmluZ1tdIGFyZ3MpIHsKCVJhbmRvbSByPSBuZXcgUmFuZG9tKDAp
OwoJZm9yIChpbnQgaT0wOyBpPDEwMDAwMDA7IGkrKykgewoJICAgIGludCB4ID0gci5uZXh0SW50
KDEwMDApOwoJICAgIGlmICgoeCA8IDApIHx8ICh4ID49IDEwMDApKQoJCVN5c3RlbS5vdXQucHJp
bnRsbigiaT0iK2krIiwgeD0iK3gpOwoJfQogICAgfQp9Cg==


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: libgcj/8481: java.Random.nextInt(int) may return negative
@ 2002-11-06 20:22 tromey
  0 siblings, 0 replies; 2+ messages in thread
From: tromey @ 2002-11-06 20:22 UTC (permalink / raw)
  To: gcc-bugs, gcc-prs, java-prs, nobody, svens, tromey

Synopsis: java.Random.nextInt(int) may return negative

Responsible-Changed-From-To: unassigned->tromey
Responsible-Changed-By: tromey
Responsible-Changed-When: Wed Nov  6 20:22:56 2002
Responsible-Changed-Why:
    I'm handling this
State-Changed-From-To: open->closed
State-Changed-By: tromey
State-Changed-When: Wed Nov  6 20:22:56 2002
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.

http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=8481


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2002-11-07  4:22 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-11-06  9:46 libgcj/8481: java.Random.nextInt(int) may return negative svens
2002-11-06 20:22 tromey

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).