-
Bug
-
Resolution: Fixed
-
P3
-
7u10, 8
-
b04
-
linux
FULL PRODUCT VERSION :
java version " 1.7.0_10 "
Java(TM) SE Runtime Environment (build 1.7.0_10-b18)
Java HotSpot(TM) Server VM (build 23.6-b04, mixed mode)
ADDITIONAL OS VERSION INFORMATION :
Linux mima-nb 3.2.0-35-generic-pae #55-Ubuntu SMP Wed Dec 5 18:04:39 UTC 2012 i686 i686 i386 GNU/Linux
A DESCRIPTION OF THE PROBLEM :
The contract for String.indexOf(String str, int fromIndex) as stated by javadoc is as follows:
" The returned index is the smallest value k for which:
k >= fromIndex && this.startsWith(str, k)
If no such value of k exists, then -1 is returned. "
Formally it will look like this:
( (k >= fromIndex) && this.startsWith(str, k) ) || (k == -1)
which implicates
(k >= fromIndex) || (k == -1)
And here is where this contract is broken:
given:
s instanceof String
fromIndex > s.length()
when
int k = s.indexOf( " " , fromIndex)
then
k == s.length()
0 <= s.length() == k < fromIndex
which implicates
0 <= k < fromIndex
which implicates
!(k == -1) && !(k >= fromIndex)
which implicates
!(k >= fromIndex || k == -1)
and contracts states exactly opposite:
k >= fromIndex || k == -1
This finishes the proof that the contract is broken in case presented above.
The root cause of the bug is the first if in the following method:
static int indexOf(char[] source, int sourceOffset, int sourceCount,
char[] target, int targetOffset, int targetCount,
int fromIndex) {
if (fromIndex >= sourceCount) {
return (targetCount == 0 ? sourceCount : -1);
}
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
String s = " bug " ;
int fromIndex = s.length() + 1;
int k = s.indexOf( " " , fromIndex);
System.out.println ( " fromIndex= " + fromIndex + " , k= " + k);
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
// system output: fromIndex=4, k=-1
ACTUAL -
// system output: fromIndex=4, k=3
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
public class StringIndexOfContractBug {
public static void main(String[] args) {
String s = " bug " ;
int fromIndex = s.length() + 1;
int k = s.indexOf( " " , fromIndex);
System.out.println ( " fromIndex= " + fromIndex + " , k= " + k);
}
}
---------- END SOURCE ----------
java version " 1.7.0_10 "
Java(TM) SE Runtime Environment (build 1.7.0_10-b18)
Java HotSpot(TM) Server VM (build 23.6-b04, mixed mode)
ADDITIONAL OS VERSION INFORMATION :
Linux mima-nb 3.2.0-35-generic-pae #55-Ubuntu SMP Wed Dec 5 18:04:39 UTC 2012 i686 i686 i386 GNU/Linux
A DESCRIPTION OF THE PROBLEM :
The contract for String.indexOf(String str, int fromIndex) as stated by javadoc is as follows:
" The returned index is the smallest value k for which:
k >= fromIndex && this.startsWith(str, k)
If no such value of k exists, then -1 is returned. "
Formally it will look like this:
( (k >= fromIndex) && this.startsWith(str, k) ) || (k == -1)
which implicates
(k >= fromIndex) || (k == -1)
And here is where this contract is broken:
given:
s instanceof String
fromIndex > s.length()
when
int k = s.indexOf( " " , fromIndex)
then
k == s.length()
0 <= s.length() == k < fromIndex
which implicates
0 <= k < fromIndex
which implicates
!(k == -1) && !(k >= fromIndex)
which implicates
!(k >= fromIndex || k == -1)
and contracts states exactly opposite:
k >= fromIndex || k == -1
This finishes the proof that the contract is broken in case presented above.
The root cause of the bug is the first if in the following method:
static int indexOf(char[] source, int sourceOffset, int sourceCount,
char[] target, int targetOffset, int targetCount,
int fromIndex) {
if (fromIndex >= sourceCount) {
return (targetCount == 0 ? sourceCount : -1);
}
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
String s = " bug " ;
int fromIndex = s.length() + 1;
int k = s.indexOf( " " , fromIndex);
System.out.println ( " fromIndex= " + fromIndex + " , k= " + k);
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
// system output: fromIndex=4, k=-1
ACTUAL -
// system output: fromIndex=4, k=3
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
public class StringIndexOfContractBug {
public static void main(String[] args) {
String s = " bug " ;
int fromIndex = s.length() + 1;
int k = s.indexOf( " " , fromIndex);
System.out.println ( " fromIndex= " + fromIndex + " , k= " + k);
}
}
---------- END SOURCE ----------
- duplicates
-
JDK-8071431 "ab".indexOf("", 3) returns the value 2, violating the specification of indexOf
-
- Closed
-