You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
SR-7849DataStorage's get(:) method should use load(fromByteOffset:as:)
SR-7850DataStorage's set(:) method should use storeBytes(of:toByteOffset:as:)
SR-7851 _DataStorage's enumerateBytes method should use bindMemory instead of assumingMemoryBound(to:)
Issue Description:
Currently the implementation (core libs foundation) of Data's withUnsafeBytes and withUnsafeMutableBytes methods uses assumingMemoryBound(to:) in order to obtain a typed pointer to the underlying memory – however this produces undefined behaviour if the type T is unrelated to the type that the memory is already bound to.
We should switch back to using bindMemory(to:) in order to avoid this undefined behaviour.
Switching to bindMemory was not a great solution anyway. It added a code motion barrier in one direction but not the other.
I added a note to SR-11087 to fix this the right way once we have stdlib/compiler support.
Although fixing the implementation doesn't change the fact that we don't want anyone using this Data API. The issue was that this Data API hides a fundamentally unsafe operation and masquerades as a safe operation. If the user happens to know the type of the buffer that was passed into Data, then they should explicitly state that by using the Unsafe API. We could also eventually provide a typed Data-like buffer that has unique ownership of its memory.
Additional Detail from JIRA
md5: 1b93fb05d5fdda8846f4a50088a5f7b1
relates to:
Issue Description:
Currently the implementation (core libs foundation) of
Data
'swithUnsafeBytes
andwithUnsafeMutableBytes
methods usesassumingMemoryBound(to:)
in order to obtain a typed pointer to the underlying memory – however this produces undefined behaviour if the typeT
is unrelated to the type that the memory is already bound to.We should switch back to using
bindMemory(to:)
in order to avoid this undefined behaviour.See https://forums.swift.org/t/how-to-use-data-withunsafebytes-in-a-well-defined-manner/12811 for more context.
The text was updated successfully, but these errors were encountered: