summaryrefslogtreecommitdiff
path: root/libc/test/UnitTest/ExecuteFunctionUnix.cpp
diff options
context:
space:
mode:
authorNick Desaulniers <nickdesaulniers@users.noreply.github.com>2025-01-09 14:15:31 -0800
committerGitHub <noreply@github.com>2025-01-09 14:15:31 -0800
commit3caa68a021c2f795de3df7f6ad7953556e825fab (patch)
treecd55419eade069b3401727cf4726018f03fde90b /libc/test/UnitTest/ExecuteFunctionUnix.cpp
parent156e6051630d136b48090c0d1cb79a4457564e9d (diff)
[libc][test] fix memory leak (#122378)
Looks like the smart pointer I removed was being used to free the underlying object. Fixes: #122369
Diffstat (limited to 'libc/test/UnitTest/ExecuteFunctionUnix.cpp')
-rw-r--r--libc/test/UnitTest/ExecuteFunctionUnix.cpp19
1 files changed, 15 insertions, 4 deletions
diff --git a/libc/test/UnitTest/ExecuteFunctionUnix.cpp b/libc/test/UnitTest/ExecuteFunctionUnix.cpp
index df7173866859..b004e8c08911 100644
--- a/libc/test/UnitTest/ExecuteFunctionUnix.cpp
+++ b/libc/test/UnitTest/ExecuteFunctionUnix.cpp
@@ -36,18 +36,23 @@ int ProcessStatus::get_fatal_signal() {
ProcessStatus invoke_in_subprocess(FunctionCaller *func, unsigned timeout_ms) {
int pipe_fds[2];
- if (::pipe(pipe_fds) == -1)
+ if (::pipe(pipe_fds) == -1) {
+ ::free(func);
return ProcessStatus::error("pipe(2) failed");
+ }
// Don't copy the buffers into the child process and print twice.
::fflush(stderr);
::fflush(stdout);
pid_t pid = ::fork();
- if (pid == -1)
+ if (pid == -1) {
+ ::free(func);
return ProcessStatus::error("fork(2) failed");
+ }
if (!pid) {
(*func)();
+ ::free(func);
::exit(0);
}
::close(pipe_fds[1]);
@@ -57,11 +62,14 @@ 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)
+ if (::poll(&poll_fd, 1, timeout_ms) == -1) {
+ ::free(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);
return ProcessStatus::timed_out_ps();
}
@@ -69,9 +77,12 @@ ProcessStatus invoke_in_subprocess(FunctionCaller *func, unsigned timeout_ms) {
// Wait on the pid of the subprocess here so it gets collected by the system
// and doesn't turn into a zombie.
pid_t status = ::waitpid(pid, &wstatus, 0);
- if (status == -1)
+ if (status == -1) {
+ ::free(func);
return ProcessStatus::error("waitpid(2) failed");
+ }
assert(status == pid);
+ ::free(func);
return {wstatus};
}