[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 01:40:45 PDT 2021


TH3CHARLie created this revision.
TH3CHARLie added a reviewer: NoQ.
Herald added subscribers: manas, steakhal, ASDenysPetrov, martong, dkrupp, donat.nagy, Szelethus, mikhail.ramalho, a.sidorin, szepet, baloghadamsoftware, xazax.hun.
TH3CHARLie requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

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

Make sizeof pointer type is compatible with void* in MallocSizeofChecker.


Repository:
  rG LLVM Github Monorepo

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.348622.patch
Type: text/x-patch
Size: 1350 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210529/39a85162/attachment.bin>


More information about the cfe-commits mailing list