Skip to content

Commit 9ece640

Browse files
[libc][test] fix memory leak
Looks like the smart pointer I removed was being used to free the underlying object. Fixes: llvm#122369
1 parent 0efb376 commit 9ece640

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

libc/test/UnitTest/ExecuteFunctionUnix.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,23 @@ int ProcessStatus::get_fatal_signal() {
3636

3737
ProcessStatus invoke_in_subprocess(FunctionCaller *func, unsigned timeout_ms) {
3838
int pipe_fds[2];
39-
if (::pipe(pipe_fds) == -1)
39+
if (::pipe(pipe_fds) == -1) {
40+
::free(func);
4041
return ProcessStatus::error("pipe(2) failed");
42+
}
4143

4244
// Don't copy the buffers into the child process and print twice.
4345
::fflush(stderr);
4446
::fflush(stdout);
4547
pid_t pid = ::fork();
46-
if (pid == -1)
48+
if (pid == -1) {
49+
::free(func);
4750
return ProcessStatus::error("fork(2) failed");
51+
}
4852

4953
if (!pid) {
5054
(*func)();
55+
::free(func);
5156
::exit(0);
5257
}
5358
::close(pipe_fds[1]);
@@ -57,21 +62,27 @@ ProcessStatus invoke_in_subprocess(FunctionCaller *func, unsigned timeout_ms) {
5762
};
5863
// No events requested so this call will only return after the timeout or if
5964
// the pipes peer was closed, signaling the process exited.
60-
if (::poll(&poll_fd, 1, timeout_ms) == -1)
65+
if (::poll(&poll_fd, 1, timeout_ms) == -1) {
66+
::free(func);
6167
return ProcessStatus::error("poll(2) failed");
68+
}
6269
// If the pipe wasn't closed by the child yet then timeout has expired.
6370
if (!(poll_fd.revents & POLLHUP)) {
6471
::kill(pid, SIGKILL);
72+
::free(func);
6573
return ProcessStatus::timed_out_ps();
6674
}
6775

6876
int wstatus = 0;
6977
// Wait on the pid of the subprocess here so it gets collected by the system
7078
// and doesn't turn into a zombie.
7179
pid_t status = ::waitpid(pid, &wstatus, 0);
72-
if (status == -1)
80+
if (status == -1) {
81+
::free(func);
7382
return ProcessStatus::error("waitpid(2) failed");
83+
}
7484
assert(status == pid);
85+
::free(func);
7586
return {wstatus};
7687
}
7788

libc/test/UnitTest/FPExceptMatcher.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ FPExceptMatcher::FPExceptMatcher(FunctionCaller *func) {
4444
fputil::get_env(&oldEnv);
4545
if (sigsetjmp(jumpBuffer, 1) == 0)
4646
func->call();
47+
free(func);
4748
// We restore the previous floating point environment after
4849
// the call to the function which can potentially raise SIGFPE.
4950
fputil::set_env(&oldEnv);

0 commit comments

Comments
 (0)