Skip to content

fix two non-owned strings being freed #1032

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CoreFoundation/Parsing.subproj/CFXMLInterface.c
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ void _CFXMLNodeSetURI(_CFXMLNodePtr node, const unsigned char* URI) {
if (doc->URL) {
xmlFree((xmlChar*)doc->URL);
}
doc->URL = URI;
doc->URL = xmlStrdup(URI);
}
break;

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

docPtr->encoding = encoding;
docPtr->encoding = xmlStrdup(encoding);
}

CF_RETURNS_RETAINED CFStringRef _CFXMLDocVersion(_CFXMLDocPtr doc) {
Expand Down
18 changes: 16 additions & 2 deletions TestFoundation/TestNSXMLDocument.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ class TestNSXMLDocument : XCTestCase {
// ("test_validation_failure", test_validation_failure),
("test_dtd", test_dtd),
("test_documentWithDTD", test_documentWithDTD),
("test_dtd_attributes", test_dtd_attributes)
("test_dtd_attributes", test_dtd_attributes),
("test_documentWithEncodingSetDoesntCrash", test_documentWithEncodingSetDoesntCrash)
]
#else // On Linux, currently the tests that rely on NSError are segfaulting in swift_dynamicCast
return [
Expand All @@ -60,7 +61,8 @@ class TestNSXMLDocument : XCTestCase {
// ("test_validation_failure", test_validation_failure),
("test_dtd", test_dtd),
// ("test_documentWithDTD", test_documentWithDTD),
("test_dtd_attributes", test_dtd_attributes)
("test_dtd_attributes", test_dtd_attributes),
("test_documentWithEncodingSetDoesntCrash", test_documentWithEncodingSetDoesntCrash)
]
#endif
}
Expand Down Expand Up @@ -406,4 +408,16 @@ class TestNSXMLDocument : XCTestCase {
let attrDecl = dtd.attributeDeclaration(forName: "print", elementName: "foo")!
XCTAssert(attrDecl.dtdKind == .enumerationAttribute)
}

func test_documentWithEncodingSetDoesntCrash() throws {
weak var weakDoc: XMLDocument? = nil
func makeSureDocumentIsAllocatedAndFreed() {
let doc = XMLDocument(rootElement: XMLElement(name: "test"))
doc.characterEncoding = "UTF-8"
weakDoc = doc
}
makeSureDocumentIsAllocatedAndFreed()
XCTAssertNil(weakDoc, "document not freed even through it should have")
}

}