mirror of
https://github.com/troglobit/finit.git
synced 2026-10-05 15:23:31 +07:00
run_interactive(): Fix descriptor leak.
Make sure to close dup()'ed descriptors when we're done with them. Thanks to Coverity Scan for finding this nasty one! Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
@@ -517,23 +517,33 @@ int run_interactive(char *cmd, char *fmt, ...)
|
|||||||
print_desc("", line);
|
print_desc("", line);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Redirect output from cmd to a tempfile */
|
||||||
if (fp && !debug) {
|
if (fp && !debug) {
|
||||||
oldout = dup(STDOUT_FILENO);
|
oldout = dup(STDOUT_FILENO);
|
||||||
olderr = dup(STDERR_FILENO);
|
olderr = dup(STDERR_FILENO);
|
||||||
dup2(fileno(fp), STDOUT_FILENO);
|
dup2(fileno(fp), STDOUT_FILENO);
|
||||||
dup2(fileno(fp), STDERR_FILENO);
|
dup2(fileno(fp), STDERR_FILENO);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Run cmd ... */
|
||||||
status = run(cmd);
|
status = run(cmd);
|
||||||
|
|
||||||
|
/* Restore stderr/stdout */
|
||||||
if (fp && !debug) {
|
if (fp && !debug) {
|
||||||
if (oldout >= 0)
|
if (oldout >= 0) {
|
||||||
dup2(oldout, STDOUT_FILENO);
|
dup2(oldout, STDOUT_FILENO);
|
||||||
if (olderr >= 0)
|
close(oldout);
|
||||||
|
}
|
||||||
|
if (olderr >= 0) {
|
||||||
dup2(olderr, STDERR_FILENO);
|
dup2(olderr, STDERR_FILENO);
|
||||||
|
close(oldout);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fmt)
|
if (fmt)
|
||||||
print_result(status);
|
print_result(status);
|
||||||
|
|
||||||
|
/* Dump any results of cmd on stderr after we've printed [ OK ] or [FAIL] */
|
||||||
if (fp && !debug) {
|
if (fp && !debug) {
|
||||||
size_t len, written;
|
size_t len, written;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user