Skip to content

Commit 94a9456

Browse files
authored
Fix some force unwraps and type conversion issues in OpenStepPlist (#1430)
1 parent 2d2e249 commit 94a9456

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

Sources/FoundationEssentials/PropertyList/OpenStepPlist.swift

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,13 @@ private func parseQuotedPlistString(_ pInfo: inout _ParseInfo, quote: UInt16) ->
238238
pInfo.err = OpenStepPlistError("Unterminated backslash sequence on line \(lineNumberStrings(pInfo))")
239239
return nil
240240
}
241-
242-
result!.unicodeScalars.append(UnicodeScalar(getSlashedChar(&pInfo))!)
241+
242+
guard let scalar = UnicodeScalar(getSlashedChar(&pInfo)) else {
243+
pInfo.err = OpenStepPlistError("Invalid character on line \(lineNumberStrings(pInfo))")
244+
return nil
245+
}
246+
247+
result!.unicodeScalars.append(scalar)
243248
mark = pInfo.curr
244249
} else {
245250
pInfo.advance()
@@ -406,7 +411,10 @@ private func getDataBytes(_ pInfo: inout _ParseInfo, bytes: UnsafeMutableBufferP
406411
return numBytesRead
407412
}
408413

409-
func fromHexDigit(ch: UInt8) -> UInt8? {
414+
func fromHexDigit(ch: UInt16) -> UInt8? {
415+
guard let ch = UInt8(exactly: ch) else {
416+
return nil
417+
}
410418
if isdigit(Int32(ch)) != 0 {
411419
return ch &- UInt8(ascii: "0")
412420
}
@@ -419,14 +427,14 @@ private func getDataBytes(_ pInfo: inout _ParseInfo, bytes: UnsafeMutableBufferP
419427
return nil
420428
}
421429

422-
if let first = fromHexDigit(ch: UInt8(ch1)) {
430+
if let first = fromHexDigit(ch: ch1) {
423431
pInfo.advance()
424432
if pInfo.isAtEnd {
425433
return -2 // Error: uneven number of hex digits
426434
}
427435

428436
let ch2 = pInfo.currChar
429-
guard let second = fromHexDigit(ch: UInt8(ch2)) else {
437+
guard let second = fromHexDigit(ch: ch2) else {
430438
return -2 // Error: uneven number of hex digits
431439
}
432440

0 commit comments

Comments
 (0)