[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [PATCH 1/6] tests: Resolve Coverity RESOURCE_LEAK in commandhelper



Coverity determined that 'log' and 'newenv' were not freed in
some cases. Free them in 'error' branch and normal branch.

Signed-off-by: Wang Rui <moon wangrui huawei com>
---
 tests/commandhelper.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)

diff --git a/tests/commandhelper.c b/tests/commandhelper.c
index 796b89d..0bba0d6 100644
--- a/tests/commandhelper.c
+++ b/tests/commandhelper.c
@@ -61,7 +61,7 @@ int main(int argc, char **argv) {
     size_t i, n;
     int open_max;
     char **origenv;
-    char **newenv;
+    char **newenv = NULL;
     char *cwd;
     FILE *log = fopen(abs_builddir "/commandhelper.log", "w");
 
@@ -80,7 +80,7 @@ int main(int argc, char **argv) {
     }
 
     if (VIR_ALLOC_N_QUIET(newenv, n) < 0)
-        return EXIT_FAILURE;
+        goto error;
 
     origenv = environ;
     n = i = 0;
@@ -100,7 +100,7 @@ int main(int argc, char **argv) {
 
     open_max = sysconf(_SC_OPEN_MAX);
     if (open_max < 0)
-        return EXIT_FAILURE;
+        goto error;
     for (i = 0; i < open_max; i++) {
         int f;
         int closed;
@@ -114,15 +114,13 @@ int main(int argc, char **argv) {
 
     fprintf(log, "DAEMON:%s\n", getpgrp() == getsid(0) ? "yes" : "no");
     if (!(cwd = getcwd(NULL, 0)))
-        return EXIT_FAILURE;
+        goto error;
     if (strlen(cwd) > strlen(".../commanddata") &&
         STREQ(cwd + strlen(cwd) - strlen("/commanddata"), "/commanddata"))
         strcpy(cwd, ".../commanddata");
     fprintf(log, "CWD:%s\n", cwd);
     VIR_FREE(cwd);
 
-    VIR_FORCE_FCLOSE(log);
-
     if (argc > 1 && STREQ(argv[1], "--close-stdin")) {
         if (freopen("/dev/null", "r", stdin) != stdin)
             goto error;
@@ -154,9 +152,14 @@ int main(int argc, char **argv) {
     fprintf(stderr, "END STDERR\n");
     fflush(stderr);
 
+    VIR_FORCE_FCLOSE(log);
+    VIR_FREE(newenv);
+
     return EXIT_SUCCESS;
 
  error:
+    VIR_FORCE_FCLOSE(log);
+    VIR_FREE(newenv);
     return EXIT_FAILURE;
 }
 
-- 
1.7.12.4



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]