This is an archive of the discontinued LLVM Phabricator instance.

[NFC][CLANG] Fix static code analyzer concerns with dereference null return value
ClosedPublic

Authored by Manna on May 22 2023, 11:32 AM.

Details

Summary

Reported by Static Analyzer Tool:

Inside "SemaExprMember.cpp" file, in clang::​Sema::​BuildMemberReferenceExpr(clang::​Expr *, clang::​QualType, clang::​SourceLocation, bool, clang::​CXXScopeSpec &, clang::​SourceLocation, clang::​NamedDecl *, clang::​DeclarationNameInfo const &, clang::​TemplateArgumentListInfo const *, clang::​Scope const *, clang::​Sema::​ActOnMemberAccessExtraArgs *): Return value of function which returns null is dereferenced without checking.

//Condition !Base, taking true branch.
if (!Base) {
  TypoExpr *TE = nullptr;
  QualType RecordTy = BaseType;

  //Condition IsArrow, taking true branch.
   if (IsArrow) RecordTy = RecordTy->castAs<PointerType>()->getPointeeType();
  	//returned_null: getAs returns nullptr (checked 279 out of 294 times). 
  	//Condition TemplateArgs != NULL, taking true branch.
  	
   //Dereference null return value (NULL_RETURNS)
   //dereference: Dereferencing a pointer that might be nullptr RecordTy->getAs() when calling LookupMemberExprInRecord. 
   if (LookupMemberExprInRecord(
         *this, R, nullptr, RecordTy->getAs<RecordType>(), OpLoc, IsArrow,
         SS, TemplateArgs != nullptr, TemplateKWLoc, TE))
      return ExprError();
   if (TE)
     return TE;

This patch uses castAs instead of getAs which will assert if the type doesn't match.

Diff Detail

Event Timeline

Manna created this revision.May 22 2023, 11:32 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 22 2023, 11:32 AM
Manna requested review of this revision.May 22 2023, 11:32 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 22 2023, 11:32 AM
Manna edited the summary of this revision. (Show Details)May 22 2023, 11:33 AM
Manna edited the summary of this revision. (Show Details)
erichkeane accepted this revision.May 22 2023, 11:55 AM
This revision is now accepted and ready to land.May 22 2023, 11:55 AM

Thank you @erichkeane for reviews!