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

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


This revision was automatically updated to reflect the committed changes.
Closed by commit rG620cef91207b: [analyzer] MallocSizeof: sizeof pointer type is compatible with void* (authored by TH3CHARLie).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D103358/new/

https://reviews.llvm.org/D103358

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


Index: clang/test/Analysis/malloc-sizeof.c
===================================================================
--- clang/test/Analysis/malloc-sizeof.c
+++ clang/test/Analysis/malloc-sizeof.c
@@ -26,6 +26,8 @@
   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 differ only by constness.
Index: clang/lib/StaticAnalyzer/Checkers/MallocSizeofChecker.cpp
===================================================================
--- clang/lib/StaticAnalyzer/Checkers/MallocSizeofChecker.cpp
+++ clang/lib/StaticAnalyzer/Checkers/MallocSizeofChecker.cpp
@@ -139,6 +139,10 @@
   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();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D103358.348670.patch
Type: text/x-patch
Size: 1350 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210530/1c15341d/attachment.bin>


More information about the cfe-commits mailing list