Skip to content

Commit

Permalink
Further improvements of error handling in escape sequences within ASC…
Browse files Browse the repository at this point in the history
…II property lists
  • Loading branch information
3breadt committed Aug 15, 2018
1 parent b8d6987 commit 44ecdc2
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions src/main/java/com/dd/plist/ASCIIPropertyListParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
* <p>
* Parser for ASCII property lists. Supports Apple OS X/iOS and GnuStep/NeXTSTEP format.
* This parser is based on the recursive descent paradigm, but the underlying grammar
* is not explicitely defined.
* is not explicitly defined.
* </p>
* <p>
* Resources on ASCII property list format:
Expand Down Expand Up @@ -584,10 +584,10 @@ private String parseQuotedString() throws ParseException {

String unescapedString;
try {
unescapedString = this.parseQuotedString(new String(bytArr, "UTF-8"));
unescapedString = parseQuotedString(new String(bytArr, "UTF-8"));
}
catch (ParseException ex) {
throw new ParseException(ex.getMessage(), this.index);
throw new ParseException(ex.getMessage(), this.index + ex.getErrorOffset());
}
catch (Exception ex) {
throw new ParseException("A quoted string could not be parsed.", this.index);
Expand All @@ -610,19 +610,19 @@ private String parseQuotedString() throws ParseException {
* @throws ParseException The string contains an invalid escape sequence.
*/
private static synchronized String parseQuotedString(String s) throws UnsupportedEncodingException, CharacterCodingException, ParseException {
StringBuffer result = new StringBuffer();
StringBuilder result = new StringBuilder();

StringCharacterIterator iterator = new StringCharacterIterator(s);
char c = iterator.current();

while (iterator.getIndex() < iterator.getEndIndex()) {
switch (c) {
case '\\': { //An escaped sequence is following
result.append(parseEscapedSequence(iterator));
result.append(parseEscapedSequence(iterator));
break;
}
default: { //a normal UTF-8 char
result.append(c);
result.append(c);
break;
}
}
Expand All @@ -647,6 +647,7 @@ private static char parseEscapedSequence(StringCharacterIterator iterator) throw
{
case '\\':
case '"':
case '\'':
return c;
case 'b':
return '\b';
Expand All @@ -662,7 +663,12 @@ private static char parseEscapedSequence(StringCharacterIterator iterator) throw
{
//4 digit hex Unicode value
String unicodeValue = new String(new char[] {iterator.next(), iterator.next(), iterator.next(), iterator.next()});
return (char)Integer.parseInt(unicodeValue, 16);
try {
return (char) Integer.parseInt(unicodeValue, 16);
}
catch (NumberFormatException ex) {
throw new ParseException("The property list contains a string with an invalid escape sequence: \\" + c + unicodeValue, iterator.getIndex() - 4);
}
}

case '0':
Expand All @@ -676,11 +682,16 @@ private static char parseEscapedSequence(StringCharacterIterator iterator) throw
{
//3 digit octal ASCII value
String num = new String(new char[] {c, iterator.next(), iterator.next()});
return (char)Integer.parseInt(num, 8);
try {
return (char) Integer.parseInt(num, 8);
}
catch (NumberFormatException ex) {
throw new ParseException("The property list contains a string with an invalid escape sequence: \\" + num, iterator.getIndex() - 2);
}
}

default:
throw new ParseException("The property list contains an invalid escape sequence: \\" + c, 0);
throw new ParseException("The property list contains a string with an invalid escape sequence: \\" + c, iterator.getIndex());
}
}
}

0 comments on commit 44ecdc2

Please sign in to comment.