Skip to content

Commit

Permalink
Throw ParseException for NSNumber strings in XML plists (Issue #76)
Browse files Browse the repository at this point in the history
  • Loading branch information
3breadt committed Apr 10, 2023
1 parent 0145618 commit 81be6e6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
26 changes: 14 additions & 12 deletions src/main/java/com/dd/plist/XMLPropertyListParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import javax.xml.parsers.ParserConfigurationException;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
Expand Down Expand Up @@ -110,7 +109,7 @@ public static DocumentBuilder getDocBuilder() throws ParserConfigurationExceptio
* @throws java.io.IOException If any I/O error occurs while reading the file.
* @throws org.xml.sax.SAXException If any parse error occurs.
* @throws com.dd.plist.PropertyListFormatException If the given property list has an invalid format.
* @throws java.text.ParseException If a date string could not be parsed.
* @throws java.text.ParseException If a number or date string could not be parsed.
* @see javax.xml.parsers.DocumentBuilder#parse(java.io.File)
*/
public static NSObject parse(File f)
Expand All @@ -128,7 +127,7 @@ public static NSObject parse(File f)
* @throws java.io.IOException If any I/O error occurs while reading the file.
* @throws org.xml.sax.SAXException If any parse error occurs.
* @throws com.dd.plist.PropertyListFormatException If the given property list has an invalid format.
* @throws java.text.ParseException If a date string could not be parsed.
* @throws java.text.ParseException If a number or date string could not be parsed.
* @see javax.xml.parsers.DocumentBuilder#parse(java.io.File)
*/
public static NSObject parse(Path path)
Expand All @@ -148,7 +147,7 @@ public static NSObject parse(Path path)
* @throws java.io.IOException If any I/O error occurs while reading the file.
* @throws org.xml.sax.SAXException If any parse error occurs.
* @throws com.dd.plist.PropertyListFormatException If the given property list has an invalid format.
* @throws java.text.ParseException If a date string could not be parsed.
* @throws java.text.ParseException If a number or date string could not be parsed.
*/
public static NSObject parse(final byte[] bytes)
throws ParserConfigurationException, ParseException, SAXException, PropertyListFormatException, IOException {
Expand All @@ -168,7 +167,7 @@ public static NSObject parse(final byte[] bytes)
* @throws java.io.IOException If any I/O error occurs while reading the file.
* @throws org.xml.sax.SAXException If any parse error occurs.
* @throws com.dd.plist.PropertyListFormatException If the given property list has an invalid format.
* @throws java.text.ParseException If a date string could not be parsed.
* @throws java.text.ParseException If a number or date string could not be parsed.
* @see javax.xml.parsers.DocumentBuilder#parse(java.io.InputStream)
*/
public static NSObject parse(InputStream is)
Expand All @@ -189,7 +188,7 @@ public static NSObject parse(InputStream is)
* @throws java.io.IOException If any I/O error occurs while reading the file.
* @throws org.xml.sax.SAXException If any parse error occurs.
* @throws com.dd.plist.PropertyListFormatException If the given property list has an invalid format.
* @throws java.text.ParseException If a date string could not be parsed.
* @throws java.text.ParseException If a number or date string could not be parsed.
* @see javax.xml.parsers.DocumentBuilder#parse(java.io.InputStream)
*/
public static NSObject parse(Reader reader)
Expand All @@ -204,7 +203,7 @@ public static NSObject parse(Reader reader)
* @return The root NSObject of the property list contained in the XML document.
* @throws java.io.IOException If any I/O error occurs while reading the file.
* @throws com.dd.plist.PropertyListFormatException If the given property list has an invalid format.
* @throws java.text.ParseException If a date string could not be parsed.
* @throws java.text.ParseException If a number or date string could not be parsed.
*/
public static NSObject parse(Document doc) throws PropertyListFormatException, IOException, ParseException {
DocumentType docType = doc.getDoctype();
Expand Down Expand Up @@ -242,7 +241,7 @@ public static NSObject parse(Document doc) throws PropertyListFormatException, I
* @param n The XML node.
* @return The corresponding NSObject.
* @throws java.io.IOException If any I/O error occurs while parsing a Base64 encoded NSData object.
* @throws java.text.ParseException If a date string could not be parsed.
* @throws java.text.ParseException If a number or date string could not be parsed.
*/
private static NSObject parseObject(Node n) throws ParseException, IOException {
String type = n.getNodeName();
Expand Down Expand Up @@ -274,7 +273,11 @@ private static NSObject parseObject(Node n) throws ParseException, IOException {
return new NSNumber(false);
case "integer":
case "real":
return new NSNumber(getNodeTextContents(n));
try {
return new NSNumber(getNodeTextContents(n));
} catch (IllegalArgumentException ex) {
throw new ParseException("The NSNumber object has an invalid format.", -1);
}
case "string":
return new NSString(getNodeTextContents(n));
case "data":
Expand Down Expand Up @@ -334,10 +337,9 @@ private static String getNodeTextContents(Node n) {
}
}

return "";
} else {
return "";
}

return "";
}
}

Expand Down
8 changes: 7 additions & 1 deletion src/test/java/com/dd/plist/test/IssueTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,17 @@ public void testIssue75_CyclicReferencesInBinaryPropertyLists(File file) {
}

@Test
public void testIssue76_UnexpectedIllegalArgumentException() {
public void testIssue76_UnexpectedIllegalArgumentExceptionForInvalidNumberInAsciiPropertyList() {
File plistFile = new File("test-files/github-issue76.plist");
assertThrows(ParseException.class, () -> PropertyListParser.parse(plistFile));
}

@Test
public void testIssue76_UnexpectedIllegalArgumentExceptionForInvalidNumberInXmlPropertyList() {
File plistFile = new File("test-files/github-issue76-xml.plist");
assertThrows(ParseException.class, () -> PropertyListParser.parse(plistFile));
}

@Test
public void testIssue78_NullReferenceExceptionForInvalidNSDictionaryKey() {
File plistFile = new File("test-files/github-issue78.plist");
Expand Down
8 changes: 8 additions & 0 deletions test-files/github-issue76-xml.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>key1</key>
<integer>42xy</integer>
</dict>
</plist>

0 comments on commit 81be6e6

Please sign in to comment.