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-8965] NSError.localizedDescription caused SIGABRT with StaticString #51470

Open
swift-ci opened this issue Oct 10, 2018 · 3 comments
Open
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. standard library Area: Standard library umbrella

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-8965
Radar https://bugreport.apple.com/web/?problemID=45171933
Original Reporter leohemanth (JIRA User)
Type Bug

Attachment: Download

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

md5: c599fed8c34270c778132f5ec0f0703f

Issue Description:

Calling localizedDescription on NSError when userInfo has StringString causes SIGABRT

The following code causes SIGABRT every single time.

let reason: StaticString = "Static String"
let userInfo = [
 NSLocalizedDescriptionKey: "Localized Description",
 "Reason": reason
]
let error = NSError(domain: "Some Error", code: 0, userInfo: userInfo)
print(error.localizedDescription)

Attached sample playground to showcase the issue.

@swift-ci
Copy link
Collaborator Author

Comment by Garo Hussenjian (JIRA)

Here are some details from our crash report..

Fatal Exception: NSInvalidArgumentException
-[_SwiftValue _fastCStringContents:]: unrecognized selector sent to instance 0x280bfc5c0

Fatal Exception: NSInvalidArgumentException
0 CoreFoundation 0x1eacd3ef8 __exceptionPreprocess
1 libobjc.A.dylib 0x1e9ea1a40 objc_exception_throw
2 CoreFoundation 0x1eabeb154 -[NSOrderedSet initWithSet:copyItems:]
3 CoreFoundation 0x1eacd9810 _forwarding_
4 CoreFoundation 0x1eacdb4bc _CF_forwarding_prep_0
5 libswiftCore.dylib 0x104130314 *makeCocoaStringGuts(*🙂
6 libswiftCore.dylib 0x104130030 String.init(_cocoaString🙂
...

@jckarter
Copy link
Member

I suspect that what's happening is that `reason` is causing the entire Dictionary to be inferred with `StaticString` keys, since `StaticString` is ExpressibleByStringLiteral and the type checker favors forming a dictionary with one value type over one with `Any` or some other polymorphic type. The `localizedDescription` property implementation expects the value of NSLocalizedDescriptionKey to be a `String`. This is all working "correctly" as intended, though the combination of behaviors here is definitely surprising. You could do this to work around the issue:

```
import Foundation

let reason: StaticString = "Static String"
let userInfo: [String: Any] = [
NSLocalizedDescriptionKey: "Localized Description",
"Reason": reason
]
let error = NSError(domain: "Some Error", code: 0, userInfo: userInfo)
print(error.localizedDescription)
```

By explicitly giving the dictionary `Any` as its value type, "Localized Description" is allowed to default to String, even though reason is a StaticString.

@swift-ci
Copy link
Collaborator Author

Comment by Garo Hussenjian (JIRA)

Fascinating, thanks @jckarter

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
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. standard library Area: Standard library umbrella
Projects
None yet
Development

No branches or pull requests

2 participants