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-11040] EXC_BAD_ACCESS in Swift when accessing @optional ObjC properties #53429
Comments
Comment by Andrew Bennett (JIRA) This might be a duplicate of SR-8622. It was determined in that one by @belkadan that:
Although it seems like it's already being checked for bridged types, and in this particular case it may not be too expensive to treat it as |
Comment by Ding Ye (JIRA) I am not sure if we can call it a Swift compiler bug. The logic of let tmp: SomeSwiftType? = obj.objcProperty // Line 1
if let x = tmp { } // Line 2 In Line 1, an optional (i.e. enum) variable In Line 2, it checks which enum case In your specific case, the inject-enum-addr in Line 1 has undefined behaviour, since the data Anyway, it would be good if clang could raise warnings at the nil returning points in methods declared |
Comment by Andrew Bennett (JIRA) Yeah, I don’t mind whether this is fixed at runtime, but because it only happens on release builds and a lot of the code is optimized out it means that it’s very hard to track down why it’s crashing. We changed it to a required property that is nullable, and at least now ObjC has warnings when you do it wrong (don’t implement it). The real bug IMO is that the nullability requirements of the ObjC interface are not enforced (or warnings) on the implementation. As you said dingobye (JIRA User). This seems inconsistent and incomplete, and for us lead to a difficult to find bug. This ticket is probably mislabeled, I’m sorry, but there didn’t seem to be a better way to label it. |
Comment by Ding Ye (JIRA) For [1] It should have been clearly documented somewhere that violating [2] Improve the [3] Enforce the nullability at runtime. For every |
I think we should consolidate discussion in SR-8622, since the |
Attachment: Download
Environment
{code:bash}$ xcodebuild -version
Xcode 10.2.1
Build version 10E1001
$ swift --version
Apple Swift version 5.0.1 (swiftlang-1001.0.82.4 clang-1001.0.46.5)
Target: x86_64-apple-darwin18.2.0
Additional Detail from JIRA
md5: c3b9d6818264022e889eba045206c98b
duplicates:
Issue Description:
Swift code can crash at runtime with
EXC_BAD_ACCESS
when you use anonnull
property as an@optional
requirement in ObjC that mistakenly returnsnil
.This happens in debug and release builds with unguarded access to that property, and only in release if used in an
if let x = obj.objcProperty
.It only seems to occur when the property isn't a bridged type (it doesn't crash with
String
).The crash output looks like this:
See the attached project for a more nuanced example (ViewController.swift). Note that you will have to run on the debug or release schemes to see all effects.
The text was updated successfully, but these errors were encountered: