Bizarre StringBuffer/toHexString bug

Stuart Ballard sballard at NetReach.Net
Wed Aug 29 14:48:15 PDT 2001

This is really strange.

I'm using the debian package of kaffe, version 1.0.6-3.

At first I thought this was a pure Kaffe bug, but apparently it isn't -
Sun's JVM gives an error too, but it's a different error (a segmentation
fault). And it's not a compiler bug because Jikes and Javac behave the
same way. I found that compiling against Klasses.jar fixed the
segmentation fault on Sun's java but not the misbehavior on kaffe. So to

       Klasses.jar  J2re1.3/rt.jar
Kaffe   Exception     Exception
Java     Correct      Segfault

(The columns in this table refer to what the class was *compiled*
against only - when I *ran* the VMs they each used their own classlibs).

I can't for the life of me figure out what's so strange about what I'm
doing that it would cause Sun's JVM to segfault with its own libraries -
or why Kaffe gives this exception. Note that Sun's JVM only fails on
this reduced testcase - the original program that used a more complex
version of this code runs fine under Sun's JVM. But perhaps I'm just
avoiding the segfault through luck in that case.

Any ideas would be much appreciated.

This is the output I get under kaffe:

$ kaffe KaffeBug ce:ccx:attr
        at java.lang.Throwable.fillInStackTrace(
        at java.lang.Throwable.<init>(
        at java.lang.Exception.<init>(
        at java.lang.RuntimeException.<init>(
        at java.lang.StringBuffer.append(
        at java.lang.StringBuffer.append(
        at KaffeBug.main(

This is the class I'm using:

class KaffeBug {
  public static void main(String[] args) {
    String result = calcHash();
    // This version succeeds.
    // So does this version.
    // This version fails.
    System.err.println("Hash is " + result);
  static String calcHash() {
    StringBuffer sb = new StringBuffer(10);
    // Both the use of toHexString() and the replace() are necessary to
    // this fail. Hardcoding hx = "1a06" does not fail, and neither does
    // append()ing hx rather than using replace(). There's nothing
    // special about the number 6662, just what I happened to be trying
    // reasons too complex to go into.
    String hx = Integer.toHexString(6662);
    int len = sb.length();
    sb.replace(len - hx.length(), len, hx);
    return sb.toString();

More information about the kaffe mailing list