@@ -36,18 +36,23 @@ int ProcessStatus::get_fatal_signal() {
3636
3737ProcessStatus 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
0 commit comments