Skip to content

Commit

Permalink
ICU4J: Throw parse error for lone surrogates
Browse files Browse the repository at this point in the history
  • Loading branch information
catamorphism committed Sep 18, 2024
1 parent 5e376ba commit fb0c873
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ int peekChar() {
return buffer.charAt(cursor);
}

// Used when checking for unpaired surrogates
int readChar() {
int ch = peekChar();
cursor++;
return (char) ch;
}

int readCodePoint() {
// TODO: remove this?
// START Detect possible infinite loop
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,17 @@ private MFDataModel.PatternPart getPatternPart() throws MFParseException {
}
}

private String getText() {
private String getText() throws MFParseException {
StringBuilder result = new StringBuilder();
while (true) {
int ch = input.readChar();
// Check for unmatched surrogates
checkCondition(!(Character.isHighSurrogate((char) ch)
&& (input.atEnd()
|| !Character.isLowSurrogate((char) input.peekChar()))),
"Unpaired high surrogate");
checkCondition (!Character.isLowSurrogate((char) ch), "Unpaired low surrogate");
input.backup(1);
int cp = input.readCodePoint();
switch (cp) {
case EOF:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,36 @@ public void test() {
mf2.formatToString(Args.NONE));
}

@Test
public void testHighLoneSurrogate() {
try {
MessageFormatter mf2 = MessageFormatter.builder()
.setPattern("\uda02").build();
assertEquals("testHighLoneSurrogate: expected to throw, but didn't",
false, true);
} catch (IllegalArgumentException e) {
// Parse error was thrown, as expected
} catch (Exception e) {
assertEquals("testHighLoneSurrogate: expected IllegalArgumentException "
+ "but threw " + e.toString(), false, true);
}
}

@Test
public void testLowLoneSurrogate() {
try {
MessageFormatter mf2 = MessageFormatter.builder()
.setPattern("\udc02").build();
assertEquals("testLowLoneSurrogate: expected to throw, but didn't",
false, true);
} catch (IllegalArgumentException e) {
// Parse error was thrown, as expected
} catch (Exception e) {
assertEquals("testLowLoneSurrogate: expected IllegalArgumentException "
+ "but threw " + e.toString(), false, true);
}
}

@Test
public void testDateFormat() {
Date expiration = new Date(2022 - 1900, java.util.Calendar.OCTOBER, 27);
Expand Down

0 comments on commit fb0c873

Please sign in to comment.