This is an archive of the discontinued LLVM Phabricator instance.

[NFC][CLANG] Fix static code analyzer concerns
ClosedPublic

Authored by Manna on May 20 2023, 6:14 PM.

Details

Summary

Reported by Static Analyzer Tool:

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). 
     //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. 
  Array.addArray(Info, E, ArrayType);

This patch adds an assert.

Diff Detail

Event Timeline

Manna created this revision.May 20 2023, 6:14 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 20 2023, 6:14 PM
Manna requested review of this revision.May 20 2023, 6:14 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 20 2023, 6:14 PM
Manna edited the summary of this revision. (Show Details)May 20 2023, 6:15 PM
Manna added a reviewer: erichkeane.
Manna retitled this revision from [NFC][CLANG] Fix static analyzer concerns to [NFC][CLANG] Fix static code analyzer concerns.
Manna edited the summary of this revision. (Show Details)May 20 2023, 6:17 PM
Manna edited the summary of this revision. (Show Details)
erichkeane added inline comments.May 22 2023, 6:15 AM
clang/lib/AST/ExprConstant.cpp
10172

I think this ends up being a regression if the LValue doesn't evaluate right. This should probably go down near 10179.

Manna updated this revision to Diff 524291.May 22 2023, 7:07 AM

Thank you @erichkeane for reviews. I have updated patch to avoid regression if the LValue doesn't evaluate right.

Manna marked an inline comment as done.May 22 2023, 7:09 AM
erichkeane accepted this revision.May 22 2023, 7:16 AM
This revision is now accepted and ready to land.May 22 2023, 7:16 AM
Manna set the repository for this revision to rG LLVM Github Monorepo.May 22 2023, 12:25 PM
This revision was automatically updated to reflect the committed changes.