Skip to content
Open
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
2 changes: 1 addition & 1 deletion Sources/XMLCoder/Auxiliaries/XMLStackParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class XMLStackParser: NSObject {
errorContextLength: UInt,
shouldProcessNamespaces: Bool
) throws -> XMLCoderElement {
let xmlParser = XMLParser(data: data)
let xmlParser = XMLParser(stream: InputStream(data: data))
xmlParser.shouldProcessNamespaces = shouldProcessNamespaces
xmlParser.delegate = self

Expand Down
14 changes: 3 additions & 11 deletions Tests/XMLCoderTests/AdvancedFeatures/ErrorContextTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,10 @@ final class ErrorContextTest: XCTestCase {
at line 1, column 7:
`ah //>`
"""
let column10 = """
\(underlying.localizedDescription) \
at line 1, column 10:
`//>`
"""

#if os(Linux) && swift(<5.4)
// XML Parser returns a different column on Linux and iOS 16+
// https://bugs.swift.org/browse/SR-11192
#if os(Windows) || os(Linux)
// Input is fed to the parser as a stream, so the reported column
// is the position of the error rather than the end of the input.
XCTAssertEqual(ctx.debugDescription, column7)
#elseif os(Windows) || os(Linux)
XCTAssertEqual(ctx.debugDescription, column10)
#else
if #available(iOS 16.0, tvOS 16.0, macOS 13.0, *) {
XCTAssertEqual(ctx.debugDescription, column7)
Expand Down
26 changes: 26 additions & 0 deletions Tests/XMLCoderTests/Auxiliary/XMLStackParserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,30 @@ class XMLStackParserTests: XCTestCase {
XCTAssertEqual(root.elements[1].elements[0].stringValue, "escaped data: &lt;\r\n")
XCTAssertEqual(root.elements[2].stringValue, "\n")
}

// libxml2 rejects a single input chunk larger than 10 MB unless
// XML_PARSE_HUGE is set, so the parser must not receive all data at once.
func testDecodeDocumentLargerThan10MB() throws {
struct Catalog: Decodable {
let item: [Item]
}
struct Item: Decodable, Equatable {
let id: Int
let name: String
}

let count = 100
let name = String(repeating: "a", count: 110_000)
var xmlString = "<catalog>"
for i in 0..<count {
xmlString += "<item><id>\(i)</id><name>\(name)</name></item>"
}
xmlString += "</catalog>"
let xmlData = xmlString.data(using: .utf8)!
XCTAssertGreaterThan(xmlData.count, 10_000_000)

let catalog = try XMLDecoder().decode(Catalog.self, from: xmlData)
XCTAssertEqual(catalog.item.count, count)
XCTAssertEqual(catalog.item.last, Item(id: count - 1, name: name))
}
}