[PATCH] D140194: [clang-tidy] Fix crash in bugprone-suspicious-realloc-usage.

Balázs Kéri via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Dec 16 01:53:19 PST 2022


balazske created this revision.
Herald added subscribers: carlosgalvezp, steakhal, martong, gamesh411, Szelethus, dkrupp, xazax.hun.
Herald added a reviewer: njames93.
Herald added a project: All.
balazske requested review of this revision.
Herald added a project: clang-tools-extra.
Herald added a subscriber: cfe-commits.

The problem occurs if a statement is found by the checker that has a null child.
Fixes issue #59518.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D140194

Files:
  clang-tools-extra/clang-tidy/bugprone/SuspiciousReallocUsageCheck.cpp
  clang-tools-extra/test/clang-tidy/checkers/bugprone/suspicious-realloc-usage.cpp


Index: clang-tools-extra/test/clang-tidy/checkers/bugprone/suspicious-realloc-usage.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/checkers/bugprone/suspicious-realloc-usage.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/bugprone/suspicious-realloc-usage.cpp
@@ -100,3 +100,10 @@
   // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'p' may be set to null if 'realloc' fails, which may result in a leak of the original buffer [bugprone-suspicious-realloc-usage]
   void *q = p;
 }
+
+void test_null_child(void *p) {
+  for (;;)
+    break;
+  p = realloc(p, 111);
+  // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'p' may be set to null if 'realloc' fails, which may result in a leak of the original buffer [bugprone-suspicious-realloc-usage]
+}
Index: clang-tools-extra/clang-tidy/bugprone/SuspiciousReallocUsageCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/bugprone/SuspiciousReallocUsageCheck.cpp
+++ clang-tools-extra/clang-tidy/bugprone/SuspiciousReallocUsageCheck.cpp
@@ -95,7 +95,7 @@
   }
   bool VisitStmt(const Stmt *S) {
     for (const Stmt *Child : S->children())
-      if (Visit(Child))
+      if (Child && Visit(Child))
         return true;
     return false;
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D140194.483456.patch
Type: text/x-patch
Size: 1305 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20221216/9b64ecea/attachment.bin>


More information about the cfe-commits mailing list