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.


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.
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to