JDK-6248507 : AbstractStringBuilder.replace does not handle count < start < end
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 5.0
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2005-03-31
  • Updated: 2012-01-11
  • Resolved: 2005-08-10
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other JDK 6
5.0u6Fixed 6 b47Fixed
Description
FULL PRODUCT VERSION :
java version "1.5.0_01"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_01-b08)
Java HotSpot(TM) Client VM (build 1.5.0_01-b08, mixed mode, sharing)

ADDITIONAL OS VERSION INFORMATION :
XP 5.1.2600

A DESCRIPTION OF THE PROBLEM :
If you call replace with start< end and both start and end indexes bigger than the size of the string, it throws a StringIndexOutOfBoundsException("start > end"). It should either

1) Not throw an exception. If end > count, it currently sets end = count, and just replaces to the end of the character sequence. It would be reasonable if it did the same thing for end > count and start > count - ie, append instead of replace.

2) Throw an exception with a better error message. Since start < end when the function is called, this error message is completely confusing, and would be utterly indecipherable to anyone who didn't have the source to look at. (On a related note, the actual "start > end" message could be improved to print out the start and end indexes as well).

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
StringBuffer sb=new StringBuffer("string");
sb.replace(10,12,"buffer");

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Desired result: Either
1) sb becomes "stringbuffer" or
2) StringIndexOutOfBoundsException with message "start > number of characters (10 > 6)"
ACTUAL -
Result: StringIndexOutOfBoundsException with message start > end
(Even though start < end in the function call)


ERROR MESSAGES/STACK TRACES THAT OCCUR :
Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: start > end

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
StringBuffer sb=new StringBuffer("string");
sb.replace(10,12,"buffer");
---------- END SOURCE ----------
###@###.### 2005-03-31 05:48:12 GMT

Comments
EVALUATION Oops. Trivial modification to provide correct exception detail message. ###@###.### 2005-06-15 22:06:10 GMT
15-06-2005