Skip to content

SR-10776: Fix runtime crash of calling XMLDocument's var name: String? #2316

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 2 commits into from
Aug 15, 2019
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
1 change: 1 addition & 0 deletions CoreFoundation/Parsing.subproj/CFXMLInterface.c
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,7 @@ static inline xmlChar* _getQName(xmlNodePtr node) {
const xmlChar* ncname = node->name;

switch (node->type) {
case XML_DOCUMENT_NODE:
case XML_NOTATION_NODE:
case XML_DTD_NODE:
case XML_ELEMENT_DECL:
Expand Down
8 changes: 6 additions & 2 deletions Foundation/XMLNode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -328,9 +328,13 @@ open class XMLNode: NSObject, NSCopying {
return returned == nil ? nil : unsafeBitCast(returned!, to: NSString.self) as String
}
set {
if case .namespace = kind {
switch kind {
case .document:
// As with Darwin, ignore the name when the node is document.
break
case .namespace:
_CFXMLNamespaceSetPrefix(_xmlNode, newValue, Int64(newValue?.utf8.count ?? 0))
} else {
default:
if let newName = newValue {
_CFXMLNodeSetName(_xmlNode, newName)
} else {
Expand Down
11 changes: 10 additions & 1 deletion TestFoundation/TestXMLDocument.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class TestXMLDocument : LoopbackServerTest {
("test_optionPreserveAll", test_optionPreserveAll),
("test_rootElementRetainsDocument", test_rootElementRetainsDocument),
("test_nodeKinds", test_nodeKinds),
("test_sr10776_documentName", test_sr10776_documentName),
]
}

Expand Down Expand Up @@ -625,7 +626,7 @@ class TestXMLDocument : LoopbackServerTest {

XCTAssertEqual(try? test(), "plans")
}

func test_nodeKinds() {
XCTAssertEqual(XMLDocument(rootElement: nil).kind, .document)
XCTAssertEqual(XMLElement(name: "prefix:localName").kind, .element)
Expand All @@ -640,6 +641,14 @@ class TestXMLDocument : LoopbackServerTest {
XCTAssertEqual(XMLDTDNode(xmlString: "<!ELEMENT E EMPTY>")?.kind, .elementDeclaration)
XCTAssertEqual(XMLDTDNode(xmlString: #"<!NOTATION f SYSTEM "F">"#)?.kind, .notationDeclaration)
}

func test_sr10776_documentName() {
let doc = XMLDocument(rootElement: nil)
XCTAssertNil(doc.name)

doc.name = "name"
XCTAssertNil(doc.name) // `name` of XMLDocument is always nil.
}
}

fileprivate extension XMLNode {
Expand Down