Warning about unsafe code
radianttap opened this issue · comments
static func hash(_ data: Data, crypto: Crypto) -> Data {
var buffer = Array<UInt8>(repeating: 0, count: Int(crypto.length))
crypto.method(data: (data as NSData).bytes, length: UInt32(data.count), buffer: &buffer)
return Data(bytes: UnsafePointer<UInt8>(buffer), count: buffer.count)
}
The last line results in warning:
Initialization of 'UnsafePointer<UInt8>' results in a dangling pointer
- Implicit argument conversion from '[UInt8]' to 'UnsafePointer<UInt8>' produces a pointer valid only for the duration of the call to 'init(_:)'
- the 'withUnsafeBufferPointer' method on Array in order to explicitly convert argument to buffer pointer valid for a defined scope
Anyone knows how to rewrite this..? It's beyond me.
I am not sure if this is the reason, but I suspect it could be. I just had a case where generated SHA256 turned out like this (on watchOS 6.2.8, Series 4):
2000000000000000000000000000000001d18b3095e5145a9a33e35d65b3a11a
Certainly does not look like a correct value :). Just a warning to people using the library.
@radianttap Hi, thanks for raising this. I'm currently revamping Arcane to support SPM and will look into this
@radianttap I see that we can simply use Data with UInt8 array
var buffer: [UInt8]
Data(buffer)
Yeah, that's a much nicer approach. Accidentally, I spent last two months deep down into Bluetooth raw data parsing, thus [UInt8]
is my best mate now ;)
@radianttap UInt8 must be hard to tame 😄 I'm closing this for now, been using Arcane 3.0.0 in production apps without any issues. Feel free to reopen or make new ticket if you find any problem