This is an archive of the discontinued LLVM Phabricator instance.

Fix nullptr dereference found by Coverity static analysis tool
ClosedPublic

Authored by Manna on May 4 2023, 6:50 PM.

Details

Summary

Reported by Coverity:

In clang::​ASTContext::​hasUniqueObjectRepresentations(clang::​QualType, bool): Return value of function which returns null is dereferenced without checking.
(Ty->isMemberPointerType()) {
    	//returned_null: getAs returns nullptr.
    	//var_assigned: Assigning: MPT = nullptr return value from getAs.
    const auto *MPT = Ty->getAs<MemberPointerType>();
    	
   //dereference: Dereferencing a pointer that might be nullptr MPT when calling getMemberPointerInfo. (The virtual call resolves to 
   <unnamed>::ItaniumCXXABI::getMemberPointerInfo.)
    return !ABI->getMemberPointerInfo(MPT).HasPadding;
  }

This patch uses castAs instead of getAs.

Diff Detail

Event Timeline

Manna created this revision.May 4 2023, 6:50 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 4 2023, 6:50 PM
Manna requested review of this revision.May 4 2023, 6:50 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 4 2023, 6:50 PM
Manna abandoned this revision.May 4 2023, 7:00 PM

Looks like both ABIs that use this assume the parameter passed to getMemberPointerInfo is non-null. While we DO check the type (in reality, we probably should be doing a if (const auto *MPT = Ty->getAs<MemberPointerType>()) instead, I think this is a problem that at least SHOULD be fixed somehow.

Why was this abandoned?

Manna added a comment.May 5 2023, 8:05 AM

Looks like both ABIs that use this assume the parameter passed to getMemberPointerInfo is non-null. While we DO check the type (in reality, we probably should be doing a if (const auto *MPT = Ty->getAs<MemberPointerType>()) instead, I think this is a problem that at least SHOULD be fixed somehow.

Why was this abandoned?

@erichkeane, Sorry i abandoned the patch without any comment. My change (use castAs instead of getAs) was not correct.

Looks like both ABIs that use this assume the parameter passed to getMemberPointerInfo is non-null. While we DO check the type (in reality, we probably should be doing a if (const auto *MPT = Ty->getAs<MemberPointerType>()) instead

Agreed.

Manna updated this revision to Diff 519859.May 5 2023, 8:06 AM

I have updated patch

erichkeane added inline comments.May 5 2023, 8:07 AM
clang/lib/AST/ASTContext.cpp
2862

Remove the curley braces, otherwise LGTM.

Manna updated this revision to Diff 519868.May 5 2023, 8:13 AM
Manna marked an inline comment as done.
Manna added inline comments.
clang/lib/AST/ASTContext.cpp
2862

Missed that. Done

erichkeane accepted this revision.May 5 2023, 8:17 AM
This revision is now accepted and ready to land.May 5 2023, 8:17 AM
Manna added a comment.May 5 2023, 8:19 AM

Thanks @erichkeane for reviews!