Skip to content

[5.4] In backwards compatibility mode, be more permissive of Obj-C nulls (#35825) #35851

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

Conversation

tbkka
Copy link
Contributor

@tbkka tbkka commented Feb 9, 2021

(This cherry-picks #35825 from main to release/5.4)

The new cast logic checks and aborts if a non-nullable pointer has a null value at runtime. However, because this was tolerated by the old casting logic, some apps inadvertently rely on being able to cast such null references.

This change adds specific checks for null pointers in compatibility mode and handles them similarly to the previous casting logic:

  • Casting to Obj-C or CF type: casting a null pointer succeeds
  • Casting to class-constrained existential: casting a null pointer succeeds
  • Casting to a Swift class type: cast fails without crashing
  • Bridging from Obj-C class to Swift struct type: cast fails without crashing
    This also adds a guard to avoid trying to lookup the dynamic type of a null class reference.

In non-compatibility mode, all of the above cause an immediate runtime crash. The changes here are only intended to support existing binaries running against new Swift runtimes.

Resolves rdar://72323929

…ll references (swiftlang#35825)

(This cherry-picks swiftlang#35825 from main to release/5.4)

The new cast logic checks and aborts if a non-nullable pointer has a null value
at runtime.  However, because this was tolerated by the old casting logic, some
apps inadvertently rely on being able to cast such null references.

This change adds specific checks for null pointers in compatibility mode and
handles them similarly to the previous casting logic:
 * Casting to Obj-C or CF type: casting a null pointer succeeds
 * Casting to class-constrained existential: casting a null pointer succeeds
 * Casting to a Swift class type: cast fails without crashing
 * Bridging from Obj-C class to Swift struct type: cast fails without crashing
This also adds a guard to avoid trying to lookup the dynamic type of a null
class reference.

In non-compatibility mode, all of the above cause an immediate runtime crash.
The changes here are only intended to support existing binaries running against
new Swift runtimes.

Resolves rdar://72323929
@tbkka tbkka requested a review from a team as a code owner February 9, 2021 15:46
@tbkka
Copy link
Contributor Author

tbkka commented Feb 9, 2021

@swift-ci Please test

@tbkka tbkka requested a review from mikeash February 9, 2021 22:25
@tkremenek tkremenek merged commit 91e770b into swiftlang:release/5.4 Feb 9, 2021
@tbkka tbkka deleted the tbkka/permissiveObjCNullCastHandling54 branch August 1, 2024 16:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants