From 6cbd8cbf159d628d18f0df626aa83fd950f11928 Mon Sep 17 00:00:00 2001 From: Morgan Pretty Date: Tue, 15 Oct 2024 13:58:01 +1100 Subject: [PATCH] Updated the method 'Setting' data manipulation to be safer Also resolves a warning about an unsafe pointer --- .../Database/Models/Setting.swift | 24 +++++-------------- 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/SessionUtilitiesKit/Database/Models/Setting.swift b/SessionUtilitiesKit/Database/Models/Setting.swift index 163fd01b8..a1ff2ce85 100644 --- a/SessionUtilitiesKit/Database/Models/Setting.swift +++ b/SessionUtilitiesKit/Database/Models/Setting.swift @@ -25,42 +25,30 @@ extension Setting { // MARK: - Numeric Setting fileprivate init?(key: String, value: T?) { - guard let value: T = value else { return nil } - - var targetValue: T = value + guard var value: T = value else { return nil } self.key = key - self.value = Data(bytes: &targetValue, count: MemoryLayout.size(ofValue: targetValue)) + self.value = withUnsafeBytes(of: &value) { Data($0) } } fileprivate func value(as type: T.Type) -> T? { - // Note: The 'assumingMemoryBound' is essentially going to try to convert - // the memory into the provided type so can result in invalid data being - // returned if the type is incorrect. But it does seem safer than the 'load' - // method which crashed under certain circumstances (an `Int` value of 0) return value.withUnsafeBytes { - $0.baseAddress?.assumingMemoryBound(to: T.self).pointee + $0.loadUnaligned(as: T.self) } } // MARK: - Bool Setting fileprivate init?(key: String, value: Bool?) { - guard let value: Bool = value else { return nil } - - var targetValue: Bool = value + guard var value: Bool = value else { return nil } self.key = key - self.value = Data(bytes: &targetValue, count: MemoryLayout.size(ofValue: targetValue)) + self.value = withUnsafeBytes(of: &value) { Data($0) } } public func unsafeValue(as type: Bool.Type) -> Bool? { - // Note: The 'assumingMemoryBound' is essentially going to try to convert - // the memory into the provided type so can result in invalid data being - // returned if the type is incorrect. But it does seem safer than the 'load' - // method which crashed under certain circumstances (an `Int` value of 0) return value.withUnsafeBytes { - $0.baseAddress?.assumingMemoryBound(to: Bool.self).pointee + $0.loadUnaligned(as: Bool.self) } }