Skip to content

Commit 34887cb

Browse files
authored
Merge pull request #3105 from lha/check-for-invalid-plist-error
Handle failing to parse plist
2 parents 5a80967 + e63f4a3 commit 34887cb

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

Sources/Foundation/PropertyListSerialization.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ open class PropertyListSerialization : NSObject {
6363
var error: Unmanaged<CFError>? = nil
6464
let decoded = withUnsafeMutablePointer(to: &fmt) { (outFmt: UnsafeMutablePointer<CFPropertyListFormat>) -> AnyObject? in
6565
withUnsafeMutablePointer(to: &error) { (outErr: UnsafeMutablePointer<Unmanaged<CFError>?>) -> AnyObject? in
66-
return CFPropertyListCreateWithData(kCFAllocatorSystemDefault, data._cfObject, CFOptionFlags(CFIndex(opt.rawValue)), outFmt, outErr).takeRetainedValue()
66+
let d = CFPropertyListCreateWithData(kCFAllocatorSystemDefault, data._cfObject, CFOptionFlags(CFIndex(opt.rawValue)), outFmt, outErr)
67+
return d?.takeRetainedValue()
6768
}
6869
}
6970
format?.pointee = PropertyListFormat(rawValue: UInt(fmt.rawValue))!

Tests/Foundation/Tests/TestPropertyListSerialization.swift

+11
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class TestPropertyListSerialization : XCTestCase {
1313
("test_BasicConstruction", test_BasicConstruction),
1414
("test_decodeData", test_decodeData),
1515
("test_decodeStream", test_decodeStream),
16+
("test_decodeEmptyData", test_decodeEmptyData),
1617
]
1718
}
1819

@@ -79,4 +80,14 @@ class TestPropertyListSerialization : XCTestCase {
7980
XCTFail("value stored is not a string")
8081
}
8182
}
83+
84+
func test_decodeEmptyData() {
85+
XCTAssertThrowsError(try PropertyListSerialization.propertyList(from: Data(), format: nil)) { error in
86+
let nserror = error as NSError
87+
XCTAssertEqual(nserror.domain, NSCocoaErrorDomain)
88+
XCTAssertEqual(CocoaError(_nsError: nserror).code, .propertyListReadCorrupt)
89+
XCTAssertEqual(nserror.userInfo[NSDebugDescriptionErrorKey] as? String, "Cannot parse a NULL or zero-length data")
90+
}
91+
92+
}
8293
}

0 commit comments

Comments
 (0)