Skip to content

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

Closed
@swift-ci

Description

@swift-ci
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.

Metadata

Metadata

Assignees

No one assigned

    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