Issue | Fix Version | Assignee | Priority | Status | Resolution | Resolved In Build |
---|---|---|---|---|---|---|
JDK-2017595 | 1.2.0 | Robert Field | P1 | Resolved | Fixed | 1.2beta2 |
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.
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.
- backported by
-
JDK-2017595 String created from StringBuffer overwritten via setLength(0)
-
- Resolved
-
- duplicates
-
JDK-4095026 Changing a StringBuffer can change an existing String
-
- Closed
-