Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-10478] EXC_BAD_ACCESS of NSError via caught error.localizedDescription, when trying to match it to other error type. #52878

Closed
swift-ci opened this issue Apr 13, 2019 · 4 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself crash Bug: A crash, i.e., an abnormal termination of software run-time crash Bug → crash: Swift code crashed during execution

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-10478
Radar rdar://problem/49906841
Original Reporter fbeeper (JIRA User)
Type Bug
Status Resolved
Resolution Done
Environment

Running on Xcode Version 10.2 (10E125).

Failing on both a Swift 4.2 project and Swift 5.0 Playground.

Compiling with Optimization Level None [-Onone].

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

md5: 8669e5a47c8541304bb7e62da864518a

duplicates:

  • SR-9207 Crash casting NSError to URLError in Swift 4.x

Issue Description:

I ran into an unexpected EXC_BAD_ACCESS on an error I catch in my code. The crash happens when accessing localizedDescription, but only when I try to compare the error with some other error type before that.

There are many ways to code a working alternative, but this crash certainly looked interesting to me. Hope this report is somehow useful.

I reproduced my issue in this nonsensical but enough-to-proof-the-crash sample code:

struct MyObject: Codable {
    let id: Int
}

enum MyError: CustomNSError {
    case tomato
}

do {
    _ = try JSONDecoder().decode(MyObject.self, from: "akldnlknglfkng".data(using: .utf8)!)
}
catch let error as NSError {
    
    if let myError = error as? MyError {
        print("MyError: \(myError.localizedDescription)")
    }
    else {
        print(error.localizedDescription)
    }
}

As I mention above, the crash happens in the execution of localizedDescription. But, if I remove the if-statement trying to match the catched error to MyError it will not crash anymore.

I defined MyError to be a CustomNSError because mine was, but making it an Error causes the same issue.

While somehow similar to the issue described on SR-7493, I am having this issue running the project on Xcode, Debug config.

@belkadan
Copy link
Contributor

Doesn't seem to be a regression (i.e. the same crash is observed in 4.1), but definitely seems bad.

@swift-ci
Copy link
Collaborator Author

Comment by Ferran Poveda (JIRA)

Let me add that the following scenarios will not end up in the crash described above:

do {
    _ = try JSONDecoder().decode(MyObject.self, from: "akldnlknglfkng".data(using: .utf8)!)
}
catch let error as MyError {
    print("MyError: \(error.localizedDescription)")
}
catch {
    print(error.localizedDescription)
}
do {
    _ = try JSONDecoder().decode(MyObject.self, from: "akldnlknglfkng".data(using: .utf8)!)
}
catch {
    if let myError = error as? MyError {
        print("MyError: \(myError.localizedDescription)")
    }
    else {
        print(error.localizedDescription)
    }
}

@swift-ci
Copy link
Collaborator Author

Comment by Slava (JIRA)

I have a similar issue with the following code. In my case Error object is released after being deallocated:

enum ErrorA: Error {
    case A
    case B
}


enum ErrorB: Error {
    case A
    case B
}


func test() {
    let error = ErrorA.A as NSError
    print(error as? ErrorB)
}


test()

call stack:

#0 objc_release ()

#1 test()

#2 main

#3 start ()

log with Zombies enabled:

-[__SwiftNativeNSError release]: message sent to deallocated instance 0x10330cf10

@DougGregor
Copy link
Member

We have a fix for this, which is tracked by the original SR-9207

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis AnthonyLatsis added the crash Bug: A crash, i.e., an abnormal termination of software label Dec 12, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself crash Bug: A crash, i.e., an abnormal termination of software run-time crash Bug → crash: Swift code crashed during execution
Projects
None yet
Development

No branches or pull requests

4 participants