From 06b33c58fd50f615d317c87f30fd5a49142f8739 Mon Sep 17 00:00:00 2001 From: Joachim Nilsson Date: Wed, 26 Nov 2014 17:29:33 +0100 Subject: [PATCH] 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 --- helpers.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/helpers.c b/helpers.c index fca5cdef..09201896 100644 --- a/helpers.c +++ b/helpers.c @@ -517,23 +517,33 @@ int run_interactive(char *cmd, char *fmt, ...) print_desc("", line); } + /* Redirect output from cmd to a tempfile */ if (fp && !debug) { oldout = dup(STDOUT_FILENO); olderr = dup(STDERR_FILENO); dup2(fileno(fp), STDOUT_FILENO); dup2(fileno(fp), STDERR_FILENO); } + + /* Run cmd ... */ status = run(cmd); + + /* Restore stderr/stdout */ if (fp && !debug) { - if (oldout >= 0) + if (oldout >= 0) { dup2(oldout, STDOUT_FILENO); - if (olderr >= 0) + close(oldout); + } + if (olderr >= 0) { dup2(olderr, STDERR_FILENO); + close(oldout); + } } if (fmt) print_result(status); + /* Dump any results of cmd on stderr after we've printed [ OK ] or [FAIL] */ if (fp && !debug) { size_t len, written;