This is an archive of the discontinued LLVM Phabricator instance.

[StaticAnalyzer] Fix nullptr dereference issue found by static analyzer tool
ClosedPublic

Authored by Manna on Jun 5 2023, 1:28 PM.

Details

Summary

This patch uses castAs instead of getAs which will assert if the type doesn't match and directly uses ReceiverType->castAs<ObjCObjectPointerType>() instead of ReceiverObjectPtrType when calling canAssignObjCInterfaces() in findMethodDecl(clang::​ObjCMessageExpr const *, clang::​ObjCObjectPointerType const *, clang::​ASTContext &).

Diff Detail

Event Timeline

Manna created this revision.Jun 5 2023, 1:28 PM
Herald added a project: Restricted Project. · View Herald Transcript
Manna requested review of this revision.Jun 5 2023, 1:28 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 5 2023, 1:28 PM
Manna added inline comments.Jun 5 2023, 1:31 PM
clang/lib/StaticAnalyzer/Checkers/DynamicTypePropagation.cpp
756

We are assigning: ReceiverObjectPtrType = nullptr return value from getAs.

const auto *ReceiverObjectPtrType =
      ReceiverType->getAs<ObjCObjectPointerType>();

Then we are dereferencing nullptr ReceiverObjectPtrType when calling canAssignObjCInterfaces()

erichkeane added inline comments.Jun 6 2023, 6:22 AM
clang/lib/StaticAnalyzer/Checkers/DynamicTypePropagation.cpp
756

This isn't NFC, as ReceiverObjectPtrType is only used here. If the MessageExpr ReceiverKind is not Instance or Class, we never dereference this. So the declaration should be in this branch.

756

Oh, and ALSO, we don't dereference it if ReceiverType is ObjCIdType or ObhjCClassType.

steakhal requested changes to this revision.Jun 6 2023, 6:48 AM

I agree with @erichkeane.
However, I can also see that the code could be improved.

I don't understand why we have that variable hoisted from the guarded block in the first place.
We could directly use ReceiverType->castAs<ObjCObjectPointerType>() instead of ReceiverObjectPtrType.

This revision now requires changes to proceed.Jun 6 2023, 6:48 AM
Manna updated this revision to Diff 529133.Jun 6 2023, 7:45 PM
Manna retitled this revision from [NFC][CLANG] Fix nullptr dereference issue found by static analyzer tool to [StaticAnalyzer] Fix nullptr dereference issue found by static analyzer tool.
Manna edited the summary of this revision. (Show Details)

Thank you @erichkeane and @steakhal for reviews and feedbacks. I have updated patch to address your review comments.

Manna edited the summary of this revision. (Show Details)Jun 6 2023, 7:47 PM
steakhal accepted this revision.Jun 7 2023, 1:10 AM

Please update the title and the summary of this patch to reflect what it actually achieves.
We also usually use the [analyzer] tag only for changes touching StaticAnalyzer stuff.

After these, you can merge the patch.
Thanks for refactoring the to express the intent more clearly.

This revision is now accepted and ready to land.Jun 7 2023, 1:10 AM
erichkeane accepted this revision.Jun 7 2023, 6:23 AM
Manna added a comment.EditedJun 15 2023, 9:00 PM

Thank you @erichkeane and @steakhal for reviews and comments.

Please update the title and the summary of this patch to reflect what it actually achieves.
We also usually use the [analyzer] tag only for changes touching StaticAnalyzer stuff.

After these, you can merge the patch.

Yes, i will update the title and summary of this patch before merging the patch.

This revision was automatically updated to reflect the committed changes.