Fix two problems in the ExecuteBinary function.

- If the process exits abnormally then we will leak the stdout and
  stderr FDs. Fix it by closing the FDs before returning.

- If another child process exits then we will incorrectly return the
  result from that process instead of waiting for our child. Fix it
  by using waitpid instead of wait.

Change-Id: I8974d5e4bd33f264cd2d364f55a60f1f5cb7eb1a
This commit is contained in:
Peter Collingbourne
2021-04-15 21:42:37 -07:00
parent 6b5e6bba9b
commit b3982fc8be

View File

@@ -72,7 +72,8 @@ std::unique_ptr<ProcResult> ExecuteBinary(const std::vector<std::string>& argv)
argv0[i] = argv[i].c_str();
}
argv0[argv.size()] = nullptr;
switch (fork()) {
int pid = fork();
switch (pid) {
case -1: // error
free(argv0);
PLOG(ERROR) << "fork";
@@ -104,8 +105,10 @@ std::unique_ptr<ProcResult> ExecuteBinary(const std::vector<std::string>& argv)
close(stdout[1]);
close(stderr[1]);
int status;
wait(&status);
waitpid(pid, &status, 0);
if (!WIFEXITED(status)) {
close(stdout[0]);
close(stderr[0]);
return nullptr;
}
std::unique_ptr<ProcResult> result(new ProcResult());