Skip to content

[PlaygroundLogger] Implemented a check to avoid trying to log nil-con… #40

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

Merged

Conversation

cwakamo
Copy link
Member

@cwakamo cwakamo commented Jan 24, 2020

…taining non-Optional objects.

It is undefined behavior for a non-Optional object to be nil in Swift, and at present, PlaygroundLogger triggers crashes in the runtime when trying to log such objects.
The most common way for such an object to exist is for an Objective-C API to be annotated as returning nonnull but in fact returns nil.
In a regular Swift program, this can be worked around by explicitly assigning the value to an Optional.
PlaygroundLogger cannot do that as it only receives an Any, and assigning to Any? does not recover the nil.

As a result, inspect the contents of the Any's existential container (i.e. the thing in memory that is the Any itself, not the value inside of the Any) to determine if it contains a non-Optional object whose value is nil.

This commit includes additional tests to ensure that we can log this broken value without crashing and that we can log a nil Optional object correctly.

This addresses rdar://problem/56370098.

Copy link

@griotspeak griotspeak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice documentation

…taining non-Optional objects.

It is undefined behavior for a non-Optional object to be nil in Swift, and at present, PlaygroundLogger triggers crashes in the runtime when trying to log such objects.
The most common way for such an object to exist is for an Objective-C API to be annotated as returning nonnull but in fact returns nil.
In a regular Swift program, this can be worked around by explicitly assigning the value to an Optional.
PlaygroundLogger cannot do that as it only receives an Any, and assigning to Any? does not recover the nil.

As a result, inspect the contents of the Any's existential container (i.e. the thing in memory that is the Any itself, not the value inside of the Any) to determine if it contains a non-Optional object whose value is nil.

This commit includes additional tests to ensure that we can log this broken value without crashing and that we can log a nil Optional object correctly.

This addresses <rdar://problem/56370098>.
@cwakamo cwakamo force-pushed the eng/PR-56370098-avoid-crashing-on-nil branch from a643349 to 1cad754 Compare February 2, 2020 23:56
@cwakamo cwakamo requested a review from jonathanpenn February 2, 2020 23:56
@cwakamo
Copy link
Member Author

cwakamo commented Feb 2, 2020

@jonathanpenn I made the changes you requested, would you mind taking another look at this PR?

@jonathanpenn
Copy link

Looks great!

@cwakamo cwakamo merged commit 6eb061c into apple:master Feb 3, 2020
@cwakamo cwakamo deleted the eng/PR-56370098-avoid-crashing-on-nil branch February 3, 2020 19:53
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.

4 participants