[kaffe] Bug Report: BigInteger.toString() fails for large numbers

Alan Eliasen eliasen at mindspring.com
Mon Apr 19 23:14:02 PDT 2004

   Here's an additional testcase for the BigInteger.toString() problem.
Again, it requires the GMP libraries to be compiled in.

   The following sample creates larger and larger numbers.  At some point, the
thread either silently stops running (but the process doesn't exit), or
segfaults.  The exact behavior can be changed (somewhat randomly) by tweaking
the "1.1" number in the code below.

   Surprisingly, when larger and larger numbers are created gradually, the
largest number that can be calculated is bigger than in the last testcase.
(The last testcase barfs at about 2^13,000,000, while this case can get up to
about 2^16,000,000 (or sometimes much less.))  This may hint at a memory
allocation/reallocation bug, or memory corruption, but the behavior seems to
be very repeatable for any given parameters.

   Here are some things I've tried:

   * Increasing heap and stack space.  No change in behavior.

   * Adding null checks to the bi_malloc and bi_realloc routines in
libraries/clib/math/BigInteger.c.  These don't seem to return null values
before the crash.  Could memory allocation be failing more subtly?

   In any case, I'm hoping that we can resolve this, as Kaffe w/GMP literally
works *thousands* of times faster than other Java VMs when calculating large
BigIntegers, and this would be a huge success story, and a reason for many
people to prefer Kaffe for large integer work.

import java.math.BigInteger;

public class BigIntTest2
   public static void main(String[] args)
      BigInteger b = new BigInteger("1");
      BigInteger p;
      String s;

      int bits = 1;
      int shift = 10;
      long start, end;

      while (bits<100000000)
         p = b.shiftLeft(bits-1);
         System.out.print("2^" + (bits-1));

         start = System.currentTimeMillis();
         s = p.toString();
         end = System.currentTimeMillis();

         System.out.println("\tformatted in\t" + (end-start) + " ms");

         if (bits<1000000)
            bits *= 2;
            bits = (int) ((double) bits * 1.1);


  Alan Eliasen                 | "You cannot reason a person out of a
  eliasen at mindspring.com       |  position he did not reason himself
  http://futureboy.homeip.net/ |  into in the first place."
                               |     --Jonathan Swift

More information about the kaffe mailing list