Skip to content

Commit 9694f90

Browse files
Henrik HallJohan Redestig
authored andcommitted
Improved error-handling in Rfc822Tokenizer
The javadoc for the Rfc822Tokenizer states that it will try to be tolerant to broken syntax instead of returning an error (as in an unchecked exception). In some rare cases where the input is clearly incorrect, the tokenizer throws a StringIndexOutOfBoundsException, which was found during one of the monkey test runs. This commits fixes that crash, and teaches the tokenizer to just continue to run anyway. Two simple junit testcases has also been added for testing the default and the errornous case.
1 parent 7bb2581 commit 9694f90

2 files changed

Lines changed: 26 additions & 2 deletions

File tree

core/java/android/text/util/Rfc822Tokenizer.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ public static void tokenize(CharSequence text, Collection<Rfc822Token> out) {
8686
i++;
8787
break;
8888
} else if (c == '\\') {
89-
name.append(text.charAt(i + 1));
89+
if (i + 1 < cursor) {
90+
name.append(text.charAt(i + 1));
91+
}
9092
i += 2;
9193
} else {
9294
name.append(c);
@@ -112,7 +114,9 @@ public static void tokenize(CharSequence text, Collection<Rfc822Token> out) {
112114
level++;
113115
i++;
114116
} else if (c == '\\') {
115-
comment.append(text.charAt(i + 1));
117+
if (i + 1 < cursor) {
118+
comment.append(text.charAt(i + 1));
119+
}
116120
i += 2;
117121
} else {
118122
comment.append(c);

tests/AndroidTests/src/com/android/unit_tests/TextUtilsTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import android.text.TextPaint;
3030
import android.text.TextUtils;
3131
import android.text.style.StyleSpan;
32+
import android.text.util.Rfc822Token;
33+
import android.text.util.Rfc822Tokenizer;
3234
import android.text.util.Rfc822Validator;
3335
import android.test.MoreAsserts;
3436

@@ -269,6 +271,24 @@ public void testEmailValidator() {
269271
}
270272
}
271273

274+
@SmallTest
275+
public void testRfc822TokenizerFullAddress() {
276+
Rfc822Token[] tokens = Rfc822Tokenizer.tokenize("Foo Bar (something) <foo@google.com>");
277+
assertNotNull(tokens);
278+
assertEquals(1, tokens.length);
279+
assertEquals("foo@google.com", tokens[0].getAddress());
280+
assertEquals("Foo Bar", tokens[0].getName());
281+
assertEquals("something",tokens[0].getComment());
282+
}
283+
284+
@SmallTest
285+
public void testRfc822TokenizeItemWithError() {
286+
Rfc822Token[] tokens = Rfc822Tokenizer.tokenize("\"Foo Bar\\");
287+
assertNotNull(tokens);
288+
assertEquals(1, tokens.length);
289+
assertEquals("Foo Bar", tokens[0].getAddress());
290+
}
291+
272292
@LargeTest
273293
public void testEllipsize() {
274294
CharSequence s1 = "The quick brown fox jumps over \u00FEhe lazy dog.";

0 commit comments

Comments
 (0)