Эх сурвалжийг харах

Fix dangling pointer warnings

David Wu 5 жил өмнө
parent
commit
b0d94e55c6
1 өөрчлөгдсөн 14 нэмэгдсэн , 14 устгасан
  1. 14 14
      Sources/Base32/Base32.swift

+ 14 - 14
Sources/Base32/Base32.swift

@@ -44,13 +44,13 @@ public func base32HexEncode(_ data: Data) -> String {
 
 public func base32DecodeToData(_ string: String) -> Data? {
     return base32decode(string, alphabetDecodeTable).flatMap {
-        Data(bytes: UnsafePointer<UInt8>($0), count: $0.count)
+        $0.withUnsafeBufferPointer(Data.init(buffer:))
     }
 }
 
 public func base32HexDecodeToData(_ string: String) -> Data? {
     return base32decode(string, extendedHexAlphabetDecodeTable).flatMap {
-        Data(bytes: UnsafePointer<UInt8>($0), count: $0.count)
+        $0.withUnsafeBufferPointer(Data.init(buffer:))
     }
 }
 
@@ -329,8 +329,8 @@ private func base32decode(_ string: String, _ table: [UInt8]) -> [UInt8]? {
         (data: UnsafeBufferPointer<CChar>) -> [UInt8] in
         var encoded = data.baseAddress!
         
-        let result = Array<UInt8>(repeating: 0, count: dataSize)
-        var decoded = UnsafeMutablePointer<UInt8>(mutating: result)
+        var result = Array<UInt8>(repeating: 0, count: dataSize)
+        var decodedOffset = 0
         
         // decode regular blocks
         var value0, value1, value2, value3, value4, value5, value6, value7: UInt8
@@ -345,14 +345,14 @@ private func base32decode(_ string: String, _ table: [UInt8]) -> [UInt8]? {
             value6 = table[Int(encoded[6])]
             value7 = table[Int(encoded[7])]
             
-            decoded[0] = value0 << 3 | value1 >> 2
-            decoded[1] = value1 << 6 | value2 << 1 | value3 >> 4
-            decoded[2] = value3 << 4 | value4 >> 1
-            decoded[3] = value4 << 7 | value5 << 2 | value6 >> 3
-            decoded[4] = value6 << 5 | value7
+            result[decodedOffset]     = value0 << 3 | value1 >> 2
+            result[decodedOffset + 1] = value1 << 6 | value2 << 1 | value3 >> 4
+            result[decodedOffset + 2] = value3 << 4 | value4 >> 1
+            result[decodedOffset + 3] = value4 << 7 | value5 << 2 | value6 >> 3
+            result[decodedOffset + 4] = value6 << 5 | value7
             
             remainEncodedLength -= 8
-            decoded = decoded.advanced(by: 5)
+            decodedOffset += 5
             encoded = encoded.advanced(by: 8)
         }
         
@@ -377,16 +377,16 @@ private func base32decode(_ string: String, _ table: [UInt8]) -> [UInt8]? {
         }
         switch remainEncodedLength {
         case 7:
-            decoded[3] = value4 << 7 | value5 << 2 | value6 >> 3
+            result[decodedOffset + 3] = value4 << 7 | value5 << 2 | value6 >> 3
             fallthrough
         case 5:
-            decoded[2] = value3 << 4 | value4 >> 1
+            result[decodedOffset + 2] = value3 << 4 | value4 >> 1
             fallthrough
         case 4:
-            decoded[1] = value1 << 6 | value2 << 1 | value3 >> 4
+            result[decodedOffset + 1] = value1 << 6 | value2 << 1 | value3 >> 4
             fallthrough
         case 2:
-            decoded[0] = value0 << 3 | value1 >> 2
+            result[decodedOffset]     = value0 << 3 | value1 >> 2
         default: break
         }