This is an archive of the discontinued LLVM Phabricator instance.

[analyzer] Fix false negative when accessing a nonnull property from a nullable object
Needs ReviewPublic

Authored by tripleCC on Sep 27 2023, 5:52 AM.

Details

Reviewers
NoQ
steakhal
Summary

ObjCMessageExpr gets the actual type through Sema::getMessageSendResultType, instead of using the return type of MethodDecl directly. The final type is generated by considering the receiver and nullability of MethodDecl together. The RetType in NullabilityChecker should all be replaced with M.getOriginExpr()->getType().

@interface A : NSObject
@property (nonnull, nonatomic, strong) NSString *name;
+ (nullable instancetype)shared;
@end

@[[A shared].name];

Consider the code above, the nullability of the name property should depend on the result of the shared method .

Diff Detail

Event Timeline

tripleCC created this revision.Sep 27 2023, 5:52 AM
Herald added a project: Restricted Project. · View Herald Transcript
tripleCC requested review of this revision.Sep 27 2023, 5:52 AM
Herald added a project: Restricted Project. · View Herald TranscriptSep 27 2023, 5:52 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
tripleCC edited the summary of this revision. (Show Details)Sep 27 2023, 5:55 AM
tripleCC edited the summary of this revision. (Show Details)
tripleCC edited the summary of this revision. (Show Details)Sep 27 2023, 5:59 AM
tripleCC added a reviewer: steakhal.
tripleCC edited the summary of this revision. (Show Details)Sep 27 2023, 6:29 AM

For new revisions, prefer creating a PR at GitHub.

clang/lib/StaticAnalyzer/Checkers/NullabilityChecker.cpp
906–907

I'm not sure if getOriginExpr is always non-null. How do you know?

tripleCC edited the summary of this revision. (Show Details)Sep 27 2023, 6:41 AM
tripleCC updated this revision to Diff 557405.Sep 27 2023, 6:54 AM

adjust return type after checking getOriginExpr is non-null

tripleCC added inline comments.Sep 27 2023, 7:00 AM
clang/lib/StaticAnalyzer/Checkers/NullabilityChecker.cpp
906–907

I'm not sure if getOriginExpr is always non-null. How do you know?

The getOriginExpr my be null , I optimized this logic to only perform the operation if getOriginExpr is not null.

tripleCC added a comment.EditedSep 27 2023, 7:08 AM

For new revisions, prefer creating a PR at GitHub.

ok, I will create a github pull request for the patch