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-9207] Crash casting NSError to URLError in Swift 4.x #51697

Closed
bjtitus opened this issue Nov 7, 2018 · 5 comments
Closed

[SR-9207] Crash casting NSError to URLError in Swift 4.x #51697

bjtitus opened this issue Nov 7, 2018 · 5 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 IRGen LLVM IR generation run-time crash Bug → crash: Swift code crashed during execution

Comments

@bjtitus
Copy link

bjtitus commented Nov 7, 2018

Previous ID SR-9207
Radar rdar://problem/45961622
Original Reporter @bjtitus
Type Bug
Status Resolved
Resolution Done

Attachment: Download

Environment

Xcode 10.x

Swift 4.x

macOS 10.14

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

md5: 285c46b30ea972e1d0abbc9cc57dc435

is duplicated by:

  • SR-9548 EXC_BAD_ACCESS when casting NSError to custom Swift.Error within a function
  • SR-10478 EXC_BAD_ACCESS of NSError via caught error.localizedDescription, when trying to match it to other error type.

Issue Description:

I have a case where casting a Swift error to an NSError, and then attempting to cast to a URLError, results in a crash:

enum MyError: Error {
    case someThing
}


let error = MyError.someThing


let foundationError = error as NSError


if let urlError = foundationError as? URLError { // Crash here
    print(urlError)
}

I'm not completely sure if this is a Swift compiler issue as I am unable to reproduce with a plain Swift file, but it is easily reproducible with a vanilla Xcode project (maybe there's a compiler flag I'm missing on the command line).

Checking the domain for NSURLErrorDomain seems to be a workaround.

@belkadan
Copy link
Contributor

belkadan commented Nov 8, 2018

Reduced by using local variables:

import Foundation

enum MyError: Error {
  case someThing
}

func test() {
  let error = MyError.someThing
  
  let foundationError = error as NSError
  
  if let urlError = foundationError as? URLError {
      print(urlError)
  }
}

test()

@mikeash, this sounds familiar, but I can't find a dup, and it's still reproducing for me on pretty-recent-master.

@mikeash
Copy link
Contributor

mikeash commented Nov 8, 2018

I'm not aware of something tracking this. I think it's an ARC bug. The generated code is calling swift_dynamicCast on the error with TakeOnSuccess and DestroyOnFailure flags, but then it also emits a call to objc_release the error.

@belkadan
Copy link
Contributor

belkadan commented Nov 8, 2018

Compiler bug, then. Thanks!

@swift-ci create

@mikeash
Copy link
Contributor

mikeash commented Nov 27, 2018

Actually a runtime bug after all. The two releases are correct, as the value is at +2 at that point. The problem is that swift_dynamicCast passes all flags through to tryDynamicCastNSErrorObjectToValue. When DestroyOnFailure is set and the cast fails, tryDynamicCastNSErrorObjectToValue obediently destroys the value. Then swift_dynamicCast reacts to that failure by also destroying the value.

@DougGregor
Copy link
Member

#28804

@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 IRGen LLVM IR generation run-time crash Bug → crash: Swift code crashed during execution
Projects
None yet
Development

No branches or pull requests

5 participants