[PATCH] D151280: [NFC][CLANG] Fix static code analyzer concerns

Soumi Manna via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue May 23 20:04:30 PDT 2023


Manna created this revision.
Manna added a reviewer: erichkeane.
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.

Reported by Static Analyzer Tool, Coverity:

Dereference null return value

Inside "CGExprConstant.cpp" file, in <unnamed>::​ConstExprEmitter::​VisitObjCEncodeExpr(clang::​ObjCEncodeExpr *, clang::​QualType): Return value of function which returns null is dereferenced without checking.

  std::string Str;
      CGM.getContext().getObjCEncodingForType(E->getEncodedType(), Str);
      	//returned_null: getAsConstantArrayType returns nullptr (checked 81 out of 93 times).
      	//var_assigned: Assigning: CAT = nullptr return value from getAsConstantArrayType.
      const ConstantArrayType *CAT = CGM.getContext().getAsConstantArrayType(T);
  
     // Resize the string to the right size, adding zeros at the end, or
     // truncating as needed.
      	identity_transfer: Member function call CAT->getSize() returns an offset off CAT (this). 
      	
     //Dereference null return value (NULL_RETURNS)
     //dereference: Dereferencing a pointer that might be nullptr CAT->getSize() when calling getZExtValue.
     Str.resize(CAT->getSize().getZExtValue(), '\0');
     return llvm::ConstantDataArray::getString(VMContext, Str, false);

This patch adds an assert for unexpected type for array initializer.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D151280

Files:
  clang/lib/CodeGen/CGExprConstant.cpp


Index: clang/lib/CodeGen/CGExprConstant.cpp
===================================================================
--- clang/lib/CodeGen/CGExprConstant.cpp
+++ clang/lib/CodeGen/CGExprConstant.cpp
@@ -1340,6 +1340,7 @@
     std::string Str;
     CGM.getContext().getObjCEncodingForType(E->getEncodedType(), Str);
     const ConstantArrayType *CAT = CGM.getContext().getAsConstantArrayType(T);
+    assert(CAT && "unexpected type for array initializer");
 
     // Resize the string to the right size, adding zeros at the end, or
     // truncating as needed.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D151280.524980.patch
Type: text/x-patch
Size: 556 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230524/d079b764/attachment.bin>


More information about the cfe-commits mailing list