Skip to content

XMLParser malformed XML handling differs between macOS and Linux #5281

@mrkprds

Description

@mrkprds

Malformed XML (unclosed tags):

  • macOS: parse() returns false, parseErrorOccurred called once
  • Linux: parse() returns true, parseErrorOccurred called multiple times

Reproduction

macOS

import Foundation
#if canImport(FoundationXML)
import FoundationXML
#endif

class MalformedDelegate: NSObject, XMLParserDelegate {
    var parseError: Error?
    var elementCount = 0

    func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String] = [:]) {
        elementCount += 1
        print("  Started element: \(elementName)")
    }

    func parser(_ parser: XMLParser, parseErrorOccurred parseError: Error) {
        self.parseError = parseError
        print("  Parse error detected: \(parseError.localizedDescription)")
    }
}

let malformedXML = """
<?xml version="1.0" encoding="utf-8"?>
<xliff version="1.2">
    <file source-language="en">
        <body>
            <trans-unit id="key">
                <source>Value
            </trans-unit>
        </body>
    </file>
</xliff>
"""

let delegate = MalformedDelegate()
let parser = XMLParser(data: malformedXML.data(using: .utf8)!)
parser.delegate = delegate
let success = parser.parse()

print("\nParse returned: \(success)")
print("Elements started: \(delegate.elementCount)")
print("Parse error: \(delegate.parseError?.localizedDescription ?? "none")")

Linux

import Foundation
#if canImport(FoundationXML)
import FoundationXML
#endif

class MalformedDelegate: XMLParserDelegate {
    var parseError: Error?
    var elementCount = 0

    func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String] = [:]) {
        elementCount += 1
        print("  Started element: \(elementName)")
    }

    func parser(_ parser: XMLParser, parseErrorOccurred parseError: Error) {
        self.parseError = parseError
        print("  Parse error detected: \(parseError.localizedDescription)")
    }
}

let malformedXML = """
<?xml version="1.0" encoding="utf-8"?>
<xliff version="1.2">
    <file source-language="en">
        <body>
            <trans-unit id="key">
                <source>Value
            </trans-unit>
        </body>
    </file>
</xliff>
"""

let delegate = MalformedDelegate()
let parser = XMLParser(data: malformedXML.data(using: .utf8)!)
parser.delegate = delegate
let success = parser.parse()

print("\nParse returned: \(success)")
print("Elements started: \(delegate.elementCount)")
print("Parse error: \(delegate.parseError?.localizedDescription ?? "none")")

macOS Output

  Started element: xliff
  Started element: file
  Started element: body
  Started element: trans-unit
  Started element: source
  Parse error detected: The operation couldn't be completed. (NSXMLParserErrorDomain error 76.)

Parse returned: false
Elements started: 5
Parse error: The operation couldn't be completed. (NSXMLParserErrorDomain error 76.)

Linux Output

  Started element: xliff
  Started element: file
  Started element: body
  Started element: trans-unit
  Started element: source
  Parse error detected: The operation could not be completed. (NSXMLParserErrorDomain error 76.)
  Parse error detected: The operation could not be completed. (NSXMLParserErrorDomain error 76.)
  Parse error detected: The operation could not be completed. (NSXMLParserErrorDomain error 76.)
  Parse error detected: The operation could not be completed. (NSXMLParserErrorDomain error 76.)
  Parse error detected: The operation could not be completed. (NSXMLParserErrorDomain error 5.)

Parse returned: true
Elements started: 5
Parse error: The operation could not be completed. (NSXMLParserErrorDomain error 5.)

On Linux, parseErrorOccurred is called multiple times but parse() still returns true.


Environment

  • Swift: 6.2 (swift-6.2-RELEASE)
  • macOS: Sequoia 15.1 (Build 26.0.1)
  • Linux: Ubuntu 24.04.3 LTS (Docker: swift:6.2)

Related Issues

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions