From 740c3eeb31eaf49ff9e3f0ae87466b009a75f518 Mon Sep 17 00:00:00 2001 From: Scott Pfeil Date: Fri, 20 Sep 2024 12:45:11 -0400 Subject: [PATCH 1/2] Digital PCT265 defect MVAPCT-271 - Ensure the app doesn't crash on invalid color hex. --- MVMCoreUI/CustomPrimitives/Color.swift | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/MVMCoreUI/CustomPrimitives/Color.swift b/MVMCoreUI/CustomPrimitives/Color.swift index 475e8aea..c1fd4b33 100644 --- a/MVMCoreUI/CustomPrimitives/Color.swift +++ b/MVMCoreUI/CustomPrimitives/Color.swift @@ -44,6 +44,7 @@ public final class Color: Codable { enum ColorError: Error { case badName(reason: String) + case invalidHex(reason: String) } //-------------------------------------------------- @@ -53,21 +54,21 @@ public final class Color: Codable { public init(uiColor: UIColor) { self.uiColor = uiColor hex = UIColor.hexString(for: uiColor) ?? "" - determineRGBA() + try? determineRGBA() } public init?(uiColor: UIColor?) { guard let uiColor = uiColor else { return nil } self.uiColor = uiColor hex = UIColor.hexString(for: uiColor) ?? "" - determineRGBA() + try? determineRGBA() } init?(name: String) { guard let colorTuple = UIColor.names[name] else { return nil } self.uiColor = colorTuple.uiColor self.hex = colorTuple.hex - determineRGBA() + try? determineRGBA() } public init?(colorString: String) throws { @@ -75,7 +76,7 @@ public final class Color: Codable { self.uiColor = components.color hex = components.hex name = components.name ?? "" - determineRGBA() + try determineRGBA() } //-------------------------------------------------- @@ -99,7 +100,7 @@ public final class Color: Codable { name = components.name ?? "" } - determineRGBA() + try determineRGBA() } public func encode(to encoder: Encoder) throws { @@ -135,9 +136,12 @@ public final class Color: Codable { return CGFloat(Int(hex[start.. 6 else { + throw ColorError.invalidHex(reason: "Invalid hex: \(hex)") + } let redStart = hex.startIndex let redEnd = hex.index(redStart, offsetBy: 2) From 8a7c07ecf2bcf7be2c33d6ee61b13f3276c938ef Mon Sep 17 00:00:00 2001 From: Scott Pfeil Date: Fri, 20 Sep 2024 14:04:44 -0400 Subject: [PATCH 2/2] Digital PCT265 defect MVAPCT-271 - greater than or equal to length fix --- MVMCoreUI/CustomPrimitives/Color.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MVMCoreUI/CustomPrimitives/Color.swift b/MVMCoreUI/CustomPrimitives/Color.swift index c1fd4b33..1c2240da 100644 --- a/MVMCoreUI/CustomPrimitives/Color.swift +++ b/MVMCoreUI/CustomPrimitives/Color.swift @@ -139,7 +139,7 @@ public final class Color: Codable { private func determineRGBA() throws { guard !hex.isEmpty else { return } - guard hex.count > 6 else { + guard hex.count >= 6 else { throw ColorError.invalidHex(reason: "Invalid hex: \(hex)") }