JDK-6961768 : String.indexOf returning index past end of string
  • Type: Bug
  • Component: hotspot
  • Sub-Component: compiler
  • Affected Version: 6u10
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: linux
  • CPU: x86
  • Submitted: 2010-06-16
  • Updated: 2012-03-20
  • Resolved: 2010-08-23
Related Reports
Duplicate :  
Description
FULL PRODUCT VERSION :
java version "1.6.0_20"
Java(TM) SE Runtime Environment (build 1.6.0_20-b02)
Java HotSpot(TM) 64-Bit Server VM (build 16.3-b01, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Linux <hostname> 2.6.18-194.3.1.el5 #1 SMP Thu May 13 13:08:30 EDT 2010 x86_64 GNU/Linux


EXTRA RELEVANT SYSTEM CONFIGURATION :
Dual network cards, otherwise standard build

A DESCRIPTION OF THE PROBLEM :
This is sporadic and I can't build a repeatable test case, but String.indexOf() will sometimes return an index past the end of a string, when it should return -1.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
I can't reproduce this reliably.  I have added logging to capture as much info as I can - in the latest occurrence, my test string was length 58201.  Here's an extract of my source code:

// I include the next line to illustrate how my string to be searched is created
String message = someLargerString.substring(start, end);

// At this point, message.length() returns 58201

// Here's how the search string is built:
String fieldname = "holds";
String search = "<" + fieldname + ">";

// Now here's the problem...(note: message does NOT contain the search string)
int index = message.indexOf(search);

// At this point, index = 58237

This happens only sporadically - it will run fine dozens or hundreds of times before failing.  Restarting the program (new JVM instance) means it will run fine again for a while, but once it starts doing this, it keeps doing it until I shut down the JVM and restart.


EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
indexOf should have returned -1
ACTUAL -
indexOf returned a value larger than the length of the string being searched

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
I can't reproduce this reliably.
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Check output of indexOf for a value larger than the string length, and treat such an occurrence as -1.