]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
lxc_container.c: avoid a leak on error paths
authorJim Meyering <meyering@redhat.com>
Fri, 4 Sep 2009 14:12:35 +0000 (16:12 +0200)
committerJim Meyering <meyering@redhat.com>
Fri, 4 Sep 2009 16:59:29 +0000 (18:59 +0200)
* src/lxc_container.c (lxcContainerMountBasicFS): Don't leak upon failure.
Add "cleanup:" label and change each post-allocation failure to
use "goto cleanup" rather than returning immediately.

src/lxc_container.c

index 20738648a5597c78e3f305a3e10b0205ecdf385c..f4381e7411df64f368025b36b687f4ab9e525f61 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Copyright IBM Corp. 2008
- * Copyright Red Hat 2008
+ * Copyright Red Hat 2008-2009
  *
  * lxc_container.c: file description
  *
@@ -384,12 +384,12 @@ static int lxcContainerMountBasicFS(virDomainFSDefPtr root)
         { "none", "/selinux", "selinuxfs" },
 #endif
     };
-    int i, rc;
+    int i, rc = -1;
     char *devpts;
 
     if (virAsprintf(&devpts, "/.oldroot%s/dev/pts", root->src) < 0) {
         virReportOOMError(NULL);
-        return -1;
+        return rc;
     }
 
     for (i = 0 ; i < ARRAY_CARDINALITY(mnts) ; i++) {
@@ -397,31 +397,35 @@ static int lxcContainerMountBasicFS(virDomainFSDefPtr root)
             virReportSystemError(NULL, errno,
                                  _("failed to mkdir %s"),
                                  mnts[i].src);
-            return -1;
+            goto cleanup;
         }
         if (mount(mnts[i].src, mnts[i].dst, mnts[i].type, 0, NULL) < 0) {
             virReportSystemError(NULL, errno,
                                  _("failed to mount %s on %s"),
                                  mnts[i].type, mnts[i].type);
-            return -1;
+            goto cleanup;
         }
     }
 
     if ((rc = virFileMakePath("/dev/pts") < 0)) {
         virReportSystemError(NULL, rc, "%s",
                              _("cannot create /dev/pts"));
-        return -1;
+        goto cleanup;
     }
 
     VIR_DEBUG("Trying to move %s to %s", devpts, "/dev/pts");
     if ((rc = mount(devpts, "/dev/pts", NULL, MS_MOVE, NULL)) < 0) {
         virReportSystemError(NULL, errno, "%s",
                              _("failed to mount /dev/pts in container"));
-        return -1;
+        goto cleanup;
     }
+
+    rc = 0;
+
+ cleanup:
     VIR_FREE(devpts);
 
-    return 0;
+    return rc;
 }
 
 static int lxcContainerPopulateDevices(void)