Skip to content

[libc][test] fix memory leak pt.2 #122384

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 9, 2025
Merged

Conversation

nickdesaulniers
Copy link
Member

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

Fixes: #122369
Fixes: #122378

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

Fixes: llvm#122369
Fixes: llvm#122378
@llvmbot
Copy link
Member

llvmbot commented Jan 9, 2025

@llvm/pr-subscribers-libc

Author: Nick Desaulniers (nickdesaulniers)

Changes

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

Fixes: #122369
Fixes: #122378


Full diff: https://github.com/llvm/llvm-project/pull/122384.diff

2 Files Affected:

  • (modified) libc/test/UnitTest/ExecuteFunctionUnix.cpp (+7-7)
  • (modified) libc/test/UnitTest/FPExceptMatcher.cpp (+1-1)
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);

@nickdesaulniers nickdesaulniers merged commit 9426fdd into llvm:main Jan 9, 2025
11 of 12 checks passed
@nickdesaulniers nickdesaulniers deleted the fix_leak2 branch January 9, 2025 22:46
BaiXilin pushed a commit to BaiXilin/llvm-fix-vnni-instr-types that referenced this pull request Jan 12, 2025
These were created with operator new (see `Test::createCallable`), so operator
delete should be used instead of free().

Fixes: llvm#122369
Fixes: llvm#122378
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants