[clang] 5c50df6 - [LifetimeSafety] Fix false positives for realloc in loops (#247178)

via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 30 06:53:25 PST 2026


Author: NeKon69
Date: 2026-11-30T14:53:19Z
New Revision: 5c50df68d33a0a227b7239752f0abdc334f06891

URL: https://github.com/llvm/llvm-project/commit/5c50df68d33a0a227b7239752f0abdc334f06891
DIFF: https://github.com/llvm/llvm-project/commit/5c50df68d33a0a227b7239752f0abdc334f06891.diff

LOG: [LifetimeSafety] Fix false positives for realloc in loops (#247178)

Fixes #247059

Added: 
    

Modified: 
    clang/lib/Analysis/LifetimeSafety/FactsGenerator.cpp
    clang/test/Sema/LifetimeSafety/safety.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Analysis/LifetimeSafety/FactsGenerator.cpp b/clang/lib/Analysis/LifetimeSafety/FactsGenerator.cpp
index 52f4fa507979e9..7fc7c933f72186 100644
--- a/clang/lib/Analysis/LifetimeSafety/FactsGenerator.cpp
+++ b/clang/lib/Analysis/LifetimeSafety/FactsGenerator.cpp
@@ -1245,8 +1245,8 @@ void FactsGenerator::handleFunctionCall(const Expr *Call,
   handleMovedArgsInCall(FD, Args);
   handleImplicitObjectFieldUses(Call, FD);
   handleLifetimeCaptureBy(FD, Args);
-  handleAllocatingCall(Call, FD);
   handleFreeingCall(Call, FD, Args);
+  handleAllocatingCall(Call, FD);
   if (!CallList)
     return;
   if (isStdReferenceCast(FD)) {

diff  --git a/clang/test/Sema/LifetimeSafety/safety.cpp b/clang/test/Sema/LifetimeSafety/safety.cpp
index 077c5de9648cbf..fb547df6617a3e 100644
--- a/clang/test/Sema/LifetimeSafety/safety.cpp
+++ b/clang/test/Sema/LifetimeSafety/safety.cpp
@@ -3979,6 +3979,16 @@ void ownership_returns_member_realloc() {
   *p = 1;            // expected-note {{later used here}}
 }
 
+void ownership_returns_member_realloc_in_loop(int n) {
+  OwnershipAllocator a;
+  int *p = a.alloc();
+  for (int i = 0; i < n; ++i) {
+    p = a.realloc2(p);
+    *p = i;
+  }
+  a.dealloc(p);
+}
+
 struct OwnershipTaker {
   OwnershipTaker(int *p) __attribute__((ownership_takes(malloc, 2)));
   OwnershipTaker(int *kept, int *freed)
@@ -4080,6 +4090,33 @@ void builtin_realloc() {
   *p = 1;  // expected-note {{later used here}}
 }
 
+// GH247059: The result of the current call is not freed by that call.
+void realloc_in_loop(int n) {
+  char *p = 0;
+  for (int i = 0; i < n; ++i) {
+    p = (char *)realloc(p, i + 1);
+    p[i] = 'a';
+  }
+  free(p);
+}
+
+void free_malloc_in_loop(int n) {
+  char *p = (char *)malloc(1);
+  for (int i = 0; i < n; ++i) {
+    free(p);
+    p = (char *)malloc(i + 1);
+    p[0] = 'a';
+  }
+  free(p);
+}
+
+void use_old_pointer_after_realloc() {
+  char *p = (char *)malloc(1);     // expected-warning {{object allocated by 'malloc' does not live long enough}}
+  char *q = (char *)realloc(p, 2); // expected-note {{object allocated by 'malloc' is freed here}}
+  p[0] = 'a';                      // expected-note {{later used here}}
+  free(q);
+}
+
 } // namespace allocating_freeing_functions
 
 namespace placement_new {


        


More information about the cfe-commits mailing list