Skip to content

Commit 9d62092

Browse files
committed
The endDocument callback was never called.
Fixed by signaling the final ("terminal") chunk to SAX.
1 parent af8f0c2 commit 9d62092

File tree

3 files changed

+10
-7
lines changed

3 files changed

+10
-7
lines changed

CoreFoundation/Parsing.subproj/CFXMLInterface.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
// This source file is part of the Swift.org open source project
22
//
3-
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
3+
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
44
// Licensed under Apache License v2.0 with Runtime Library Exception
55
//
66
// See http://swift.org/LICENSE.txt for license information
77
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
88
//
99

1010
/* CFXMLInterface.c
11-
Copyright (c) 2015 Apple Inc. and the Swift project authors
11+
Copyright (c) 2020 Apple Inc. and the Swift project authors
1212
*/
1313

1414
#include <CoreFoundation/CFRuntime.h>
@@ -210,7 +210,8 @@ void _CFXMLInterfaceCtxtUseOptions(_CFXMLInterfaceParserContext ctx, CFIndex opt
210210
}
211211

212212
int _CFXMLInterfaceParseChunk(_CFXMLInterfaceParserContext ctxt, const char *chunk, int size, int terminate) {
213-
return xmlParseChunk(ctxt, chunk, size, terminate);
213+
int ret = xmlParseChunk(ctxt, chunk, size, terminate);
214+
return ret == XML_ERR_DOCUMENT_END && terminate ? XML_ERR_OK : ret;
214215
}
215216

216217
void _CFXMLInterfaceStopParser(_CFXMLInterfaceParserContext ctx) {

Sources/FoundationXML/XMLParser.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -486,8 +486,9 @@ open class XMLParser : NSObject {
486486
return false
487487
}
488488

489-
internal func parseData(_ data: Data) -> Bool {
489+
internal func parseData(_ data: Data, lastChunkOfData: Bool = false) -> Bool {
490490
_CFXMLInterfaceSetStructuredErrorFunc(interface, _structuredErrorFunc)
491+
defer { _CFXMLInterfaceSetStructuredErrorFunc(interface, nil) }
491492

492493
let handler: _CFXMLInterfaceSAXHandler? = (delegate != nil ? _handler : nil)
493494
let unparsedData: Data
@@ -538,11 +539,10 @@ open class XMLParser : NSObject {
538539

539540
let parseResult = unparsedData.withUnsafeBytes { (rawBuffer: UnsafeRawBufferPointer) -> Int32 in
540541
let bytes = rawBuffer.baseAddress!.assumingMemoryBound(to: CChar.self)
541-
return _CFXMLInterfaceParseChunk(_parserContext, bytes, Int32(unparsedData.count), 0)
542+
return _CFXMLInterfaceParseChunk(_parserContext, bytes, Int32(unparsedData.count), lastChunkOfData ? 1 : 0)
542543
}
543544

544545
let result = _handleParseResult(parseResult)
545-
_CFXMLInterfaceSetStructuredErrorFunc(interface, nil)
546546
return result
547547
}
548548

@@ -560,6 +560,7 @@ open class XMLParser : NSObject {
560560
let data = Data(bytesNoCopy: buffer, count: len, deallocator: .none)
561561
result = parseData(data)
562562
case 0:
563+
result = parseData(Data(), lastChunkOfData: true)
563564
break parseLoop
564565
default: // See SR-13516, should be `case ..<0:`
565566
result = false
@@ -582,7 +583,7 @@ open class XMLParser : NSObject {
582583
if _stream != nil {
583584
return parseFrom(_stream!)
584585
} else if _data != nil {
585-
return parseData(_data!)
586+
return parseData(_data!, lastChunkOfData: true)
586587
}
587588

588589
return false

Tests/Foundation/Tests/TestXMLParser.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ class TestXMLParser : XCTestCase {
9595
.foundCharacters("bar"),
9696
.didEndElement("foo", uri, namespaces ? "foo" : nil),
9797
.didEndElement("test", uri, namespaces ? "test" : nil),
98+
.endDocument,
9899
]
99100
}
100101

0 commit comments

Comments
 (0)