Skip to content

Commit 633caba

Browse files
committed
Avoid crashing when XPath queries return no results.
Check before dereferencing nodesetval because it may be NULL if an XPath query returns no results (but is a valid query). Fixes SR-4628.
1 parent 5a6f4c6 commit 633caba

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

CoreFoundation/Parsing.subproj/CFXMLInterface.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -885,8 +885,7 @@ CFArrayRef _CFXMLNodesForXPath(_CFXMLNodePtr node, const unsigned char* xpath) {
885885
xmlXPathObjectPtr evalResult = xmlXPathNodeEval(node, xpath, context);
886886

887887
xmlNodeSetPtr nodes = evalResult->nodesetval;
888-
889-
int count = nodes->nodeNr;
888+
int count = nodes ? nodes->nodeNr : 0;
890889

891890
CFMutableArrayRef results = CFArrayCreateMutable(NULL, count, NULL);
892891
for (int i = 0; i < count; i++) {

TestFoundation/TestXMLDocument.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,10 @@ class TestXMLDocument : XCTestCase {
158158
XCTAssertEqual(nodes[0], bar1)
159159
XCTAssertEqual(nodes[1], bar2)
160160
XCTAssertEqual(nodes[2], bar3)
161-
161+
162+
let emptyResults = try! doc.nodes(forXPath: "/items/item/name[@type='alternate']/@value")
163+
XCTAssertEqual(emptyResults.count, 0)
164+
162165
let xmlString = """
163166
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
164167
<D:propfind xmlns:D="DAV:">

0 commit comments

Comments
 (0)