[clang] ad571e0 - [NFC][CLANG] Fix issue with dereference null return value found by Coverity

via cfe-commits cfe-commits at lists.llvm.org
Wed May 24 19:17:35 PDT 2023


Author: Manna, Soumi
Date: 2023-05-24T19:17:03-07:00
New Revision: ad571e0d84b30f73fa36d6694c66d5b0fb896f97

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

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

Reported by Static Analyzer Tool, Coverity:

Inside "SemaDeclCXX.cpp" file, in clang::Sema::CheckExplicitlyDefaultedSpecialMember(clang::CXXMethodDecl *, clang::Sema::CXXSpecialMember, clang::SourceLocation): Return value of function which returns null is dereferenced without checking.

  //returned_null: getAs returns nullptr (checked 117 out of 143 times).
  // var_assigned: Assigning: Type = nullptr return value from getAs.
  const FunctionProtoType *Type = MD->getType()->getAs<FunctionProtoType>();

  //Dereference null return value (NULL_RETURNS)
  //dereference: Dereferencing a pointer that might be nullptr Type when calling getReturnType.
  ReturnType = Type->getReturnType();

  //Dereference null return value (NULL_RETURNS)
  //dereference: Dereferencing a pointer that might be nullptr Type when calling getParamType.
  QualType ArgType = ExpectedParams ? Type->getParamType(0) : QualType();

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/D151281

Added: 
    

Modified: 
    clang/lib/Sema/SemaDeclCXX.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index cbe4229b05d0..65122c04c4b3 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -7517,7 +7517,7 @@ bool Sema::CheckExplicitlyDefaultedSpecialMember(CXXMethodDecl *MD,
     }
   }
 
-  const FunctionProtoType *Type = MD->getType()->getAs<FunctionProtoType>();
+  const FunctionProtoType *Type = MD->getType()->castAs<FunctionProtoType>();
 
   bool CanHaveConstParam = false;
   if (CSM == CXXCopyConstructor)


        


More information about the cfe-commits mailing list