@@ -36,18 +36,23 @@ int ProcessStatus::get_fatal_signal() {
36
36
37
37
ProcessStatus invoke_in_subprocess (FunctionCaller *func, unsigned timeout_ms) {
38
38
int pipe_fds[2 ];
39
- if (::pipe (pipe_fds) == -1 )
39
+ if (::pipe (pipe_fds) == -1 ) {
40
+ ::free (func);
40
41
return ProcessStatus::error (" pipe(2) failed" );
42
+ }
41
43
42
44
// Don't copy the buffers into the child process and print twice.
43
45
::fflush (stderr);
44
46
::fflush (stdout);
45
47
pid_t pid = ::fork ();
46
- if (pid == -1 )
48
+ if (pid == -1 ) {
49
+ ::free (func);
47
50
return ProcessStatus::error (" fork(2) failed" );
51
+ }
48
52
49
53
if (!pid) {
50
54
(*func)();
55
+ ::free (func);
51
56
::exit (0 );
52
57
}
53
58
::close (pipe_fds[1 ]);
@@ -57,21 +62,27 @@ ProcessStatus invoke_in_subprocess(FunctionCaller *func, unsigned timeout_ms) {
57
62
};
58
63
// No events requested so this call will only return after the timeout or if
59
64
// 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);
61
67
return ProcessStatus::error (" poll(2) failed" );
68
+ }
62
69
// If the pipe wasn't closed by the child yet then timeout has expired.
63
70
if (!(poll_fd.revents & POLLHUP)) {
64
71
::kill (pid, SIGKILL);
72
+ ::free (func);
65
73
return ProcessStatus::timed_out_ps ();
66
74
}
67
75
68
76
int wstatus = 0 ;
69
77
// Wait on the pid of the subprocess here so it gets collected by the system
70
78
// and doesn't turn into a zombie.
71
79
pid_t status = ::waitpid (pid, &wstatus, 0 );
72
- if (status == -1 )
80
+ if (status == -1 ) {
81
+ ::free (func);
73
82
return ProcessStatus::error (" waitpid(2) failed" );
83
+ }
74
84
assert (status == pid);
85
+ ::free (func);
75
86
return {wstatus};
76
87
}
77
88
0 commit comments