Skip to content

Commit 53ac1f1

Browse files
authored
fix: parse hex color string crash on iOS (#186)
asciiValue overflow
1 parent 69f58f7 commit 53ac1f1

File tree

1 file changed

+20
-24
lines changed

1 file changed

+20
-24
lines changed

ios/RCTImageMarker/Utils.swift

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -65,37 +65,33 @@ class Utils: NSObject {
6565
static func stringToInt(_ string: String) -> UInt32 {
6666
if string.count == 1 {
6767
let hexChar = string[string.startIndex]
68-
var intCh: UInt32 = 0
69-
if hexChar >= "0" && hexChar <= "9" {
70-
intCh = (UInt32(hexChar.asciiValue!) - 48) * 16 /* 0 的Ascll - 48 */
71-
} else if hexChar >= "A" && hexChar <= "F" {
72-
intCh = (UInt32(hexChar.asciiValue!) - 55) * 16 /* A 的Ascll - 65 */
73-
} else {
74-
intCh = (UInt32(hexChar.asciiValue!) - 87) * 16 /* a 的Ascll - 97 */
75-
}
68+
var intCh: UInt32 = getCharInt(hexChar)
7669
return intCh * 2
7770
} else {
7871
let hexChar1 = string[string.startIndex]
79-
var intCh1: UInt32 = 0
80-
if hexChar1 >= "0" && hexChar1 <= "9" {
81-
intCh1 = (UInt32(hexChar1.asciiValue!) - 48) * 16 /* 0 的Ascll - 48 */
82-
} else if hexChar1 >= "A" && hexChar1 <= "F" {
83-
intCh1 = (UInt32(hexChar1.asciiValue!) - 55) * 16 /* A 的Ascll - 65 */
84-
} else {
85-
intCh1 = (UInt32(hexChar1.asciiValue!) - 87) * 16 /* a 的Ascll - 97 */
86-
}
72+
var intCh1: UInt32 = getCharInt(hexChar1)
8773
let hexChar2 = string[string.index(after: string.startIndex)]
88-
var intCh2: UInt32 = 0
89-
if hexChar2 >= "0" && hexChar2 <= "9" {
90-
intCh2 = UInt32(hexChar2.asciiValue!) - 48 /* 0 的Ascll - 48 */
91-
} else if hexChar1 >= "A" && hexChar1 <= "F" {
92-
intCh2 = UInt32(hexChar2.asciiValue!) - 55 /* A 的Ascll - 65 */
93-
} else {
94-
intCh2 = UInt32(hexChar2.asciiValue!) - 87 /* a 的Ascll - 97 */
95-
}
74+
var intCh2: UInt32 = getCharInt(hexChar2)
9675
return intCh1 + intCh2
9776
}
9877
}
78+
79+
static func getCharInt(_ char: Character) -> UInt32 {
80+
var charInt: UInt32 = 0
81+
if let asciiValue = char.asciiValue {
82+
switch asciiValue {
83+
case 48...57: // '0'...'9'
84+
charInt = UInt32(asciiValue) - 48
85+
case 65...70: // 'A'...'F'
86+
charInt = UInt32(asciiValue) - 55
87+
case 97...102: // 'a'...'f'
88+
charInt = UInt32(asciiValue) - 87
89+
default:
90+
print("Invalid hex character")
91+
}
92+
}
93+
return charInt
94+
}
9995

10096
static func getShadowStyle(_ shadowStyle: [AnyHashable: Any]?) -> NSShadow? {
10197
if let shadowStyle = shadowStyle {

0 commit comments

Comments
 (0)