[clang] 34d8cd1 - [NFC][CLANG] Fix issue with dereference null return value found by Coverity static analyzer tool

via cfe-commits cfe-commits at lists.llvm.org
Fri May 19 19:46:17 PDT 2023


Author: Manna, Soumi
Date: 2023-05-19T19:41:38-07:00
New Revision: 34d8cd153812c3e5e2db1a1e27f057db33b07b6e

URL: https://github.com/llvm/llvm-project/commit/34d8cd153812c3e5e2db1a1e27f057db33b07b6e
DIFF: https://github.com/llvm/llvm-project/commit/34d8cd153812c3e5e2db1a1e27f057db33b07b6e.diff

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

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.

Reviewed By: erichkeane

Differential Revision: https://reviews.llvm.org/D150968

Added: 
    

Modified: 
    clang/lib/AST/Decl.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp
index cf00db9c948b..5317b9d805b5 100644
--- a/clang/lib/AST/Decl.cpp
+++ b/clang/lib/AST/Decl.cpp
@@ -3301,7 +3301,7 @@ bool FunctionDecl::isReplaceableGlobalAllocationFunction(
     QualType T = Ty;
     while (const auto *TD = T->getAs<TypedefType>())
       T = TD->getDecl()->getUnderlyingType();
-    IdentifierInfo *II = T->getAs<EnumType>()->getDecl()->getIdentifier();
+    IdentifierInfo *II = T->castAs<EnumType>()->getDecl()->getIdentifier();
     if (II && II->isStr("__hot_cold_t"))
       Consume();
   }


        


More information about the cfe-commits mailing list