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

Printing an NSError casted to Error results in a crash thrown by JSONSerialization. #59804

Open
HellotalkXJP opened this issue Jun 30, 2022 · 8 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. casting Feature: explicit casting (is, as, as? and as!) crash Bug: A crash, i.e., an abnormal termination of software run-time crash Bug → crash: Swift code crashed during execution runtime The Swift Runtime

Comments

@HellotalkXJP
Copy link

Describe the bug

do {
    let jsonObject = try JSONSerialization.jsonObject(with: _json.data(using: String.Encoding.utf8)!, options: .allowFragments)
    if let jsonDict = jsonObject as? NSDictionary {
        return self.deserializeFrom(dict: jsonDict, designatedPath: designatedPath)
    }
} catch let error {
    InternalLogger.logError(error)
}

To Reproduce
Occasionally on iOS15.4.0

Environment (please complete the following information):

  • OS: [e.g. macOS 11.0]
  • Xcode Version/Tag/Branch:

Additional context
image

@HellotalkXJP HellotalkXJP added the bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. label Jun 30, 2022
@coppercash
Copy link

coppercash commented Nov 24, 2022

0 libswiftCore.dylib swift::_swift_getClass(void const*) + 8
1 libswiftCore.dylib swift::SwiftError::isPureNSError() const + 20
2 libswiftCore.dylib _swift_getDynamicType + 108
3 libswiftCore.dylib Swift.String.init<A>(describing: A) -> Swift.String + 348

I got something similar.

iOS 15.4 (19E241) iPhone14,4
Xcode 14.1

@coppercash
Copy link

0 libswiftCore.dylib swift::_swift_getClass(void const*) + 8
1 libswiftCore.dylib swift::SwiftError::isPureNSError() const + 20
2 libswiftCore.dylib _swift_getDynamicType + 108
3 libswiftCore.dylib Swift.String.init<A>(describing: A) -> Swift.String + 348

I got something similar.

iOS 15.4 (19E241) iPhone14,4 Xcode 14.1

I was wrong.
My crash has nothing related to printing Swift.Error.
It was an unexpected nil (from ObjC) filled in an Swift.Error's place.

@AnthonyLatsis AnthonyLatsis added compiler The Swift compiler in itself casting Feature: explicit casting (is, as, as? and as!) run-time crash Bug → crash: Swift code crashed during execution crash Bug: A crash, i.e., an abnormal termination of software runtime The Swift Runtime and removed compiler The Swift compiler in itself labels Nov 28, 2022
@AnthonyLatsis
Copy link
Collaborator

@HellotalkXJP Can you provide a self-contained test case? Reproducing this with so much omitted is going to be guesswork.

@tbkka
Copy link
Contributor

tbkka commented Nov 28, 2022

It was an unexpected nil (from ObjC) filled in an Swift.Error's place.

@coppercash Can you provide a very small (one ObjC file, one Swift file) project that reproduces this? There are already some checks in the runtime to detect cases like this and print out nice error messages before crashing, and I'd like to see if we should add more for this particular issue.

@coppercash
Copy link

It was an unexpected nil (from ObjC) filled in an Swift.Error's place.

@coppercash Can you provide a very small (one ObjC file, one Swift file) project that reproduces this? There are already some checks in the runtime to detect cases like this and print out nice error messages before crashing, and I'd like to see if we should add more for this particular issue.

Sure.
I set up a runnable project.

The stack I get from it:

* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x0)
    frame #0: 0x00007ff811f43124 libswiftCore.dylib`swift::_swift_getClass(void const*) + 20
    frame #1: 0x00007ff811f42a89 libswiftCore.dylib`swift_getErrorValue + 25
  * frame #2: 0x0000000100b69485 NonnullButNilTests`NonnullButNilTests.test_should_crash(self=0x0000000100f056d0) at NonnullButNilTests.swift:7:15

Some nice error messages would definitely be lovely.

@tbkka
Copy link
Contributor

tbkka commented Dec 3, 2022

CC: @mikeash @al45tair

@tbkka
Copy link
Contributor

tbkka commented Dec 3, 2022

@mikeash Should _swift_getClass() have an explicit NULL check and tailored failure message to help people diagnose this kind of problem?

@mikeash
Copy link
Contributor

mikeash commented Dec 5, 2022

That would probably be a good idea. I don't think this function is called so often that the overhead of a NULL check would matter, and a better crash would be useful.

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. casting Feature: explicit casting (is, as, as? and as!) crash Bug: A crash, i.e., an abnormal termination of software run-time crash Bug → crash: Swift code crashed during execution runtime The Swift Runtime
Projects
None yet
Development

No branches or pull requests

5 participants