Skip to content

Commit 482a27a

Browse files
authored
Merge pull request #2316 from YOCKOW/sr-10776
SR-10776: Fix runtime crash of calling XMLDocument's `var name: String?`
2 parents aa080d1 + ae3542d commit 482a27a

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

CoreFoundation/Parsing.subproj/CFXMLInterface.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,7 @@ static inline xmlChar* _getQName(xmlNodePtr node) {
495495
const xmlChar* ncname = node->name;
496496

497497
switch (node->type) {
498+
case XML_DOCUMENT_NODE:
498499
case XML_NOTATION_NODE:
499500
case XML_DTD_NODE:
500501
case XML_ELEMENT_DECL:

Foundation/XMLNode.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,9 +330,13 @@ open class XMLNode: NSObject, NSCopying {
330330
return returned == nil ? nil : unsafeBitCast(returned!, to: NSString.self) as String
331331
}
332332
set {
333-
if case .namespace = kind {
333+
switch kind {
334+
case .document:
335+
// As with Darwin, ignore the name when the node is document.
336+
break
337+
case .namespace:
334338
_CFXMLNamespaceSetPrefix(_xmlNode, newValue, Int64(newValue?.utf8.count ?? 0))
335-
} else {
339+
default:
336340
if let newName = newValue {
337341
_CFXMLNodeSetName(_xmlNode, newName)
338342
} else {

TestFoundation/TestXMLDocument.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class TestXMLDocument : LoopbackServerTest {
4040
("test_optionPreserveAll", test_optionPreserveAll),
4141
("test_rootElementRetainsDocument", test_rootElementRetainsDocument),
4242
("test_nodeKinds", test_nodeKinds),
43+
("test_sr10776_documentName", test_sr10776_documentName),
4344
]
4445
}
4546

@@ -625,7 +626,7 @@ class TestXMLDocument : LoopbackServerTest {
625626

626627
XCTAssertEqual(try? test(), "plans")
627628
}
628-
629+
629630
func test_nodeKinds() {
630631
XCTAssertEqual(XMLDocument(rootElement: nil).kind, .document)
631632
XCTAssertEqual(XMLElement(name: "prefix:localName").kind, .element)
@@ -640,6 +641,14 @@ class TestXMLDocument : LoopbackServerTest {
640641
XCTAssertEqual(XMLDTDNode(xmlString: "<!ELEMENT E EMPTY>")?.kind, .elementDeclaration)
641642
XCTAssertEqual(XMLDTDNode(xmlString: #"<!NOTATION f SYSTEM "F">"#)?.kind, .notationDeclaration)
642643
}
644+
645+
func test_sr10776_documentName() {
646+
let doc = XMLDocument(rootElement: nil)
647+
XCTAssertNil(doc.name)
648+
649+
doc.name = "name"
650+
XCTAssertNil(doc.name) // `name` of XMLDocument is always nil.
651+
}
643652
}
644653

645654
fileprivate extension XMLNode {

0 commit comments

Comments
 (0)