This repository was archived by the owner on Oct 4, 2024. It is now read-only.
Fixed Warning: Initialization of 'UnsafePointer<CUnsignedChar>' results in a dangling pointer#48
Open
GayleDDS wants to merge 1 commit into
Open
Conversation
…'UnsafePointer<UInt8>') results in a dangling pointer This is a new warning added to Xcode 11.4 (See release notes snippet below) See also: https://stackoverflow.com/questions/60861711/initialization-of-unsafemutablerawpointer-results-in-a-dangling-pointer Xcode 11.4 Release Notes The compiler will now emit a warning when attempting to pass a temporary pointer argument produced from an array, string, or inout argument to a parameter which is known to escape it. This includes the various initializers for the UnsafePointer/UnsafeBufferPointer family of types, as well as memberwise initializers. For example, the compiler will emit warnings compiling the following code: struct S { var ptr: UnsafePointer<Int8> } func foo() { var i: Int8 = 0 let ptr = UnsafePointer(&i) // warning: initialization of 'UnsafePointer<Int8>' results in a // dangling pointer let s1 = S(ptr: [1, 2, 3]) // warning: passing '[Int8]' to parameter, but argument 'ptr' should be a // pointer that outlives the call to 'init(ptr:)' let s2 = S(ptr: "hello") // warning: passing 'String' to parameter, but argument 'ptr' should be a // pointer that outlives the call to 'init(ptr:)' } All 3 of the above examples are unsound because each argument produces a temporary pointer only valid for the duration of the call to which they are passed. Therefore the returned value in each case references a dangling pointer. (SR-2790) https://bugs.swift.org/browse/SR-2790 (31232450)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixed Warning: Initialization of 'UnsafePointer' (aka 'UnsafePointer') results in a dangling pointer
This is a new warning added to Xcode 11.4 (See release notes snippet below)
See also: https://stackoverflow.com/questions/60861711/initialization-of-unsafemutablerawpointer-results-in-a-dangling-pointer
Xcode 11.4 Release Notes
The compiler will now emit a warning when attempting to pass a temporary pointer argument produced from an array, string, or inout argument to a parameter which is known to escape it. This includes the various initializers for the UnsafePointer/UnsafeBufferPointer family of types, as well as memberwise initializers. For example, the compiler will emit warnings compiling the following code:
struct S {
var ptr: UnsafePointer
}
func foo() {
var i: Int8 = 0
let ptr = UnsafePointer(&i)
// warning: initialization of 'UnsafePointer' results in a
// dangling pointer
}
All 3 of the above examples are unsound because each argument produces a temporary pointer only valid for the duration of the call to which they are passed. Therefore the returned value in each case references a dangling pointer. (SR-2790) https://bugs.swift.org/browse/SR-2790 (31232450)