hello all,

the attached patch fixes the above PR.

2006-08-06  Raif S. Naffah  <[EMAIL PROTECTED]>

        PR Classpath/27372
        * java/math/BigInteger.java: Updated copyright year.
        (init): Consume as little bytes as possible.


a new Mauve test gnu/testlet/java/math/BigInteger/TestOfPR27372.java has been 
added to highlight the problem and validate the fix.  a new method 
(testCtorIntRandom) has been added to the Mauve test 
gnu/testlet/java/math/BigInteger/ctor.java to ensure no regression was 
introduced.

still, because BigInteger is a sensitive class, i would appreciate it if 
another pair of eyes can go over the patch to ensure no mistakes have been 
introduced.


TIA + cheers;
rsn
Index: BigInteger.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/math/BigInteger.java,v
retrieving revision 1.28
diff -u -r1.28 BigInteger.java
--- BigInteger.java	27 May 2006 02:09:30 -0000	1.28
+++ BigInteger.java	6 Aug 2006 07:30:15 -0000
@@ -1,5 +1,5 @@
 /* java.math.BigInteger -- Arbitary precision integers
-   Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2005  Free Software Foundation, Inc.
+   Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2005, 2006  Free Software Foundation, Inc.

 This file is part of GNU Classpath.

@@ -197,8 +197,20 @@
   private void init(int numBits, Random rnd)
   {
     int highbits = numBits & 31;
+    // minimum number of bytes to store the above number of bits
+    int highBitByteCount = (highbits + 7) / 8;
+    // number of bits to discard from the last byte
+    int discardedBitCount = highbits % 8;
+    if (discardedBitCount != 0)
+      discardedBitCount = 8 - discardedBitCount;
+    byte[] highBitBytes = new byte[highBitByteCount];
     if (highbits > 0)
-      highbits = rnd.nextInt() >>> (32 - highbits);
+      {
+        rnd.nextBytes(highBitBytes);
+        highbits = (highBitBytes[highBitByteCount - 1] & 0xFF) >>> discardedBitCount;
+        for (int i = highBitByteCount - 2; i >= 0; i--)
+          highbits = (highbits << 8) | (highBitBytes[i] & 0xFF);
+      }
     int nwords = numBits / 32;

     while (highbits == 0 && nwords > 0)

Reply via email to