swift-png icon indicating copy to clipboard operation
swift-png copied to clipboard

Leaving Array's buffer uninitialised is breach of contract

Open valeriyvan opened this issue 2 years ago • 1 comments

https://github.com/kelvin13/swift-png/blob/075dfb248ae327822635370e9d4f94a5d3fe93b2/sources/png/decoder.swift#L289

https://developer.apple.com/documentation/swift/array/init(unsafeuninitializedcapacity:initializingwith:):

Discussion

Inside the closure, set the initializedCount parameter to the number of elements that are initialized by the closure. The memory in the range buffer[0..<initializedCount] must be initialized at the end of the closure’s execution, and the memory in the range buffer[initializedCount...] must be uninitialized. This postcondition must hold even if the initializer closure throws an error.

valeriyvan avatar Nov 30 '22 10:11 valeriyvan

this is only a breach-of-contract in the generic case; because UInt8 is a trivial type, it should be perfectly valid to leave the memory uninitialized. i am reluctant to have PNG.Data.Rectangular be backed by a managed buffer or some sort because there are a lot of benefits to having it backed by a normal Array

tayloraswift avatar Dec 06 '22 22:12 tayloraswift