- 
                Notifications
    
You must be signed in to change notification settings  - Fork 1.2k
 
Open
Description
Malformed XML (unclosed tags):
- macOS: 
parse()returnsfalse,parseErrorOccurredcalled once - Linux: 
parse()returnstrue,parseErrorOccurredcalled 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
- [SR-11192] XML Parsing Error Inconsistency #3989 - XML Parsing Error Inconsistency (malformed XML handling)
 - [SR-2301] NSXMLParser not fully implemented #4342 - NSXMLParser not fully implemented
 
Metadata
Metadata
Assignees
Labels
No labels