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-9548] EXC_BAD_ACCESS when casting NSError to custom Swift.Error within a function #51997

Closed
swift-ci opened this issue Dec 20, 2018 · 2 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 standard library Area: Standard library umbrella

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-9548
Radar None
Original Reporter leonid.kokhnovych (JIRA User)
Type Bug
Status Resolved
Resolution Duplicate
Environment

OS X: 10.14.2 (18C54)

Xcode: Version 10.1 (10B61)

swiftc --version

Apple Swift version 4.2.1 (swiftlang-1000.11.42 clang-1000.11.45.1)

Target: x86_64-apple-darwin18.2.0

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

md5: ec9ccf1e97e808e3f56a47fea6e90a24

duplicates:

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

Issue Description:

There is a crash with reason EXC_BAD_ACCESS when running the following code:

import Foundation


enum Test1Error: Error {
    case test1
}


enum Test2Error: Error {
    case test2
}


func testFunction(_ error: NSError) {
    let myError = error as? Test2Error
    print(String(describing: myError))
}


testFunction(Test1Error.test1 as NSError)

It does print "nil" from the "testFunction" in the Xcode console, but it gets crashed upon returning to the line

testFunction(Test1Error.test1 as NSError)

I used Xcode Playground to test the code.

As a workaround for the crash, you can first cast NSError to Swift.Error and then to a custom Swift.Error:

import Foundation


enum Test1Error: Error {
    case test1
}


enum Test2Error: Error {
    case test2
}


func testFunction(_ error: NSError) {
    let myError = (error as Error) as? Test2Error
    print(String(describing: myError))
}


testFunction(Test1Error.test1 as NSError)
@belkadan
Copy link
Contributor

@mikeash, is this one of the things that got fixed for 5.0?

@mikeash
Copy link
Contributor

mikeash commented Dec 20, 2018

Looks the same as https://bugs.swift.org/browse/SR-9207 but I haven't yet fixed it.

@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 standard library Area: Standard library umbrella
Projects
None yet
Development

No branches or pull requests

4 participants