google/EarlGrey

Missing nullability annotation causes crash in libswiftCore.dylib

Interfere opened this issue · 2 comments

Abstract

Under certain circumstances a nil value might be passed to the -[GREYElementMatcherBlock matches:] method, which causes a crash in swift_getObjectType routine.

Steps to reproduce

Sample project to reproduce the crash:
https://github.com/Interfere/LibCoreCrash

Details of the crash

Due to a nonnull attribute applied to the input argument of GREYMatchesBlock, the code generated by compiler assumes passing nil is an impossible situation. At some point the execution flow falls into the swift_getObjectType routine omitting any checks for nullability, causing the crash at reading the data at null address (line 12):

Screenshot 2021-04-19 at 11 57 49

It happens due to the fact, that any matcher actually is able to receive the nil if used as a assertion condition. In the sample project I exposed the one possible situation: when an element, which is supposed to be a target for assertion, was not actually found on screen. I reckon the assertion should fail in that case providing the corresponding circumstances of the failure in the linked NSError object, but not to crash the app.

Solution

There are some possible ways to fix the issue, but I reckon the best one is to provide a proper nullability annotations for GREYMatchesBlock. See the linked Pull Request.

Alternatives considered

Another possible solution is to add a check to call the matcher only if the argument is not nil, but I believe it's the topic for further discussion. I would like to mention then, this alternative would also require to provide proper annotations for GREYMatcher and GREYBaseMatcher.

Sorry, could add a comment with the linked PR?

Merged in #1594.