Error: EXC_BAD_ACCESS on string conversion to data

I am making this call deepHash and it always only fails on the second string to data conversion called "blob". Never on the "list". But they have identical code. What is the issue?

func deepHash(data: [Data]) -> Data {
    if data.count > 1 {
        let tag = concatData(data: [
            "list".data(using: .utf8)!, // never crashes here
            String(data.count).data(using: .utf8)!
        ])
        
        return deepHashChunks(chunks: data, acc: SHA384.hash(data: tag).data)
    }
    
    let tag = concatData(data: [
        "blob".data(using: .utf8)!, // crashes here every time
        data.first!.count.description.data(using: .utf8)!
    ])
    
    let taggedHash = concatData(data: [
        SHA384.hash(data: tag).data,
        SHA384.hash(data: data.first!).data
    ])
    
    return SHA384.hash(data: taggedHash).data
}

The following works without issue:

import Foundation
print("blob".data(using: .utf8)!)

I would surmise the error lies in concatData, but I can't see the code. Can you post a self-contained example that reproduces the error?

Maybe it crashes on the next line where you do data.first! and data happened to be empty?

That can't be the case because trying to force-unwrap a nil value produces a specific error message indicating the value was nil, not "EXC_BAD_ACCESS".

Terms of Service

Privacy Policy

Cookie Policy