From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 18923 invoked by alias); 6 Nov 2002 17:46:05 -0000 Mailing-List: contact gcc-prs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-prs-owner@gcc.gnu.org Received: (qmail 18880 invoked by uid 71); 6 Nov 2002 17:46:03 -0000 Resent-Date: 6 Nov 2002 17:46:03 -0000 Resent-Message-ID: <20021106174603.18879.qmail@sources.redhat.com> Resent-From: gcc-gnats@gcc.gnu.org (GNATS Filer) Resent-Cc: gcc-prs@gcc.gnu.org, gcc-bugs@gcc.gnu.org, java-prs@gcc.gnu.org Resent-Reply-To: gcc-gnats@gcc.gnu.org, svens@it.uu.se Received: (qmail 14904 invoked by uid 61); 6 Nov 2002 17:41:29 -0000 Message-Id: <20021106174129.14903.qmail@sources.redhat.com> Date: Wed, 06 Nov 2002 09:46:00 -0000 From: svens@it.uu.se Reply-To: svens@it.uu.se To: gcc-gnats@gcc.gnu.org X-Send-Pr-Version: gnatsweb-2.9.3 (1.1.1.1.2.31) Subject: libgcj/8481: java.Random.nextInt(int) may return negative X-SW-Source: 2002-11/txt/msg00311.txt.bz2 List-Id: >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==