Skip to content

Commit 3883bf8

Browse files
weissiparkera
authored andcommitted
fix two non-owned strings being freed (#1032)
1 parent a1ea77f commit 3883bf8

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

CoreFoundation/Parsing.subproj/CFXMLInterface.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ void _CFXMLNodeSetURI(_CFXMLNodePtr node, const unsigned char* URI) {
415415
if (doc->URL) {
416416
xmlFree((xmlChar*)doc->URL);
417417
}
418-
doc->URL = URI;
418+
doc->URL = xmlStrdup(URI);
419419
}
420420
break;
421421

@@ -658,7 +658,7 @@ void _CFXMLDocSetCharacterEncoding(_CFXMLDocPtr doc, const unsigned char* _Null
658658
xmlFree((xmlChar*)docPtr->encoding);
659659
}
660660

661-
docPtr->encoding = encoding;
661+
docPtr->encoding = xmlStrdup(encoding);
662662
}
663663

664664
CF_RETURNS_RETAINED CFStringRef _CFXMLDocVersion(_CFXMLDocPtr doc) {

TestFoundation/TestNSXMLDocument.swift

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ class TestNSXMLDocument : XCTestCase {
4040
// ("test_validation_failure", test_validation_failure),
4141
("test_dtd", test_dtd),
4242
("test_documentWithDTD", test_documentWithDTD),
43-
("test_dtd_attributes", test_dtd_attributes)
43+
("test_dtd_attributes", test_dtd_attributes),
44+
("test_documentWithEncodingSetDoesntCrash", test_documentWithEncodingSetDoesntCrash)
4445
]
4546
#else // On Linux, currently the tests that rely on NSError are segfaulting in swift_dynamicCast
4647
return [
@@ -60,7 +61,8 @@ class TestNSXMLDocument : XCTestCase {
6061
// ("test_validation_failure", test_validation_failure),
6162
("test_dtd", test_dtd),
6263
// ("test_documentWithDTD", test_documentWithDTD),
63-
("test_dtd_attributes", test_dtd_attributes)
64+
("test_dtd_attributes", test_dtd_attributes),
65+
("test_documentWithEncodingSetDoesntCrash", test_documentWithEncodingSetDoesntCrash)
6466
]
6567
#endif
6668
}
@@ -406,4 +408,16 @@ class TestNSXMLDocument : XCTestCase {
406408
let attrDecl = dtd.attributeDeclaration(forName: "print", elementName: "foo")!
407409
XCTAssert(attrDecl.dtdKind == .enumerationAttribute)
408410
}
411+
412+
func test_documentWithEncodingSetDoesntCrash() throws {
413+
weak var weakDoc: XMLDocument? = nil
414+
func makeSureDocumentIsAllocatedAndFreed() {
415+
let doc = XMLDocument(rootElement: XMLElement(name: "test"))
416+
doc.characterEncoding = "UTF-8"
417+
weakDoc = doc
418+
}
419+
makeSureDocumentIsAllocatedAndFreed()
420+
XCTAssertNil(weakDoc, "document not freed even through it should have")
421+
}
422+
409423
}

0 commit comments

Comments
 (0)