Skip to content

Commit 289b5a0

Browse files
authored
Merge pull request #1154 from ianpartridge/xmlparser-bitcast
2 parents 8615f82 + 1138ce4 commit 289b5a0

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

Foundation/XMLParser.swift

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,14 @@ extension XMLParser {
4242
}
4343

4444
private func UTF8STRING(_ bytes: UnsafePointer<UInt8>?) -> String? {
45-
guard let bytes = bytes else { return nil }
46-
// strlen operates on the wrong type, char*. We can't rebind the memory to a different type without knowing its length,
47-
// but since we know strlen is in libc, it's safe to directly bitcast the pointer without worrying about multiple accesses
48-
// of different types visible to the compiler.
49-
let len = strlen(unsafeBitCast(bytes, to: UnsafePointer<Int8>.self))
50-
let str = String._fromCodeUnitSequence(UTF8.self, input: UnsafeBufferPointer(start: bytes, count: Int(len)))
51-
return str
45+
guard let bytes = bytes else {
46+
return nil
47+
}
48+
if let (str, _) = String.decodeCString(bytes, as: UTF8.self,
49+
repairingInvalidCodeUnits: false) {
50+
return str
51+
}
52+
return nil
5253
}
5354

5455
internal func _NSXMLParserCurrentParser() -> _CFXMLInterface? {

0 commit comments

Comments
 (0)