Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4089062

String created from StringBuffer overwritten via setLength(0)

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P1 P1
    • 1.1.6
    • 1.2.0
    • core-libs
    • None
    • 1.1.6
    • generic, x86
    • generic, windows_nt
    • Verified

        A String created from a StringBuffer can be overwritten if setLength()
        to a value less than the buffer length is called on the StringBuffer
        and then the StringBuffer is appended to.

        The following program demonstrates this:

        class Smashing {
            public static void main(String[] argv) {
                StringBuffer active = new StringBuffer();
                active.append("jumble");
                String a = active.toString();
                active.setLength(0);
                active.append("remark");
                String b = active.toString();
                active.setLength(0);
                System.out.println("a: " + a);
                System.out.println("b: " + b);
            }
        }

        Output is:

        10 1.2 - palau: home/rfield] javac Smashing.java
        11 1.2 - palau: home/rfield] java Smashing
        a: remark
        b: remark

        Should not be effected by changes to the buffer (from StringBuffer doc):

         public String toString()

              Converts to a string representing the data in this string buffer. A new String object is allocated and initialized
              to contain the character sequence currently represented by this string buffer. This String is then returned.
              Subsequent changes to the string buffer do not affect the contents of the String.

              rfield Robert Field (Inactive)
              rfield Robert Field (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: