Consider the two methods named regionMatches in class String.
Because of a programming error,, if the len argument is the most
negative integer, the while loop will not terminate immediately
(integer overflow occurs) and there may actually bean attempt to
index off the end of one of the arrays.
To correct this error, the test (--len >= 0) should be (len-- > 0).
Better yet, write "for(; len > 0; --len)".
public boolean regionMatches(boolean ignoreCase,
int toffset,
String other, int ooffset, int len) {
char ta[] = value;
int to = offset + toffset;
int tlim = offset + count;
char pa[] = other.value;
char trt[] = Character.upCase;
int po = other.offset + ooffset;
int plim = po + other.count;
if ((ooffset < 0) || (toffset < 0) || (to + len > tlim) || (po + len > plim)) {
return false;
}
while (--len >= 0) {
int c1 = ta[to++];
int c2 = pa[po++];
if ((c1 != c2)
&& (!ignoreCase ||
(c1 > 256) || (c2 > 256) ||
(trt[c1] != trt[c2]))) {
return false;
}
}
return true;
}
Because of a programming error,, if the len argument is the most
negative integer, the while loop will not terminate immediately
(integer overflow occurs) and there may actually bean attempt to
index off the end of one of the arrays.
To correct this error, the test (--len >= 0) should be (len-- > 0).
Better yet, write "for(; len > 0; --len)".
public boolean regionMatches(boolean ignoreCase,
int toffset,
String other, int ooffset, int len) {
char ta[] = value;
int to = offset + toffset;
int tlim = offset + count;
char pa[] = other.value;
char trt[] = Character.upCase;
int po = other.offset + ooffset;
int plim = po + other.count;
if ((ooffset < 0) || (toffset < 0) || (to + len > tlim) || (po + len > plim)) {
return false;
}
while (--len >= 0) {
int c1 = ta[to++];
int c2 = pa[po++];
if ((c1 != c2)
&& (!ignoreCase ||
(c1 > 256) || (c2 > 256) ||
(trt[c1] != trt[c2]))) {
return false;
}
}
return true;
}