[Bug libgcj/12350] New: StringBuffer.substring handles shared flag incorrected.

suckfish at ihug dot co dot nz gcc-bugzilla@gcc.gnu.org
Sun Sep 21 10:36:00 GMT 2003


PLEASE REPLY TO gcc-bugzilla@gcc.gnu.org ONLY, *NOT* gcc-bugs@gcc.gnu.org.

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=12350

           Summary: StringBuffer.substring handles shared flag incorrected.
           Product: gcc
           Version: 3.4
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: libgcj
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: suckfish at ihug dot co dot nz
                CC: gcc-bugs at gcc dot gnu dot org

The substring method of java.lang.StringBuffer can clear the boolean shared
field of the object.  This is incorrect.  The value may be shared for some
reason other than the current call to substring.

Test case and patch inline below.

-----------------test case------------------
public class test
{
    static public void main (String[] ignored) throws Throwable
    {
	StringBuffer b = new StringBuffer ("Good string.  More than 16 chars.");

	// Should cause sharing.
	String s = b.toString();

	// Take a char by char unshared copy of s.
	String t = new String (s.toCharArray());

	b.substring (0, 4);	// BUG: Clears shared flag.
	b.replace (0, 4, "Bad "); // Modifies shared data.

	System.out.println (s);
	assert s.equals (t);
    }

}
-------------------patch---------------------
Index: libjava/java/lang/StringBuffer.java
===================================================================
RCS file: /cvsroot/gcc/gcc/libjava/java/lang/StringBuffer.java,v
retrieving revision 1.15
diff -u -r1.15 StringBuffer.java
--- libjava/java/lang/StringBuffer.java 24 Mar 2003 00:50:18 -0000      1.15
+++ libjava/java/lang/StringBuffer.java 21 Sep 2003 08:53:19 -0000
@@ -565,7 +565,9 @@
     if (len == 0)
       return "";
     // Share the char[] unless 3/4 empty.
-    shared = (len << 2) >= value.length;
+    boolean shared = (len << 2) >= value.length;
+    if (shared)
+      this.shared = true;
     // Package constructor avoids an array copy.
     return new String(value, beginIndex, len, shared);
   }



More information about the Gcc-bugs mailing list