Swift 应用程序在 SwiftCrypto 库中的 Array.reserveCapacity 上崩溃

Swift app crashing on Array.reserveCapacity in SwiftCrypto library

提问人:Federico Malagoni 提问时间:6/9/2023 更新时间:6/9/2023 访问量:30

问:

这是我无法修复的崩溃的堆栈跟踪。

libsystem_kernel.dylib
__pthread_kill + 8
12
libswiftCore.dylib
Array._reserveCapacityImpl(minimumCapacity:growForAppend:) + 280
13
Wallife
Array+Extension.swift - Line 21
Array.init(reserveCapacity:) + 21
14
Wallife
AES.swift - Line 518
AES.encrypt(_:) + 518
15
Wallife
<compiler-generated> - Line 16
protocol witness for Cipher.encrypt(_:) in conformance AES + 16
16
Wallife
Cipher.swift - Line 41
Cipher.encrypt(_:) + 41
17
Wallife
Encryptor.swift - Line 76
Encryptor.encrypt(data:) + 76
18
Wallife
TrackerRecord.swift - Line 101
TrackerRecord.encode(encryptor:) + 101
19
Wallife
TrackerBinarySerializer.swift - Line 25

崩溃:com.apple.root.background-qos

我从 Array+Extension 上的 SwiftCrypto 库 Crashlytics 得到了这个崩溃.swift - 第 21 行。

我认为这是引起的,但是一个未处理的错误,但我在此代码中找不到任何内容。

这是生成上述错误的嵌套方法的流。

func serialize(record: TrackerRecord) -> Data? {
    let recordToSave = record.copyWith(sessionId: sessionId, id: counter)
    do {
        let serializedData = try serializer.encode(recordToSave)
        return serializedData
    } catch {
        print(error)
    }
    return nil
}

func encode(_ record: TrackerRecord) throws -> Data {
    let data = try record.encode(encryptor: encryptor)
    return data
}


public func encode(encryptor: Encryptor? = nil) throws -> Data {
    var data = Data()
    ..
    // Encrypt
    if encryptor != nil {
        var encryptedData = Data()
        encryptedData.append(xData)
        ..
        encryptedData = try encryptor!.encrypt(data: encryptedData)
        
        data.append(encryptedData)
    } else {
        ..
    }
    
    return data
}



func encrypt(data: Data) throws -> Data {
    guard let aes = self.aes else {
        throw EncryptorError.notInitialized
    }
    do {
        let encryptedBytes = try aes.encrypt(data.bytes)
        let encryptedData = Data(encryptedBytes)
        return encryptedData
    } catch {
        throw EncryptorError.unableToEncrypt
    }
}


extension Cipher {
  public func encrypt(_ bytes: Array<UInt8>) throws -> Array<UInt8> {
    try self.encrypt(bytes.slice)
  }
}

extension AES: Cipher {
  @inlinable
  public func encrypt(_ bytes: ArraySlice<UInt8>) throws -> Array<UInt8> {
    let blockSize = self.blockMode.customBlockSize ?? AES.blockSize
    let chunks = bytes.batched(by: blockSize)

    var oneTimeCryptor = try makeEncryptor()
    var out = Array<UInt8>(reserveCapacity: bytes.count)
    for chunk in chunks {
      out += try oneTimeCryptor.update(withBytes: chunk, isLast: false)
    }
    // Padding may be added at the very end
    out += try oneTimeCryptor.finish()

    if self.blockMode.options.contains(.paddingRequired) && (out.count % AES.blockSize != 0) {
      throw Error.dataPaddingRequired
    }

    return out
  }
}

崩溃似乎在 AES 加密方法的 Array(reserveCapacity: bytes.count) 行中。

iOS Swift 错误处理

评论


答: 暂无答案