Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

String.compareTo

44 views
Skip to first unread message

Jeremy Roman

unread,
Mar 24, 2014, 9:43:32 AM3/24/14
to
I was looking at java/lang/String.java, and I noticed this compareTo
method:

public int compareTo(String other) {
int i = 0;
boolean b = true;
while(b) {
if(i >= chars.length && i >= other.chars.length) return 0;
if(i >= chars.length) return -1;
if(i >= other.chars.length) return 1;
if(chars[i] < other.chars[i]) return -1;
if(chars[i] > other.chars[i]) return 1;
}
return 0;
}

In particular, "b" is never changed (why does it exist? to circumvent the
reachability checker?), but more interestingly, "i" is never changed,
either. So if two strings which start with the same character are passed
in, the code enters an infinite loop, e.g. "ondrej".compareTo("orange")
will not terminate.

I'm pretty sure this is a bug; I take it this code is not called in any
tests?

Ondřej Lhoták

unread,
Mar 26, 2014, 9:48:55 PM3/26/14
to
Yes, this is a bug. Thank you for catching it. I will fix it and issue
a version 5.1 of the standard library, and change Marmoset to use the
new version.

(Apparently this bug has already been fixed twice before, but the fix
was accidentally reverted when a different bug was fixed.)

Yes, the purpose of the boolean b is to make this code pass A4 even for
groups that do not yet have constant folding implemented in their
reachability checker.

0 new messages