[PATCH] D151040: [NFC][CLANG] Fix static analyzer concerns

Soumi Manna via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat May 20 18:14:45 PDT 2023


Manna created this revision.
Herald added subscribers: manas, ASDenysPetrov, dkrupp, donat.nagy, Szelethus, a.sidorin, baloghadamsoftware.
Herald added a project: All.
Manna requested review of this revision.
Herald added a project: clang.

Dereference null return value

Inside "ExprConstant.cpp" file, in <unnamed>::​RecordExprEvaluator::​VisitCXXStdInitializerListExpr(clang::​CXXStdInitializerListExpr const *): Return value of function which returns null is dereferenced without checking.

  bool RecordExprEvaluator::VisitCXXStdInitializerListExpr(
   const CXXStdInitializerListExpr *E) {
       // returned_null: getAsConstantArrayType returns nullptr (checked 81 out of 93 times). [show details]
       //var_assigned: Assigning: ArrayType = nullptr return value from getAsConstantArrayType.
    const ConstantArrayType *ArrayType =
       Info.Ctx.getAsConstantArrayType(E->getSubExpr()->getType());
    LValue Array;
    //Condition !EvaluateLValue(E->getSubExpr(), Array, this->Info, false), taking false branch.
    if (!EvaluateLValue(E->getSubExpr(), Array, Info))
     return false;
  
    // Get a pointer to the first element of the array.
       	
   //Dereference null return value (NULL_RETURNS)
  //dereference: Dereferencing a pointer that might be nullptr ArrayType when calling addArray. [show details]
    Array.addArray(Info, E, ArrayType);
  `

This patch adds an assert


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D151040

Files:
  clang/lib/AST/ExprConstant.cpp


Index: clang/lib/AST/ExprConstant.cpp
===================================================================
--- clang/lib/AST/ExprConstant.cpp
+++ clang/lib/AST/ExprConstant.cpp
@@ -10169,6 +10169,7 @@
     const CXXStdInitializerListExpr *E) {
   const ConstantArrayType *ArrayType =
       Info.Ctx.getAsConstantArrayType(E->getSubExpr()->getType());
+  assert(ArrayType && "unexpected type for array initializer");
 
   LValue Array;
   if (!EvaluateLValue(E->getSubExpr(), Array, Info))


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D151040.524071.patch
Type: text/x-patch
Size: 489 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230521/2f3772e1/attachment.bin>


More information about the cfe-commits mailing list