tayloraswift / swift-png

decode, inspect, edit, and encode png images in pure swift

Home Page:https://swiftinit.org/docs/swift-png

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Leaving Array's buffer uninitialised is breach of contract

valeriyvan opened this issue · 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.

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