[clang] 620cef9 - [analyzer] MallocSizeof: sizeof pointer type is compatible with void*

Xuanda Yang via cfe-commits cfe-commits at lists.llvm.org
Sat May 29 18:52:39 PDT 2021


Author: Xuanda Yang
Date: 2021-05-30T09:51:41+08:00
New Revision: 620cef91207bbeb570a529328976040e658a60ee

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

LOG: [analyzer] MallocSizeof: sizeof pointer type is compatible with void*

source: https://bugs.llvm.org/show_bug.cgi?id=50214

Make sizeof pointer type compatible with void* in MallocSizeofChecker.

Reviewed By: NoQ

Differential Revision: https://reviews.llvm.org/D103358

Added: 
    

Modified: 
    clang/lib/StaticAnalyzer/Checkers/MallocSizeofChecker.cpp
    clang/test/Analysis/malloc-sizeof.c

Removed: 
    


################################################################################
diff  --git a/clang/lib/StaticAnalyzer/Checkers/MallocSizeofChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/MallocSizeofChecker.cpp
index 71f593cb2b561..4b5206a102b87 100644
--- a/clang/lib/StaticAnalyzer/Checkers/MallocSizeofChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/MallocSizeofChecker.cpp
@@ -139,6 +139,10 @@ static bool typesCompatible(ASTContext &C, QualType A, QualType B) {
   if (B->isVoidPointerType() && A->getAs<PointerType>())
     return true;
 
+  // sizeof(pointer type) is compatible with void*
+  if (A->isVoidPointerType() && B->getAs<PointerType>())
+    return true;
+
   while (true) {
     A = A.getCanonicalType();
     B = B.getCanonicalType();

diff  --git a/clang/test/Analysis/malloc-sizeof.c b/clang/test/Analysis/malloc-sizeof.c
index ee104245b819a..22c4045b7bbd3 100644
--- a/clang/test/Analysis/malloc-sizeof.c
+++ b/clang/test/Analysis/malloc-sizeof.c
@@ -26,6 +26,8 @@ void foo(unsigned int unsignedInt, unsigned int readSize) {
   struct A *ap5 = calloc(4, sizeof(struct B)); // expected-warning {{Result of 'calloc' is converted to a pointer of type 'struct A', which is incompatible with sizeof operand type 'struct B'}}
   struct A *ap6 = realloc(ap5, sizeof(struct A));
   struct A *ap7 = realloc(ap5, sizeof(struct B)); // expected-warning {{Result of 'realloc' is converted to a pointer of type 'struct A', which is incompatible with sizeof operand type 'struct B'}}
+
+  void **vpp1 = (void **)malloc(sizeof(struct A*)); // no warning
 }
 
 // Don't warn when the types 
diff er only by constness.


        


More information about the cfe-commits mailing list