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-1700] Runtime crash with NSJSONSerialization example #4144

Closed
ddunbar opened this issue Jun 7, 2016 · 9 comments
Closed

[SR-1700] Runtime crash with NSJSONSerialization example #4144

ddunbar opened this issue Jun 7, 2016 · 9 comments

Comments

@ddunbar
Copy link
Member

ddunbar commented Jun 7, 2016

Previous ID SR-1700
Radar None
Original Reporter @ddunbar
Type Bug
Status Closed
Resolution Invalid
Additional Detail from JIRA
Votes 0
Component/s Foundation
Labels Bug
Assignee None
Priority Medium

md5: 347009a98ceb158c19902eaff3526739

Issue Description:

This trivial sample code crashes at runtime with the latest snapshot, on Ubuntu 15.10

$ cat x.swift                                                                                                                                                   
import Foundation                                                                                                                                                                         
                                                                                                                                                                                          
let x = try! NSJSONSerialization.data(withJSONObject: "a".bridge(), options: .prettyPrinted)                                                                                              
print("x = \(x)")                                                                                                                                                                         

$ /tmp/swift-DEVELOPMENT-SNAPSHOT-2016-06-06-a-ubuntu15.10/usr/bin/swiftc x.swift  && ./x                                                                       
Segmentation fault (core dumped)                                                                                                                                                          
@ddunbar
Copy link
Member Author

ddunbar commented Jun 7, 2016

@phausler any idea what is going on here? I don't know if this is a Foundation or compiler problem.

@phausler
Copy link
Member

phausler commented Jun 7, 2016

Hmm you probably want to have `allowFragments` in the options

@ddunbar
Copy link
Member Author

ddunbar commented Jun 7, 2016

Ah, of course. NSJSONWritingOptions doesn't have allowFragments, but using an array does work as desired.

Should I close this as invalid?

@phausler
Copy link
Member

phausler commented Jun 7, 2016

Yea, an unquoted string is questionably not a json object

@phausler
Copy link
Member

phausler commented Jun 7, 2016

On Darwin running the same thing modulo the bridging

2016-06-07 15:30:29.209739 asdf[74319:18282990] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[NSJSONSerialization dataWithJSONObject:options:error:]: Invalid top-level type in JSON write'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff9367b74b __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x00007fffa6ead80a objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff936f3125 +[NSException raise:format:] + 197
    3   Foundation                          0x00007fff94ff603f +[NSJSONSerialization dataWithJSONObject:options:error:] + 249
    4   asdf                                0x000000010031b1a4 main + 228
    5   libdyld.dylib                       0x00007fffa7786285 start + 1
    6   ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

@ddunbar
Copy link
Member Author

ddunbar commented Jun 7, 2016

Yeah. It's unfortunate we don't get the same kind of information in the crash, to make it more obvious what is going on.

@phausler
Copy link
Member

phausler commented Jun 7, 2016

I wonder if there is a way we can change our fatal errors to call backtrace_symbols to print nicer failures. (Even if they were mangled it would be an improvement)

@ddunbar
Copy link
Member Author

ddunbar commented Jun 7, 2016

Maybe corelibs Foundation should consider internally using an alternative to fatalError() which print the information which would have been in the Obj-C exception?

The default behavior of fatalError() for Swift is desired, IIUC, so that the codegen is minimal (in particular, it doesn't cause register pressure), but for Foundation where the APIs can't change but need to have sensible parity with Obj-C I'm not sure it is appropriate.

@phausler
Copy link
Member

phausler commented Jun 7, 2016

Yep all of our fatal errors are not constrained; filed https://bugs.swift.org/browse/SR-1701 which it should be a pretty easy task to add and vastly improve the state of affairs of debugging issues like this.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from apple/swift May 5, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants