Skip to content

[SR-13439] Swift is memory unsafe when escaping a fully initialised self in a fallible initialiser that returns nil. #55880

@weissi

Description

@weissi
Previous ID SR-13439
Radar rdar://problem/67746791
Original Reporter @weissi
Type Bug
Status Resolved
Resolution Done

Attachment: Download

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, RunTimeCrash
Assignee @slavapestov
Priority Medium

md5: bd71160abded35bdcd864e042d815a3a

Issue Description:

Consider this program

import Dispatch

class Foo: CustomStringConvertible {
    var x: Optional<Int>

    init?(_ x: Int) {
        print("Foo init   \(x)")
        if x == 0 {
            return nil
        }
        self.x = x
        if x == 1 {
            DispatchQueue(label: "Foo").async {
                print(self)
            }
            return nil
        }
    }

    var description: String {
        return "I exist!"
    }

    deinit {
        print("Foo deinit \(self.x.debugDescription)")
    }
}

print(Foo(1).debugDescription)
sleep(1)

it prints for me

$ swiftc test.swift && ./test
Foo init   1
nil
Segmentation fault: 11
-SEGV-

in

$ swift -version
Apple Swift version 5.2.4 (swiftlang-1103.0.32.9 clang-1103.0.32.53)
Target: x86_64-apple-darwin19.6.0

Metadata

Metadata

Assignees

Labels

bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfcrashBug: A crash, i.e., an abnormal termination of softwarerun-time crashBug → crash: Swift code crashed during execution

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions