From 44ecdc281bde2e9eb13873ae8c062ddc2638d702 Mon Sep 17 00:00:00 2001 From: Daniel Dreibrodt Date: Wed, 15 Aug 2018 18:02:33 +0200 Subject: [PATCH] Further improvements of error handling in escape sequences within ASCII property lists --- .../com/dd/plist/ASCIIPropertyListParser.java | 29 +++++++++++++------ 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/dd/plist/ASCIIPropertyListParser.java b/src/main/java/com/dd/plist/ASCIIPropertyListParser.java index a9f2eb2..37b120f 100644 --- a/src/main/java/com/dd/plist/ASCIIPropertyListParser.java +++ b/src/main/java/com/dd/plist/ASCIIPropertyListParser.java @@ -37,7 +37,7 @@ *

* 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. *

*

* Resources on ASCII property list format: @@ -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); @@ -610,7 +610,7 @@ 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(); @@ -618,11 +618,11 @@ private static synchronized String parseQuotedString(String s) throws Unsupporte 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; } } @@ -647,6 +647,7 @@ private static char parseEscapedSequence(StringCharacterIterator iterator) throw { case '\\': case '"': + case '\'': return c; case 'b': return '\b'; @@ -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': @@ -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()); } } }