From b3982fc8be5cac77e572fc540addd9d489b42698 Mon Sep 17 00:00:00 2001 From: Peter Collingbourne Date: Thu, 15 Apr 2021 21:42:37 -0700 Subject: 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 --- libs/androidfw/PosixUtils.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'libs/androidfw/PosixUtils.cpp') diff --git a/libs/androidfw/PosixUtils.cpp b/libs/androidfw/PosixUtils.cpp index f1ab1493012a..4ec525a01da5 100644 --- a/libs/androidfw/PosixUtils.cpp +++ b/libs/androidfw/PosixUtils.cpp @@ -72,7 +72,8 @@ std::unique_ptr ExecuteBinary(const std::vector& 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 ExecuteBinary(const std::vector& 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 result(new ProcResult()); -- cgit v1.2.3-59-g8ed1b