[libc-commits] [libc] [libc][test] fix memory leak pt.2 (PR #122384)

Nick Desaulniers via libc-commits libc-commits at lists.llvm.org
Thu Jan 9 14:41:49 PST 2025


https://github.com/nickdesaulniers created https://github.com/llvm/llvm-project/pull/122384

These were created with operator new (see `Test::createCallable`), so operator
delete should be used instead of free().

Fixes: #122369
Fixes: #122378


>From b86e1d0cfb7d6c3dbede929dda0626f300d26b4b Mon Sep 17 00:00:00 2001
From: Nick Desaulniers <ndesaulniers at google.com>
Date: Thu, 9 Jan 2025 14:36:59 -0800
Subject: [PATCH] [libc][test] fix memory leak pt.2

These were created with operator new (see `Test::createCallable`), so operator
delete should be used instead of free().

Fixes: #122369
Fixes: #122378
---
 libc/test/UnitTest/ExecuteFunctionUnix.cpp | 14 +++++++-------
 libc/test/UnitTest/FPExceptMatcher.cpp     |  2 +-
 2 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/libc/test/UnitTest/ExecuteFunctionUnix.cpp b/libc/test/UnitTest/ExecuteFunctionUnix.cpp
index b004e8c0891171..c90d7e0cb9ff17 100644
--- a/libc/test/UnitTest/ExecuteFunctionUnix.cpp
+++ b/libc/test/UnitTest/ExecuteFunctionUnix.cpp
@@ -37,7 +37,7 @@ int ProcessStatus::get_fatal_signal() {
 ProcessStatus invoke_in_subprocess(FunctionCaller *func, unsigned timeout_ms) {
   int pipe_fds[2];
   if (::pipe(pipe_fds) == -1) {
-    ::free(func);
+    delete func;
     return ProcessStatus::error("pipe(2) failed");
   }
 
@@ -46,13 +46,13 @@ ProcessStatus invoke_in_subprocess(FunctionCaller *func, unsigned timeout_ms) {
   ::fflush(stdout);
   pid_t pid = ::fork();
   if (pid == -1) {
-    ::free(func);
+    delete func;
     return ProcessStatus::error("fork(2) failed");
   }
 
   if (!pid) {
     (*func)();
-    ::free(func);
+    delete func;
     ::exit(0);
   }
   ::close(pipe_fds[1]);
@@ -63,13 +63,13 @@ ProcessStatus invoke_in_subprocess(FunctionCaller *func, unsigned timeout_ms) {
   // No events requested so this call will only return after the timeout or if
   // the pipes peer was closed, signaling the process exited.
   if (::poll(&poll_fd, 1, timeout_ms) == -1) {
-    ::free(func);
+    delete func;
     return ProcessStatus::error("poll(2) failed");
   }
   // If the pipe wasn't closed by the child yet then timeout has expired.
   if (!(poll_fd.revents & POLLHUP)) {
     ::kill(pid, SIGKILL);
-    ::free(func);
+    delete func;
     return ProcessStatus::timed_out_ps();
   }
 
@@ -78,11 +78,11 @@ ProcessStatus invoke_in_subprocess(FunctionCaller *func, unsigned timeout_ms) {
   // and doesn't turn into a zombie.
   pid_t status = ::waitpid(pid, &wstatus, 0);
   if (status == -1) {
-    ::free(func);
+    delete func;
     return ProcessStatus::error("waitpid(2) failed");
   }
   assert(status == pid);
-  ::free(func);
+  delete func;
   return {wstatus};
 }
 
diff --git a/libc/test/UnitTest/FPExceptMatcher.cpp b/libc/test/UnitTest/FPExceptMatcher.cpp
index 928c3df6ac48a6..119a06985b8f1c 100644
--- a/libc/test/UnitTest/FPExceptMatcher.cpp
+++ b/libc/test/UnitTest/FPExceptMatcher.cpp
@@ -44,7 +44,7 @@ FPExceptMatcher::FPExceptMatcher(FunctionCaller *func) {
   fputil::get_env(&oldEnv);
   if (sigsetjmp(jumpBuffer, 1) == 0)
     func->call();
-  free(func);
+  delete func;
   // We restore the previous floating point environment after
   // the call to the function which can potentially raise SIGFPE.
   fputil::set_env(&oldEnv);



More information about the libc-commits mailing list