This is an archive of the discontinued LLVM Phabricator instance.

[NFC][CLANG] Fix issue with dereference null return value found by Coverity static analyzer tool
ClosedPublic

Authored by Manna on May 19 2023, 7:50 AM.

Details

Summary

Reported by Coverity:

In clang::​FunctionDecl::​isReplaceableGlobalAllocationFunction(std::​optional<unsigned int> *, bool *): Return value of function which returns null is dereferenced without checking 

if (!IsSizedDelete && !Ty.isNull() && Ty->isEnumeralType()) {
     QualType T = Ty;
     //Condition TD, taking false branch.
    while (const auto *TD = T->getAs<TypedefType>())
      T = TD->getDecl()->getUnderlyingType();
      //returned_null: getAs returns nullptr (checked 95 out of 97 times). 
    	
    //Dereference null return value (NULL_RETURNS)
    // dereference: Dereferencing a pointer that might be nullptr T->getAs() when calling getDecl. 
    IdentifierInfo *II = T->getAs<EnumType>()->getDecl()->getIdentifier();
    if (II && II->isStr("__hot_cold_t"))
      Consume();
  }

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 19 2023, 7:50 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 19 2023, 7:50 AM
Manna requested review of this revision.May 19 2023, 7:50 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 19 2023, 7:50 AM
erichkeane accepted this revision.May 19 2023, 7:56 AM
This revision is now accepted and ready to land.May 19 2023, 7:56 AM
Manna added a comment.May 19 2023, 8:00 AM

Thank you @erichkeane for review!