[PATCH 2/4] tests: Fix leaks in commandtest

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

 



---
 tests/commandtest.c |  213 +++++++++++++++++++++++++++++++++-----------------
 1 files changed, 140 insertions(+), 73 deletions(-)

diff --git a/tests/commandtest.c b/tests/commandtest.c
index 9ccbcef..48c6335 100644
--- a/tests/commandtest.c
+++ b/tests/commandtest.c
@@ -107,7 +107,7 @@ static int test0(const void *unused ATTRIBUTE_UNUSED)
 
     free(virtTestLogContentAndReset());
     cmd = virCommandNew(abs_builddir "/commandhelper-doesnotexist");
-    if (virCommandRun(cmd, NULL) == 0)
+    if (!cmd || virCommandRun(cmd, NULL) == 0)
         goto cleanup;
     if ((log = virtTestLogContentAndReset()) == NULL)
         goto cleanup;
@@ -133,7 +133,7 @@ static int test1(const void *unused ATTRIBUTE_UNUSED)
     int status;
 
     cmd = virCommandNew(abs_builddir "/commandhelper-doesnotexist");
-    if (virCommandRun(cmd, &status) < 0)
+    if (!cmd || virCommandRun(cmd, &status) < 0)
         goto cleanup;
     if (status == 0)
         goto cleanup;
@@ -151,28 +151,32 @@ cleanup:
 static int test2(const void *unused ATTRIBUTE_UNUSED)
 {
     virCommandPtr cmd = virCommandNew(abs_builddir "/commandhelper");
-    int ret;
+    virErrorPtr err = NULL;
+    int ret = -1;
 
-    if (virCommandRun(cmd, NULL) < 0) {
-        virErrorPtr err = virGetLastError();
-        printf("Cannot run child %s\n", err->message);
+    if (!cmd)
         return -1;
-    }
 
-    if ((ret = checkoutput("test2")) != 0) {
-        virCommandFree(cmd);
-        return ret;
+    if (virCommandRun(cmd, NULL) < 0) {
+        err = virGetLastError();
+        goto cleanup;
     }
 
+    if (checkoutput("test2") != 0)
+        goto cleanup;
+
     if (virCommandRun(cmd, NULL) < 0) {
-        virErrorPtr err = virGetLastError();
-        printf("Cannot run child %s\n", err->message);
-        return -1;
+        err = virGetLastError();
+        goto cleanup;
     }
 
-    virCommandFree(cmd);
+    ret = checkoutput("test2");
 
-    return checkoutput("test2");
+cleanup:
+    if (err)
+        printf("Cannot run child %s\n", err->message);
+    virCommandFree(cmd);
+    return ret;
 }
 
 /*
@@ -185,6 +189,10 @@ static int test3(const void *unused ATTRIBUTE_UNUSED)
     int newfd1 = dup(STDERR_FILENO);
     int newfd2 = dup(STDERR_FILENO);
     int newfd3 = dup(STDERR_FILENO);
+    int ret = -1;
+
+    if (!cmd)
+        goto cleanup;
 
     virCommandPreserveFD(cmd, newfd1);
     virCommandTransferFD(cmd, newfd3);
@@ -192,21 +200,23 @@ static int test3(const void *unused ATTRIBUTE_UNUSED)
     if (virCommandRun(cmd, NULL) < 0) {
         virErrorPtr err = virGetLastError();
         printf("Cannot run child %s\n", err->message);
-        return -1;
+        goto cleanup;
     }
 
     if (fcntl(newfd1, F_GETFL) < 0 ||
         fcntl(newfd2, F_GETFL) < 0 ||
         fcntl(newfd3, F_GETFL) >= 0) {
         puts("fds in wrong state");
-        return -1;
+        goto cleanup;
     }
 
+    ret = checkoutput("test3");
+
+cleanup:
     virCommandFree(cmd);
     VIR_FORCE_CLOSE(newfd1);
     VIR_FORCE_CLOSE(newfd2);
-
-    return checkoutput("test3");
+    return ret;
 }
 
 
@@ -218,8 +228,12 @@ static int test3(const void *unused ATTRIBUTE_UNUSED)
 static int test4(const void *unused ATTRIBUTE_UNUSED)
 {
     virCommandPtr cmd = virCommandNew(abs_builddir "/commandhelper");
-    pid_t pid;
     char *pidfile = virFilePid(abs_builddir, "commandhelper");
+    pid_t pid;
+    int ret = -1;
+
+    if (!cmd || !pidfile)
+        goto cleanup;
 
     virCommandSetPidFile(cmd, pidfile);
     virCommandDaemonize(cmd);
@@ -227,21 +241,22 @@ static int test4(const void *unused ATTRIBUTE_UNUSED)
     if (virCommandRun(cmd, NULL) < 0) {
         virErrorPtr err = virGetLastError();
         printf("Cannot run child %s\n", err->message);
-        return -1;
+        goto cleanup;
     }
 
     if (virFileReadPid(abs_builddir, "commandhelper", &pid) != 0) {
         printf("cannot read pidfile\n");
-        return -1;
+        goto cleanup;
     }
     while (kill(pid, 0) != -1)
         usleep(100*1000);
 
-    virCommandFree(cmd);
+    ret = checkoutput("test4");
 
+cleanup:
+    virCommandFree(cmd);
     VIR_FREE(pidfile);
-
-    return checkoutput("test4");
+    return ret;
 }
 
 
@@ -252,18 +267,24 @@ static int test4(const void *unused ATTRIBUTE_UNUSED)
 static int test5(const void *unused ATTRIBUTE_UNUSED)
 {
     virCommandPtr cmd = virCommandNew(abs_builddir "/commandhelper");
+    int ret = -1;
+
+    if (!cmd)
+        return -1;
 
     virCommandAddEnvPassCommon(cmd);
 
     if (virCommandRun(cmd, NULL) < 0) {
         virErrorPtr err = virGetLastError();
         printf("Cannot run child %s\n", err->message);
-        return -1;
+        goto cleanup;
     }
 
-    virCommandFree(cmd);
+    ret = checkoutput("test5");
 
-    return checkoutput("test5");
+cleanup:
+    virCommandFree(cmd);
+    return ret;
 }
 
 
@@ -274,6 +295,10 @@ static int test5(const void *unused ATTRIBUTE_UNUSED)
 static int test6(const void *unused ATTRIBUTE_UNUSED)
 {
     virCommandPtr cmd = virCommandNew(abs_builddir "/commandhelper");
+    int ret = -1;
+
+    if (!cmd)
+        return -1;
 
     virCommandAddEnvPass(cmd, "DISPLAY");
     virCommandAddEnvPass(cmd, "DOESNOTEXIST");
@@ -281,12 +306,14 @@ static int test6(const void *unused ATTRIBUTE_UNUSED)
     if (virCommandRun(cmd, NULL) < 0) {
         virErrorPtr err = virGetLastError();
         printf("Cannot run child %s\n", err->message);
-        return -1;
+        goto cleanup;
     }
 
-    virCommandFree(cmd);
+    ret = checkoutput("test6");
 
-    return checkoutput("test6");
+cleanup:
+    virCommandFree(cmd);
+    return ret;
 }
 
 
@@ -297,6 +324,10 @@ static int test6(const void *unused ATTRIBUTE_UNUSED)
 static int test7(const void *unused ATTRIBUTE_UNUSED)
 {
     virCommandPtr cmd = virCommandNew(abs_builddir "/commandhelper");
+    int ret = -1;
+
+    if (!cmd)
+        return -1;
 
     virCommandAddEnvPassCommon(cmd);
     virCommandAddEnvPass(cmd, "DISPLAY");
@@ -305,12 +336,14 @@ static int test7(const void *unused ATTRIBUTE_UNUSED)
     if (virCommandRun(cmd, NULL) < 0) {
         virErrorPtr err = virGetLastError();
         printf("Cannot run child %s\n", err->message);
-        return -1;
+        goto cleanup;
     }
 
-    virCommandFree(cmd);
+    ret = checkoutput("test7");
 
-    return checkoutput("test7");
+cleanup:
+    virCommandFree(cmd);
+    return ret;
 }
 
 /*
@@ -320,6 +353,10 @@ static int test7(const void *unused ATTRIBUTE_UNUSED)
 static int test8(const void *unused ATTRIBUTE_UNUSED)
 {
     virCommandPtr cmd = virCommandNew(abs_builddir "/commandhelper");
+    int ret = -1;
+
+    if (!cmd)
+        return -1;
 
     virCommandAddEnvString(cmd, "LANG=C");
     virCommandAddEnvPair(cmd, "USER", "test");
@@ -327,12 +364,14 @@ static int test8(const void *unused ATTRIBUTE_UNUSED)
     if (virCommandRun(cmd, NULL) < 0) {
         virErrorPtr err = virGetLastError();
         printf("Cannot run child %s\n", err->message);
-        return -1;
+        goto cleanup;
     }
 
-    virCommandFree(cmd);
+    ret = checkoutput("test8");
 
-    return checkoutput("test8");
+cleanup:
+    virCommandFree(cmd);
+    return ret;
 }
 
 
@@ -344,6 +383,10 @@ static int test9(const void *unused ATTRIBUTE_UNUSED)
 {
     virCommandPtr cmd = virCommandNew(abs_builddir "/commandhelper");
     const char* const args[] = { "arg1", "arg2", NULL };
+    int ret = -1;
+
+    if (!cmd)
+        return -1;
 
     virCommandAddArg(cmd, "-version");
     virCommandAddArgPair(cmd, "-log", "bar.log");
@@ -353,12 +396,14 @@ static int test9(const void *unused ATTRIBUTE_UNUSED)
     if (virCommandRun(cmd, NULL) < 0) {
         virErrorPtr err = virGetLastError();
         printf("Cannot run child %s\n", err->message);
-        return -1;
+        goto cleanup;
     }
 
-    virCommandFree(cmd);
+    ret = checkoutput("test9");
 
-    return checkoutput("test9");
+cleanup:
+    virCommandFree(cmd);
+    return ret;
 }
 
 
@@ -372,18 +417,24 @@ static int test10(const void *unused ATTRIBUTE_UNUSED)
     const char *const args[] = {
         "-version", "-log=bar.log", NULL,
     };
+    int ret = -1;
+
+    if (!cmd)
+        return -1;
 
     virCommandAddArgSet(cmd, args);
 
     if (virCommandRun(cmd, NULL) < 0) {
         virErrorPtr err = virGetLastError();
         printf("Cannot run child %s\n", err->message);
-        return -1;
+        goto cleanup;
     }
 
-    virCommandFree(cmd);
+    ret = checkoutput("test10");
 
-    return checkoutput("test10");
+cleanup:
+    virCommandFree(cmd);
+    return ret;
 }
 
 /*
@@ -397,16 +448,22 @@ static int test11(const void *unused ATTRIBUTE_UNUSED)
         "-version", "-log=bar.log", NULL,
     };
     virCommandPtr cmd = virCommandNewArgs(args);
+    int ret = -1;
+
+    if (!cmd)
+        return -1;
 
     if (virCommandRun(cmd, NULL) < 0) {
         virErrorPtr err = virGetLastError();
         printf("Cannot run child %s\n", err->message);
-        return -1;
+        goto cleanup;
     }
 
-    virCommandFree(cmd);
+    ret = checkoutput("test11");
 
-    return checkoutput("test11");
+cleanup:
+    virCommandFree(cmd);
+    return ret;
 }
 
 /*
@@ -416,18 +473,24 @@ static int test11(const void *unused ATTRIBUTE_UNUSED)
 static int test12(const void *unused ATTRIBUTE_UNUSED)
 {
     virCommandPtr cmd = virCommandNew(abs_builddir "/commandhelper");
+    int ret = -1;
+
+    if (!cmd)
+        return -1;
 
     virCommandSetInputBuffer(cmd, "Hello World\n");
 
     if (virCommandRun(cmd, NULL) < 0) {
         virErrorPtr err = virGetLastError();
         printf("Cannot run child %s\n", err->message);
-        return -1;
+        goto cleanup;
     }
 
-    virCommandFree(cmd);
+    ret = checkoutput("test12");
 
-    return checkoutput("test12");
+cleanup:
+    virCommandFree(cmd);
+    return ret;
 }
 
 /*
@@ -443,28 +506,27 @@ static int test13(const void *unused ATTRIBUTE_UNUSED)
         "END STDOUT\n";
     int ret = -1;
 
+    if (!cmd)
+        return -1;
+
     virCommandSetInputBuffer(cmd, "Hello World\n");
     virCommandSetOutputBuffer(cmd, &outactual);
 
     if (virCommandRun(cmd, NULL) < 0) {
         virErrorPtr err = virGetLastError();
         printf("Cannot run child %s\n", err->message);
-        return -1;
+        goto cleanup;
     }
 
-    virCommandFree(cmd);
-
     if (!STREQ(outactual, outexpect)) {
         virtTestDifference(stderr, outactual, outexpect);
         goto cleanup;
     }
 
-    if (checkoutput("test13") < 0)
-        goto cleanup;
-
-    ret = 0;
+    ret = checkoutput("test13");
 
 cleanup:
+    virCommandFree(cmd);
     VIR_FREE(outactual);
     return ret;
 }
@@ -486,6 +548,9 @@ static int test14(const void *unused ATTRIBUTE_UNUSED)
         "END STDERR\n";
     int ret = -1;
 
+    if (!cmd)
+        return -1;
+
     virCommandSetInputBuffer(cmd, "Hello World\n");
     virCommandSetOutputBuffer(cmd, &outactual);
     virCommandSetErrorBuffer(cmd, &erractual);
@@ -493,11 +558,9 @@ static int test14(const void *unused ATTRIBUTE_UNUSED)
     if (virCommandRun(cmd, NULL) < 0) {
         virErrorPtr err = virGetLastError();
         printf("Cannot run child %s\n", err->message);
-        return -1;
+        goto cleanup;
     }
 
-    virCommandFree(cmd);
-
     if (!STREQ(outactual, outexpect)) {
         virtTestDifference(stderr, outactual, outexpect);
         goto cleanup;
@@ -507,12 +570,10 @@ static int test14(const void *unused ATTRIBUTE_UNUSED)
         goto cleanup;
     }
 
-    if (checkoutput("test14") < 0)
-        goto cleanup;
-
-    ret = 0;
+    ret = checkoutput("test14");
 
 cleanup:
+    virCommandFree(cmd);
     VIR_FREE(outactual);
     VIR_FREE(erractual);
     return ret;
@@ -526,18 +587,24 @@ cleanup:
 static int test15(const void *unused ATTRIBUTE_UNUSED)
 {
     virCommandPtr cmd = virCommandNew(abs_builddir "/commandhelper");
+    int ret = -1;
+
+    if (!cmd)
+        return -1;
 
     virCommandSetWorkingDirectory(cmd, abs_builddir "/commanddata");
 
     if (virCommandRun(cmd, NULL) < 0) {
         virErrorPtr err = virGetLastError();
         printf("Cannot run child %s\n", err->message);
-        return -1;
+        goto cleanup;
     }
 
-    virCommandFree(cmd);
+    ret = checkoutput("test15");
 
-    return checkoutput("test15");
+cleanup:
+    virCommandFree(cmd);
+    return ret;
 }
 
 /*
@@ -551,13 +618,16 @@ static int test16(const void *unused ATTRIBUTE_UNUSED)
     int ret = -1;
     int fd = -1;
 
+    if (!cmd)
+        return -1;
+
     virCommandAddEnvPair(cmd, "A", "B");
     virCommandAddArg(cmd, "C");
 
     if ((outactual = virCommandToString(cmd)) == NULL) {
         virErrorPtr err = virGetLastError();
         printf("Cannot convert to string: %s\n", err->message);
-        return -1;
+        goto cleanup;
     }
     if ((fd = open(abs_builddir "/commandhelper.log",
                    O_CREAT | O_TRUNC | O_WRONLY, 0600)) < 0) {
@@ -570,18 +640,15 @@ static int test16(const void *unused ATTRIBUTE_UNUSED)
         goto cleanup;
     }
 
-    virCommandFree(cmd);
-
-    if (checkoutput("test16") < 0)
-        goto cleanup;
-
     if (!STREQ(outactual, outexpect)) {
         virtTestDifference(stderr, outactual, outexpect);
         goto cleanup;
     }
-    ret = 0;
+
+    ret = checkoutput("test16");
 
 cleanup:
+    virCommandFree(cmd);
     VIR_FORCE_CLOSE(fd);
     VIR_FREE(outactual);
     return ret;
-- 
1.7.3.2

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list


[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]