Skip to content

Fixed a crash that happens when you attempt to get the name of an XML document #2466

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

Closed
wants to merge 2 commits into from
Closed
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
3 changes: 2 additions & 1 deletion CoreFoundation/Parsing.subproj/CFXMLInterface.c
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ void _CFXMLNodeSetPrivateData(_CFXMLNodePtr node, void* data) {
if (!node) {
return;
}

((xmlNodePtr)node)->_private = data;
}

Expand All @@ -459,6 +459,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
10 changes: 6 additions & 4 deletions CoreFoundation/String.subproj/CFString.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down
8 changes: 6 additions & 2 deletions Foundation/XMLNode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 8 additions & 0 deletions TestFoundation/TestXMLDocument.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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),
]
}

Expand Down Expand Up @@ -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 {
Expand Down