From a6f628c98e2822d3b1ae87b0fc2846becaf4935a Mon Sep 17 00:00:00 2001 From: pi1024e Date: Sun, 4 Aug 2019 13:05:28 -0400 Subject: [PATCH 1/2] Fix XMLDocument bug causing runtime to crash when name is called --- CoreFoundation/Parsing.subproj/CFXMLInterface.c | 2 +- CoreFoundation/String.subproj/CFString.c | 10 ++++++---- Foundation/XMLNode.swift | 8 ++++++-- TestFoundation/TestXMLDocument.swift | 8 ++++++++ 4 files changed, 21 insertions(+), 7 deletions(-) diff --git a/CoreFoundation/Parsing.subproj/CFXMLInterface.c b/CoreFoundation/Parsing.subproj/CFXMLInterface.c index c21e354c9f..8294e2d5a2 100644 --- a/CoreFoundation/Parsing.subproj/CFXMLInterface.c +++ b/CoreFoundation/Parsing.subproj/CFXMLInterface.c @@ -435,7 +435,6 @@ void _CFXMLNodeSetPrivateData(_CFXMLNodePtr node, void* data) { if (!node) { return; } - ((xmlNodePtr)node)->_private = data; } @@ -459,6 +458,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: diff --git a/CoreFoundation/String.subproj/CFString.c b/CoreFoundation/String.subproj/CFString.c index 8ec33fee79..bab1ddafdb 100644 --- a/CoreFoundation/String.subproj/CFString.c +++ b/CoreFoundation/String.subproj/CFString.c @@ -1468,7 +1468,7 @@ CF_PRIVATE CFStringRef __CFStringCreateImmutableFunnel3( if (noCopy) { size = sizeof(void *); // Pointer to the buffer - if ((0) || (contentsDeallocator != alloc && contentsDeallocator != kCFAllocatorNull)) { + if (contentsDeallocator != alloc && contentsDeallocator != kCFAllocatorNull) { size += sizeof(void *); // The contentsDeallocator } if (!hasLengthByte) size += sizeof(CFIndex); // Explicit length @@ -1480,12 +1480,14 @@ CF_PRIVATE CFStringRef __CFStringCreateImmutableFunnel3( useInlineData = true; size = numBytes; - if (hasLengthByte || (encoding != kCFStringEncodingUnicode && __CFCanUseLengthByte(numBytes))) { + if (hasLengthByte) { useLengthByte = true; - if (!hasLengthByte) size += 1; + } else if (encoding != kCFStringEncodingUnicode && __CFCanUseLengthByte(numBytes)) { + useLengthByte = true; + size += 1; } else { size += sizeof(CFIndex); // Explicit length - } + } if (hasNullByte || encoding != kCFStringEncodingUnicode) { useNullByte = true; size += 1; diff --git a/Foundation/XMLNode.swift b/Foundation/XMLNode.swift index cad0ced828..b885388983 100644 --- a/Foundation/XMLNode.swift +++ b/Foundation/XMLNode.swift @@ -312,9 +312,13 @@ open class XMLNode: NSObject, NSCopying { return _CFXMLNodeCopyName(_xmlNode)?._swiftObject } 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 { diff --git a/TestFoundation/TestXMLDocument.swift b/TestFoundation/TestXMLDocument.swift index 41dd38c743..6437960ef4 100644 --- a/TestFoundation/TestXMLDocument.swift +++ b/TestFoundation/TestXMLDocument.swift @@ -35,6 +35,7 @@ class TestXMLDocument : LoopbackServerTest { ("test_removeNamespace", test_removeNamespace), ("test_optionPreserveAll", test_optionPreserveAll), ("test_rootElementRetainsDocument", test_rootElementRetainsDocument), + ("test_sr10776_documentName", test_SR10776_documentName), ] } @@ -544,6 +545,13 @@ class TestXMLDocument : LoopbackServerTest { XCTAssertEqual(try? test(), "plans") } + 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 { From e3936265d2924b82606aade5ce13ded8aa89a79d Mon Sep 17 00:00:00 2001 From: pi1024e <49824824+pi1024e@users.noreply.github.com> Date: Wed, 14 Aug 2019 09:34:42 -0400 Subject: [PATCH 2/2] Update CFXMLInterface.c --- CoreFoundation/Parsing.subproj/CFXMLInterface.c | 1 + 1 file changed, 1 insertion(+) diff --git a/CoreFoundation/Parsing.subproj/CFXMLInterface.c b/CoreFoundation/Parsing.subproj/CFXMLInterface.c index 8294e2d5a2..2f609f6296 100644 --- a/CoreFoundation/Parsing.subproj/CFXMLInterface.c +++ b/CoreFoundation/Parsing.subproj/CFXMLInterface.c @@ -435,6 +435,7 @@ void _CFXMLNodeSetPrivateData(_CFXMLNodePtr node, void* data) { if (!node) { return; } + ((xmlNodePtr)node)->_private = data; }