[libvirt PATCH 1/2] lxc: replace VIR_FREE with g_autofree / g_free

Daniel P. Berrangé berrange at redhat.com
Wed May 13 16:22:40 UTC 2020


Signed-off-by: Daniel P. Berrangé <berrange at redhat.com>
---
 src/lxc/lxc_cgroup.c     |  14 +--
 src/lxc/lxc_conf.c       |  15 +--
 src/lxc/lxc_container.c  | 256 ++++++++++++++-------------------------
 src/lxc/lxc_controller.c | 126 ++++++++-----------
 src/lxc/lxc_domain.c     |  24 ++--
 src/lxc/lxc_driver.c     |  60 ++++-----
 src/lxc/lxc_fuse.c       |   8 +-
 src/lxc/lxc_monitor.c    |   7 +-
 src/lxc/lxc_native.c     |  72 ++++++-----
 src/lxc/lxc_process.c    | 124 ++++++++-----------
 10 files changed, 269 insertions(+), 437 deletions(-)

diff --git a/src/lxc/lxc_cgroup.c b/src/lxc/lxc_cgroup.c
index 7df723a4da..4ae34925e6 100644
--- a/src/lxc/lxc_cgroup.c
+++ b/src/lxc/lxc_cgroup.c
@@ -387,16 +387,16 @@ virCgroupPtr virLXCCgroupCreate(virDomainDefPtr def,
                                 int *nicindexes)
 {
     virCgroupPtr cgroup = NULL;
-    char *machineName = virLXCDomainGetMachineName(def, 0);
+    g_autofree char *machineName = virLXCDomainGetMachineName(def, 0);
 
     if (!machineName)
-        goto cleanup;
+        return NULL;
 
     if (def->resource->partition[0] != '/') {
         virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                        _("Resource partition '%s' must start with '/'"),
                        def->resource->partition);
-        goto cleanup;
+        return NULL;
     }
 
     if (virCgroupNewMachine(machineName,
@@ -410,7 +410,7 @@ virCgroupPtr virLXCCgroupCreate(virDomainDefPtr def,
                             -1,
                             0,
                             &cgroup) < 0)
-        goto cleanup;
+        return NULL;
 
     /* setup control group permissions for user namespace */
     if (def->idmap.uidmap) {
@@ -419,14 +419,10 @@ virCgroupPtr virLXCCgroupCreate(virDomainDefPtr def,
                               def->idmap.gidmap[0].target,
                               (1 << VIR_CGROUP_CONTROLLER_SYSTEMD)) < 0) {
             virCgroupFree(&cgroup);
-            cgroup = NULL;
-            goto cleanup;
+            return NULL;
         }
     }
 
- cleanup:
-    VIR_FREE(machineName);
-
     return cgroup;
 }
 
diff --git a/src/lxc/lxc_conf.c b/src/lxc/lxc_conf.c
index 8469f30b9e..43e9b70651 100644
--- a/src/lxc/lxc_conf.c
+++ b/src/lxc/lxc_conf.c
@@ -62,7 +62,7 @@ virCapsPtr virLXCDriverCapsInit(virLXCDriverPtr driver)
     virCapsPtr caps;
     virCapsGuestPtr guest;
     virArch altArch;
-    char *lxc_path = NULL;
+    g_autofree char *lxc_path = NULL;
 
     if ((caps = virCapabilitiesNew(virArchFromHost(),
                                    false, false)) == NULL)
@@ -135,8 +135,6 @@ virCapsPtr virLXCDriverCapsInit(virLXCDriverPtr driver)
             goto error;
     }
 
-    VIR_FREE(lxc_path);
-
     if (driver) {
         /* Security driver data */
         const char *doi, *model, *label, *type;
@@ -167,7 +165,6 @@ virCapsPtr virLXCDriverCapsInit(virLXCDriverPtr driver)
     return caps;
 
  error:
-    VIR_FREE(lxc_path);
     virObjectUnref(caps);
     return NULL;
 }
@@ -290,9 +287,9 @@ virLXCDriverConfigDispose(void *obj)
 {
     virLXCDriverConfigPtr cfg = obj;
 
-    VIR_FREE(cfg->configDir);
-    VIR_FREE(cfg->autostartDir);
-    VIR_FREE(cfg->stateDir);
-    VIR_FREE(cfg->logDir);
-    VIR_FREE(cfg->securityDriverName);
+    g_free(cfg->configDir);
+    g_free(cfg->autostartDir);
+    g_free(cfg->stateDir);
+    g_free(cfg->logDir);
+    g_free(cfg->securityDriverName);
 }
diff --git a/src/lxc/lxc_container.c b/src/lxc/lxc_container.c
index 1c28ecfdb7..bd78fa7af6 100644
--- a/src/lxc/lxc_container.c
+++ b/src/lxc/lxc_container.c
@@ -136,8 +136,8 @@ int lxcContainerHasReboot(void)
         CLONE_NEWIPC|SIGCHLD;
     int cpid;
     char *childStack;
-    char *stack;
-    char *buf;
+    g_autofree char *stack = NULL;
+    g_autofree char *buf = NULL;
     int cmd, v;
     int status;
     char *tmp;
@@ -152,10 +152,8 @@ int lxcContainerHasReboot(void)
     if (virStrToLong_i(buf, NULL, 10, &v) < 0) {
         virReportError(VIR_ERR_INTERNAL_ERROR,
                        _("Malformed ctrl-alt-del setting '%s'"), buf);
-        VIR_FREE(buf);
         return -1;
     }
-    VIR_FREE(buf);
     cmd = v ? LINUX_REBOOT_CMD_CAD_ON : LINUX_REBOOT_CMD_CAD_OFF;
 
     if (VIR_ALLOC_N(stack, stacksize) < 0)
@@ -164,7 +162,6 @@ int lxcContainerHasReboot(void)
     childStack = stack + stacksize;
 
     cpid = clone(lxcContainerRebootChild, childStack, flags, &cmd);
-    VIR_FREE(stack);
     if (cpid < 0) {
         virReportSystemError(errno, "%s",
                              _("Unable to clone to check reboot support"));
@@ -638,7 +635,7 @@ static int lxcContainerResolveSymlinks(virDomainFSDefPtr fs, bool gentle)
 
     VIR_DEBUG("Resolved '%s' to %s", fs->src->path, newroot);
 
-    VIR_FREE(fs->src->path);
+    g_free(fs->src->path);
     fs->src->path = newroot;
 
     return 0;
@@ -648,7 +645,7 @@ static int lxcContainerPrepareRoot(virDomainDefPtr def,
                                    virDomainFSDefPtr root,
                                    const char *sec_mount_options)
 {
-    char *dst;
+    g_autofree char *dst = NULL;
     char *tmp;
 
     VIR_DEBUG("Prepare root %d", root->type);
@@ -679,24 +676,21 @@ static int lxcContainerPrepareRoot(virDomainDefPtr def,
 
     if (lxcContainerMountFSBlock(root, "", sec_mount_options) < 0) {
         root->dst = tmp;
-        VIR_FREE(dst);
         return -1;
     }
 
     root->dst = tmp;
     root->type = VIR_DOMAIN_FS_TYPE_MOUNT;
-    VIR_FREE(root->src->path);
-    root->src->path = dst;
+    g_free(root->src->path);
+    root->src->path = g_steal_pointer(&dst);
 
     return 0;
 }
 
 static int lxcContainerPivotRoot(virDomainFSDefPtr root)
 {
-    int ret;
-    char *oldroot = NULL, *newroot = NULL;
-
-    ret = -1;
+    g_autofree char *oldroot = NULL;
+    g_autofree char *newroot = NULL;
 
     VIR_DEBUG("Pivot via %s", root->src->path);
 
@@ -704,7 +698,7 @@ static int lxcContainerPivotRoot(virDomainFSDefPtr root)
     if (mount("", "/", "none", MS_PRIVATE|MS_REC, NULL) < 0) {
         virReportSystemError(errno, "%s",
                              _("Failed to make root private"));
-        goto err;
+        return -1;
     }
 
     oldroot = g_strdup_printf("%s/.oldroot", root->src->path);
@@ -713,7 +707,7 @@ static int lxcContainerPivotRoot(virDomainFSDefPtr root)
         virReportSystemError(errno,
                              _("Failed to create %s"),
                              oldroot);
-        goto err;
+        return -1;
     }
 
     /* Create a tmpfs root since old and new roots must be
@@ -722,7 +716,7 @@ static int lxcContainerPivotRoot(virDomainFSDefPtr root)
         virReportSystemError(errno,
                              _("Failed to mount empty tmpfs at %s"),
                              oldroot);
-        goto err;
+        return -1;
     }
 
     /* Create a directory called 'new' in tmpfs */
@@ -732,7 +726,7 @@ static int lxcContainerPivotRoot(virDomainFSDefPtr root)
         virReportSystemError(errno,
                              _("Failed to create %s"),
                              newroot);
-        goto err;
+        return -1;
     }
 
     /* ... and mount our root onto it */
@@ -740,7 +734,7 @@ static int lxcContainerPivotRoot(virDomainFSDefPtr root)
         virReportSystemError(errno,
                              _("Failed to bind %s to new root %s"),
                              root->src->path, newroot);
-        goto err;
+        return -1;
     }
 
     if (root->readonly) {
@@ -748,7 +742,7 @@ static int lxcContainerPivotRoot(virDomainFSDefPtr root)
             virReportSystemError(errno,
                                  _("Failed to make new root %s readonly"),
                                  root->src->path);
-            goto err;
+            return -1;
         }
     }
 
@@ -757,7 +751,7 @@ static int lxcContainerPivotRoot(virDomainFSDefPtr root)
     if (chdir(newroot) < 0) {
         virReportSystemError(errno,
                              _("Failed to chdir into %s"), newroot);
-        goto err;
+        return -1;
     }
 
     /* The old root directory will live at /.oldroot after
@@ -765,20 +759,14 @@ static int lxcContainerPivotRoot(virDomainFSDefPtr root)
     if (pivot_root(".", ".oldroot") < 0) {
         virReportSystemError(errno, "%s",
                              _("Failed to pivot root"));
-        goto err;
+        return -1;
     }
 
     /* CWD is undefined after pivot_root, so go to / */
     if (chdir("/") < 0)
-        goto err;
-
-    ret = 0;
-
- err:
-    VIR_FREE(oldroot);
-    VIR_FREE(newroot);
+        return -1;
 
-    return ret;
+    return 0;
 }
 
 
@@ -845,7 +833,7 @@ static int lxcContainerSetReadOnly(void)
         tmp = g_strdup(mntent.mnt_dir);
 
         if (VIR_APPEND_ELEMENT(mounts, nmounts, tmp) < 0) {
-            VIR_FREE(tmp);
+            g_free(tmp);
             goto cleanup;
         }
     }
@@ -881,13 +869,12 @@ static int lxcContainerMountBasicFS(bool userns_enabled,
                                     bool netns_disabled)
 {
     size_t i;
-    int rc = -1;
-    char* mnt_src = NULL;
     int mnt_mflags;
 
     VIR_DEBUG("Mounting basic filesystems");
 
     for (i = 0; i < G_N_ELEMENTS(lxcBasicMounts); i++) {
+        g_autofree char *mnt_src = NULL;
         bool bindOverReadonly;
         virLXCBasicMountInfo const *mnt = &lxcBasicMounts[i];
 
@@ -908,27 +895,23 @@ static int lxcContainerMountBasicFS(bool userns_enabled,
                   mnt_src, mnt->dst);
 
         if (mnt->skipUnmounted) {
-            char *hostdir;
             int ret;
-
-            hostdir = g_strdup_printf("/.oldroot%s", mnt->dst);
+            g_autofree char *hostdir = g_strdup_printf("/.oldroot%s",
+                                                       mnt->dst);
 
             ret = virFileIsMountPoint(hostdir);
-            VIR_FREE(hostdir);
             if (ret < 0)
-                goto cleanup;
+                return -1;
 
             if (ret == 0) {
                 VIR_DEBUG("Skipping '%s' which isn't mounted in host",
                           mnt->dst);
-                VIR_FREE(mnt_src);
                 continue;
             }
         }
 
         if (mnt->skipUserNS && userns_enabled) {
             VIR_DEBUG("Skipping due to user ns enablement");
-            VIR_FREE(mnt_src);
             continue;
         }
 
@@ -936,13 +919,11 @@ static int lxcContainerMountBasicFS(bool userns_enabled,
          * missing folder in /proc due to the absence of a kernel feature */
         if (STRPREFIX(mnt_src, "/") && !virFileExists(mnt_src)) {
             VIR_DEBUG("Skipping due to missing source: %s", mnt_src);
-            VIR_FREE(mnt_src);
             continue;
         }
 
         if (mnt->skipNoNetns && netns_disabled) {
             VIR_DEBUG("Skipping due to absence of network namespace");
-            VIR_FREE(mnt_src);
             continue;
         }
 
@@ -950,7 +931,7 @@ static int lxcContainerMountBasicFS(bool userns_enabled,
             virReportSystemError(errno,
                                  _("Failed to mkdir %s"),
                                  mnt->dst);
-            goto cleanup;
+            return -1;
         }
 
         /*
@@ -969,7 +950,7 @@ static int lxcContainerMountBasicFS(bool userns_enabled,
                                  _("Failed to mount %s on %s type %s flags=0x%x"),
                                  mnt_src, mnt->dst, NULLSTR(mnt->type),
                                  mnt_mflags & ~MS_RDONLY);
-            goto cleanup;
+            return -1;
         }
 
         if (bindOverReadonly &&
@@ -979,26 +960,18 @@ static int lxcContainerMountBasicFS(bool userns_enabled,
                                  _("Failed to re-mount %s on %s flags=0x%x"),
                                  mnt_src, mnt->dst,
                                  MS_BIND|MS_REMOUNT|MS_RDONLY);
-            goto cleanup;
+            return -1;
         }
-
-        VIR_FREE(mnt_src);
     }
 
-    rc = 0;
-
- cleanup:
-    VIR_FREE(mnt_src);
-    VIR_DEBUG("rc=%d", rc);
-    return rc;
+    return 0;
 }
 
 #if WITH_FUSE
 static int lxcContainerMountProcFuse(virDomainDefPtr def,
                                      const char *stateDir)
 {
-    int ret;
-    char *meminfo_path = NULL;
+    g_autofree char *meminfo_path = NULL;
 
     VIR_DEBUG("Mount /proc/meminfo stateDir=%s", stateDir);
 
@@ -1006,15 +979,15 @@ static int lxcContainerMountProcFuse(virDomainDefPtr def,
                                    stateDir,
                                    def->name);
 
-    if ((ret = mount(meminfo_path, "/proc/meminfo",
-                     NULL, MS_BIND, NULL)) < 0) {
+    if (mount(meminfo_path, "/proc/meminfo",
+              NULL, MS_BIND, NULL) < 0) {
         virReportSystemError(errno,
                              _("Failed to mount %s on /proc/meminfo"),
                              meminfo_path);
+        return -1;
     }
 
-    VIR_FREE(meminfo_path);
-    return ret;
+    return 0;
 }
 #else
 static int lxcContainerMountProcFuse(virDomainDefPtr def G_GNUC_UNUSED,
@@ -1027,8 +1000,7 @@ static int lxcContainerMountProcFuse(virDomainDefPtr def G_GNUC_UNUSED,
 static int lxcContainerMountFSDev(virDomainDefPtr def,
                                   const char *stateDir)
 {
-    int ret = -1;
-    char *path = NULL;
+    g_autofree char *path = NULL;
     int flags = def->idmap.nuidmap ? MS_BIND : MS_MOVE;
 
     VIR_DEBUG("Mount /dev/ stateDir=%s", stateDir);
@@ -1038,7 +1010,7 @@ static int lxcContainerMountFSDev(virDomainDefPtr def,
     if (virFileMakePath("/dev") < 0) {
         virReportSystemError(errno, "%s",
                              _("Cannot create /dev"));
-        goto cleanup;
+        return -1;
     }
 
     VIR_DEBUG("Trying to %s %s to /dev", def->idmap.nuidmap ?
@@ -1048,21 +1020,16 @@ static int lxcContainerMountFSDev(virDomainDefPtr def,
         virReportSystemError(errno,
                              _("Failed to mount %s on /dev"),
                              path);
-        goto cleanup;
+        return -1;
     }
 
-    ret = 0;
-
- cleanup:
-    VIR_FREE(path);
-    return ret;
+    return 0;
 }
 
 static int lxcContainerMountFSDevPTS(virDomainDefPtr def,
                                      const char *stateDir)
 {
-    int ret = -1;
-    char *path = NULL;
+    g_autofree char *path = NULL;
     int flags = def->idmap.nuidmap ? MS_BIND : MS_MOVE;
 
     VIR_DEBUG("Mount /dev/pts stateDir=%s", stateDir);
@@ -1072,7 +1039,7 @@ static int lxcContainerMountFSDevPTS(virDomainDefPtr def,
     if (virFileMakePath("/dev/pts") < 0) {
         virReportSystemError(errno, "%s",
                              _("Cannot create /dev/pts"));
-        goto cleanup;
+        return -1;
     }
 
     VIR_DEBUG("Trying to %s %s to /dev/pts", def->idmap.nuidmap ?
@@ -1082,13 +1049,10 @@ static int lxcContainerMountFSDevPTS(virDomainDefPtr def,
         virReportSystemError(errno,
                              _("Failed to mount %s on /dev/pts"),
                              path);
-        goto cleanup;
+        return -1;
     }
 
-    ret = 0;
- cleanup:
-    VIR_FREE(path);
-    return ret;
+    return 0;
 }
 
 static int lxcContainerSetupDevices(char **ttyPaths, size_t nttyPaths)
@@ -1118,15 +1082,10 @@ static int lxcContainerSetupDevices(char **ttyPaths, size_t nttyPaths)
         return -1;
 
     for (i = 0; i < nttyPaths; i++) {
-        char *tty;
-        tty = g_strdup_printf("/dev/tty%zu", i + 1);
+        g_autofree char *tty = g_strdup_printf("/dev/tty%zu", i + 1);
 
-        if (virFileBindMountDevice(ttyPaths[i], tty) < 0) {
-            VIR_FREE(tty);
+        if (virFileBindMountDevice(ttyPaths[i], tty) < 0)
             return -1;
-        }
-
-        VIR_FREE(tty);
 
         if (i == 0 &&
             virFileBindMountDevice(ttyPaths[i], "/dev/console") < 0)
@@ -1139,8 +1098,7 @@ static int lxcContainerSetupDevices(char **ttyPaths, size_t nttyPaths)
 static int lxcContainerMountFSBind(virDomainFSDefPtr fs,
                                    const char *srcprefix)
 {
-    char *src = NULL;
-    int ret = -1;
+    g_autofree char *src = NULL;
     struct stat st;
 
     VIR_DEBUG("src=%s dst=%s", fs->src->path, fs->dst);
@@ -1151,20 +1109,20 @@ static int lxcContainerMountFSBind(virDomainFSDefPtr fs,
         if (errno != ENOENT) {
             virReportSystemError(errno, _("Unable to stat bind target %s"),
                                  fs->dst);
-            goto cleanup;
+            return -1;
         }
         /* ENOENT => create the target dir or file */
         if (stat(src, &st) < 0) {
             virReportSystemError(errno, _("Unable to stat bind source %s"),
                                  src);
-            goto cleanup;
+            return -1;
         }
         if (S_ISDIR(st.st_mode)) {
             if (virFileMakePath(fs->dst) < 0) {
                 virReportSystemError(errno,
                                      _("Failed to create %s"),
                                      fs->dst);
-                goto cleanup;
+                return -1;
             }
         } else {
             /* Create Empty file for target mount point */
@@ -1174,14 +1132,14 @@ static int lxcContainerMountFSBind(virDomainFSDefPtr fs,
                     virReportSystemError(errno,
                                          _("Failed to create bind target %s"),
                                          fs->dst);
-                    goto cleanup;
+                    return -1;
                 }
             }
             if (VIR_CLOSE(fd) < 0) {
                 virReportSystemError(errno,
                                      _("Failed to close bind target %s"),
                                      fs->dst);
-                goto cleanup;
+                return -1;
             }
         }
     }
@@ -1190,7 +1148,7 @@ static int lxcContainerMountFSBind(virDomainFSDefPtr fs,
         virReportSystemError(errno,
                              _("Failed to bind mount directory %s to %s"),
                              src, fs->dst);
-        goto cleanup;
+        return -1;
     }
 
     if (fs->readonly) {
@@ -1202,11 +1160,7 @@ static int lxcContainerMountFSBind(virDomainFSDefPtr fs,
         }
     }
 
-    ret = 0;
-
- cleanup:
-    VIR_FREE(src);
-    return ret;
+    return 0;
 }
 
 
@@ -1307,14 +1261,14 @@ static int lxcContainerMountFSBlockAuto(virDomainFSDefPtr fs,
     int ret = -1;
     bool tryProc = false;
     bool gotStar = false;
-    char *fslist = NULL;
-    char *line = NULL;
+    g_autofree char *fslist = NULL;
     const char *type;
 
     VIR_DEBUG("src=%s dst=%s srcprefix=%s", src, fs->dst, srcprefix);
 
     /* First time around we use /etc/filesystems */
  retry:
+    g_free(fslist);
     fslist = g_strdup_printf("%s%s", srcprefix,
                              tryProc ? "/proc/filesystems" : "/etc/filesystems");
 
@@ -1326,7 +1280,6 @@ static int lxcContainerMountFSBlockAuto(virDomainFSDefPtr fs,
         if (errno == ENOENT &&
             !tryProc) {
             tryProc = true;
-            VIR_FREE(fslist);
             goto retry;
         }
 
@@ -1337,8 +1290,8 @@ static int lxcContainerMountFSBlockAuto(virDomainFSDefPtr fs,
     }
 
     while (!feof(fp)) {
+        g_autofree char *line = NULL;
         size_t n;
-        VIR_FREE(line);
         if (getline(&line, &n, fp) <= 0) {
             if (feof(fp))
                 break;
@@ -1401,7 +1354,6 @@ static int lxcContainerMountFSBlockAuto(virDomainFSDefPtr fs,
         !tryProc &&
         gotStar) {
         tryProc = true;
-        VIR_FREE(fslist);
         VIR_FORCE_FCLOSE(fp);
         goto retry;
     }
@@ -1415,8 +1367,6 @@ static int lxcContainerMountFSBlockAuto(virDomainFSDefPtr fs,
     VIR_DEBUG("Done mounting filesystem ret=%d tryProc=%d", ret, tryProc);
 
  cleanup:
-    VIR_FREE(line);
-    VIR_FREE(fslist);
     VIR_FORCE_FCLOSE(fp);
     return ret;
 }
@@ -1432,8 +1382,7 @@ static int lxcContainerMountFSBlockHelper(virDomainFSDefPtr fs,
                                           const char *sec_mount_options)
 {
     int fsflags = 0;
-    int ret = -1;
-    char *format = NULL;
+    g_autofree char *format = NULL;
 
     if (fs->readonly)
         fsflags |= MS_RDONLY;
@@ -1442,11 +1391,11 @@ static int lxcContainerMountFSBlockHelper(virDomainFSDefPtr fs,
         virReportSystemError(errno,
                              _("Failed to create %s"),
                              fs->dst);
-        goto cleanup;
+        return -1;
     }
 
     if (lxcContainerMountDetectFilesystem(src, &format) < 0)
-        goto cleanup;
+        return -1;
 
     if (format) {
         VIR_DEBUG("Mount '%s' on '%s' with detected format '%s' opts '%s'",
@@ -1455,16 +1404,12 @@ static int lxcContainerMountFSBlockHelper(virDomainFSDefPtr fs,
             virReportSystemError(errno,
                                  _("Failed to mount device %s to %s as %s"),
                                  src, fs->dst, format);
-            goto cleanup;
+            return -1;
         }
-        ret = 0;
+        return 0;
     } else {
-        ret = lxcContainerMountFSBlockAuto(fs, fsflags, src, srcprefix, sec_mount_options);
+        return lxcContainerMountFSBlockAuto(fs, fsflags, src, srcprefix, sec_mount_options);
     }
-
- cleanup:
-    VIR_FREE(format);
-    return ret;
 }
 
 
@@ -1472,7 +1417,7 @@ static int lxcContainerMountFSBlock(virDomainFSDefPtr fs,
                                     const char *srcprefix,
                                     const char *sec_mount_options)
 {
-    char *src = NULL;
+    g_autofree char *src = NULL;
     int ret = -1;
 
     VIR_DEBUG("src=%s dst=%s", fs->src->path, fs->dst);
@@ -1483,7 +1428,6 @@ static int lxcContainerMountFSBlock(virDomainFSDefPtr fs,
 
     VIR_DEBUG("Done mounting filesystem ret=%d", ret);
 
-    VIR_FREE(src);
     return ret;
 }
 
@@ -1491,8 +1435,7 @@ static int lxcContainerMountFSBlock(virDomainFSDefPtr fs,
 static int lxcContainerMountFSTmpfs(virDomainFSDefPtr fs,
                                     char *sec_mount_options)
 {
-    int ret = -1;
-    char *data = NULL;
+    g_autofree char *data = NULL;
 
     VIR_DEBUG("usage=%lld sec=%s", fs->usage, sec_mount_options);
 
@@ -1502,14 +1445,14 @@ static int lxcContainerMountFSTmpfs(virDomainFSDefPtr fs,
         virReportSystemError(errno,
                              _("Failed to create %s"),
                              fs->dst);
-        goto cleanup;
+        return -1;
     }
 
     if (mount("tmpfs", fs->dst, "tmpfs", MS_NOSUID|MS_NODEV, data) < 0) {
         virReportSystemError(errno,
                              _("Failed to mount directory %s as tmpfs"),
                              fs->dst);
-        goto cleanup;
+        return -1;
     }
 
     if (fs->readonly) {
@@ -1518,15 +1461,11 @@ static int lxcContainerMountFSTmpfs(virDomainFSDefPtr fs,
             virReportSystemError(errno,
                                  _("Failed to make directory %s readonly"),
                                  fs->dst);
-            goto cleanup;
+            return -1;
         }
     }
 
-    ret = 0;
-
- cleanup:
-    VIR_FREE(data);
-    return ret;
+    return 0;
 }
 
 
@@ -1600,8 +1539,8 @@ static int lxcContainerMountAllFS(virDomainDefPtr vmDef,
 
 int lxcContainerSetupHostdevCapsMakePath(const char *dev)
 {
-    int ret = -1;
-    char *dir, *tmp;
+    g_autofree char *dir = NULL;
+    char *tmp;
 
     dir = g_strdup(dev);
 
@@ -1611,30 +1550,25 @@ int lxcContainerSetupHostdevCapsMakePath(const char *dev)
             virReportSystemError(errno,
                                  _("Failed to create directory for '%s' dev '%s'"),
                                  dir, dev);
-            goto cleanup;
+            return -1;
         }
     }
 
-    ret = 0;
-
- cleanup:
-    VIR_FREE(dir);
-    return ret;
+    return 0;
 }
 
 
 static int lxcContainerUnmountForSharedRoot(const char *stateDir,
                                             const char *domain)
 {
-    int ret = -1;
-    char *tmp = NULL;
+    g_autofree char *tmp = NULL;
 
 #if WITH_SELINUX
     /* Some versions of Linux kernel don't let you overmount
      * the selinux filesystem, so make sure we kill it first
      */
     if (lxcContainerUnmountSubtree(SELINUX_MOUNT, false) < 0)
-        goto cleanup;
+        return -1;
 #endif
 
     /* These filesystems are created by libvirt temporarily, they
@@ -1642,20 +1576,20 @@ static int lxcContainerUnmountForSharedRoot(const char *stateDir,
     tmp = g_strdup_printf("%s/%s.dev", stateDir, domain);
 
     if (lxcContainerUnmountSubtree(tmp, false) < 0)
-        goto cleanup;
+        return -1;
 
-    VIR_FREE(tmp);
+    g_free(tmp);
     tmp = g_strdup_printf("%s/%s.devpts", stateDir, domain);
 
     if (lxcContainerUnmountSubtree(tmp, false) < 0)
-        goto cleanup;
+        return -1;
 
 #if WITH_FUSE
-    VIR_FREE(tmp);
+    g_free(tmp);
     tmp = g_strdup_printf("%s/%s.fuse", stateDir, domain);
 
     if (lxcContainerUnmountSubtree(tmp, false) < 0)
-        goto cleanup;
+        return -1;
 #endif
 
     /* If we have the root source being '/', then we need to
@@ -1665,13 +1599,9 @@ static int lxcContainerUnmountForSharedRoot(const char *stateDir,
     if (lxcContainerUnmountSubtree("/sys", false) < 0 ||
         lxcContainerUnmountSubtree("/dev", false) < 0 ||
         lxcContainerUnmountSubtree("/proc", false) < 0)
-        goto cleanup;
-
-    ret = 0;
+        return -1;
 
- cleanup:
-    VIR_FREE(tmp);
-    return ret;
+    return 0;
 }
 
 
@@ -1704,8 +1634,8 @@ static int lxcContainerSetupPivotRoot(virDomainDefPtr vmDef,
 {
     virCgroupPtr cgroup = NULL;
     int ret = -1;
-    char *sec_mount_options;
-    char *stateDir = NULL;
+    g_autofree char *sec_mount_options = NULL;
+    g_autofree char *stateDir = NULL;
 
     VIR_DEBUG("Setup pivot root");
 
@@ -1776,9 +1706,7 @@ static int lxcContainerSetupPivotRoot(virDomainDefPtr vmDef,
     ret = 0;
 
  cleanup:
-    VIR_FREE(stateDir);
     virCgroupFree(&cgroup);
-    VIR_FREE(sec_mount_options);
     return ret;
 }
 
@@ -2128,9 +2056,8 @@ static const char hostname_validchars[] =
 
 static int lxcContainerSetHostname(virDomainDefPtr def)
 {
-    int ret = -1;
-    char *name = NULL;
-    char *hostname = NULL;
+    g_autofree char *name = NULL;
+    const char *hostname = NULL;
 
     /* Filter the VM name to get a valid hostname */
     name = g_strdup(def->name);
@@ -2143,13 +2070,10 @@ static int lxcContainerSetHostname(virDomainDefPtr def)
 
     if (sethostname(hostname, strlen(hostname)) < 0) {
         virReportSystemError(errno, "%s", _("Failed to set hostname"));
-        goto cleanup;
+        return -1;
     }
-    ret = 0;
 
- cleanup:
-    VIR_FREE(name);
-    return ret;
+    return 0;
 }
 
 /**
@@ -2170,11 +2094,11 @@ static int lxcContainerChild(void *data)
     virDomainDefPtr vmDef = argv->config;
     int ttyfd = -1;
     int ret = -1;
-    char *ttyPath = NULL;
+    g_autofree char *ttyPath = NULL;
     virDomainFSDefPtr root;
     virCommandPtr cmd = NULL;
     int hasReboot;
-    gid_t *groups = NULL;
+    g_autofree gid_t *groups = NULL;
     int ngroups;
 
     if (NULL == vmDef) {
@@ -2303,7 +2227,6 @@ static int lxcContainerChild(void *data)
 
     ret = 0;
  cleanup:
-    VIR_FREE(ttyPath);
     VIR_FORCE_CLOSE(ttyfd);
     VIR_FORCE_CLOSE(argv->monitor);
     VIR_FORCE_CLOSE(argv->handshakefd);
@@ -2321,7 +2244,6 @@ static int lxcContainerChild(void *data)
                 virGetLastErrorMessage());
     }
 
-    VIR_FREE(groups);
     virCommandFree(cmd);
     return ret;
 }
@@ -2383,7 +2305,8 @@ int lxcContainerStart(virDomainDefPtr def,
     pid_t pid;
     int cflags;
     int stacksize = getpagesize() * 4;
-    char *stack, *stacktop;
+    g_autofree char *stack = NULL;
+    char *stacktop;
     lxc_child_argv_t args = {
         .config = def,
         .securityDriver = securityDriver,
@@ -2410,7 +2333,6 @@ int lxcContainerStart(virDomainDefPtr def,
         if (virProcessNamespaceAvailable(VIR_PROCESS_NAMESPACE_USER) < 0) {
             virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                            _("Kernel doesn't support user namespace"));
-            VIR_FREE(stack);
             return -1;
         }
         VIR_DEBUG("Enable user namespace");
@@ -2426,7 +2348,6 @@ int lxcContainerStart(virDomainDefPtr def,
             virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                            _("Config asks for inherit net namespace "
                              "as well as private network interfaces"));
-            VIR_FREE(stack);
             return -1;
         }
         VIR_DEBUG("Inheriting a net namespace");
@@ -2446,7 +2367,6 @@ int lxcContainerStart(virDomainDefPtr def,
 
     VIR_DEBUG("Cloning container init process");
     pid = clone(lxcContainerChild, stacktop, cflags, &args);
-    VIR_FREE(stack);
     VIR_DEBUG("clone() completed, new container PID is %d", pid);
 
     if (pid < 0) {
diff --git a/src/lxc/lxc_controller.c b/src/lxc/lxc_controller.c
index 453b435dd6..0438d72538 100644
--- a/src/lxc/lxc_controller.c
+++ b/src/lxc/lxc_controller.c
@@ -191,7 +191,7 @@ static virLXCControllerPtr virLXCControllerNew(const char *name)
 {
     virLXCControllerPtr ctrl = NULL;
     virLXCDriverPtr driver = NULL;
-    char *configFile = NULL;
+    g_autofree char *configFile = NULL;
 
     if (VIR_ALLOC(ctrl) < 0)
         goto error;
@@ -220,7 +220,6 @@ static virLXCControllerPtr virLXCControllerNew(const char *name)
         goto error;
 
  cleanup:
-    VIR_FREE(configFile);
     virLXCControllerDriverFree(driver);
     return ctrl;
 
@@ -288,22 +287,22 @@ static void virLXCControllerFree(virLXCControllerPtr ctrl)
     virObjectUnref(ctrl->securityManager);
 
     for (i = 0; i < ctrl->nveths; i++)
-        VIR_FREE(ctrl->veths[i]);
-    VIR_FREE(ctrl->veths);
-    VIR_FREE(ctrl->nicindexes);
+        g_free(ctrl->veths[i]);
+    g_free(ctrl->veths);
+    g_free(ctrl->nicindexes);
 
     for (i = 0; i < ctrl->npassFDs; i++)
         VIR_FORCE_CLOSE(ctrl->passFDs[i]);
-    VIR_FREE(ctrl->passFDs);
+    g_free(ctrl->passFDs);
 
     for (i = 0; i < ctrl->nconsoles; i++)
         virLXCControllerConsoleClose(&(ctrl->consoles[i]));
-    VIR_FREE(ctrl->consoles);
+    g_free(ctrl->consoles);
 
-    VIR_FREE(ctrl->devptmx);
+    g_free(ctrl->devptmx);
 
     virDomainObjEndAPI(&ctrl->vm);
-    VIR_FREE(ctrl->name);
+    g_free(ctrl->name);
 
     if (ctrl->timerShutdown != -1)
         virEventRemoveTimeout(ctrl->timerShutdown);
@@ -311,14 +310,14 @@ static void virLXCControllerFree(virLXCControllerPtr ctrl)
     virObjectUnref(ctrl->daemon);
     virLXCControllerFreeFuse(ctrl);
 
-    VIR_FREE(ctrl->nbdpids);
+    g_free(ctrl->nbdpids);
 
-    VIR_FREE(ctrl->nsFDs);
+    g_free(ctrl->nsFDs);
     virCgroupFree(&ctrl->cgroup);
 
     /* This must always be the last thing to be closed */
     VIR_FORCE_CLOSE(ctrl->handshakeFd);
-    VIR_FREE(ctrl);
+    g_free(ctrl);
 }
 
 
@@ -469,7 +468,7 @@ static int virLXCControllerSetupLoopDeviceFS(virDomainFSDefPtr fs)
      * the rest of container setup 'just works'
      */
     fs->type = VIR_DOMAIN_FS_TYPE_BLOCK;
-    VIR_FREE(fs->src->path);
+    g_free(fs->src->path);
     fs->src->path = loname;
     loname = NULL;
 
@@ -480,7 +479,7 @@ static int virLXCControllerSetupLoopDeviceFS(virDomainFSDefPtr fs)
 static int virLXCControllerSetupLoopDeviceDisk(virDomainDiskDefPtr disk)
 {
     int lofd;
-    char *loname = NULL;
+    g_autofree char *loname = NULL;
     const char *src = virDomainDiskGetSource(disk);
     int ret = -1;
 
@@ -501,7 +500,6 @@ static int virLXCControllerSetupLoopDeviceDisk(virDomainDiskDefPtr disk)
     ret = 0;
 
  cleanup:
-    VIR_FREE(loname);
     if (ret < 0)
         VIR_FORCE_CLOSE(lofd);
 
@@ -533,7 +531,7 @@ static int virLXCControllerSetupNBDDeviceFS(virDomainFSDefPtr fs)
      * the rest of container setup 'just works'
      */
     fs->type = VIR_DOMAIN_FS_TYPE_BLOCK;
-    VIR_FREE(fs->src->path);
+    g_free(fs->src->path);
     fs->src->path = dev;
 
     return 0;
@@ -542,7 +540,7 @@ static int virLXCControllerSetupNBDDeviceFS(virDomainFSDefPtr fs)
 
 static int virLXCControllerSetupNBDDeviceDisk(virDomainDiskDefPtr disk)
 {
-    char *dev;
+    g_autofree char *dev = NULL;
     const char *src = virDomainDiskGetSource(disk);
     int format = virDomainDiskGetFormat(disk);
 
@@ -565,11 +563,8 @@ static int virLXCControllerSetupNBDDeviceDisk(virDomainDiskDefPtr disk)
      * the rest of container setup 'just works'
      */
     virDomainDiskSetType(disk, VIR_STORAGE_TYPE_BLOCK);
-    if (virDomainDiskSetSource(disk, dev) < 0) {
-        VIR_FREE(dev);
+    if (virDomainDiskSetSource(disk, dev) < 0)
         return -1;
-    }
-    VIR_FREE(dev);
 
     return 0;
 }
@@ -577,16 +572,15 @@ static int virLXCControllerSetupNBDDeviceDisk(virDomainDiskDefPtr disk)
 static int virLXCControllerAppendNBDPids(virLXCControllerPtr ctrl,
                                          const char *dev)
 {
-    char *pidpath = NULL;
-    pid_t *pids = NULL;
+    g_autofree char *pidpath = NULL;
+    g_autofree pid_t *pids = NULL;
     size_t npids = 0;
     size_t i;
-    int ret = -1;
     size_t loops = 0;
     pid_t pid;
 
     if (!STRPREFIX(dev, "/dev/"))
-        goto cleanup;
+        return -1;
 
     pidpath = g_strdup_printf("/sys/devices/virtual/block/%s/pid", dev + 5);
 
@@ -600,27 +594,22 @@ static int virLXCControllerAppendNBDPids(virLXCControllerPtr ctrl,
             virReportSystemError(errno,
                                  _("Cannot check NBD device %s pid"),
                                  dev + 5);
-            goto cleanup;
+            return -1;
         }
     }
 
     if (virPidFileReadPath(pidpath, &pid) < 0)
-        goto cleanup;
+        return -1;
 
     if (virProcessGetPids(pid, &npids, &pids) < 0)
-        goto cleanup;
+        return -1;
 
     for (i = 0; i < npids; i++) {
         if (VIR_APPEND_ELEMENT(ctrl->nbdpids, ctrl->nnbdpids, pids[i]) < 0)
-            goto cleanup;
+            return -1;
     }
 
-    ret = 0;
-
- cleanup:
-    VIR_FREE(pids);
-    VIR_FREE(pidpath);
-    return ret;
+    return 0;
 }
 
 static int virLXCControllerSetupLoopDevices(virLXCControllerPtr ctrl)
@@ -957,7 +946,7 @@ static int virLXCControllerSetupServer(virLXCControllerPtr ctrl)
 {
     virNetServerPtr srv = NULL;
     virNetServerServicePtr svc = NULL;
-    char *sockpath;
+    g_autofree char *sockpath = NULL;
 
     sockpath = g_strdup_printf("%s/%s.sock", LXC_STATE_DIR, ctrl->name);
 
@@ -1002,11 +991,9 @@ static int virLXCControllerSetupServer(virLXCControllerPtr ctrl)
         goto error;
 
     virNetDaemonUpdateServices(ctrl->daemon, true);
-    VIR_FREE(sockpath);
     return 0;
 
  error:
-    VIR_FREE(sockpath);
     virObjectUnref(srv);
     virObjectUnref(ctrl->daemon);
     ctrl->daemon = NULL;
@@ -1606,11 +1593,10 @@ virLXCControllerSetupHostdevSubsysUSB(virDomainDefPtr vmDef,
                                       virDomainHostdevDefPtr def,
                                       virSecurityManagerPtr securityDriver)
 {
-    int ret = -1;
-    char *src = NULL;
-    char *dstdir = NULL;
-    char *dstfile = NULL;
-    char *vroot = NULL;
+    g_autofree char *src = NULL;
+    g_autofree char *dstdir = NULL;
+    g_autofree char *dstfile = NULL;
+    g_autofree char *vroot = NULL;
     struct stat sb;
     mode_t mode;
     virDomainHostdevSubsysUSBPtr usbsrc = &def->source.subsys.u.usb;
@@ -1626,14 +1612,14 @@ virLXCControllerSetupHostdevSubsysUSB(virDomainDefPtr vmDef,
     if (stat(src, &sb) < 0) {
         virReportSystemError(errno,
                              _("Unable to access %s"), src);
-        goto cleanup;
+        return -1;
     }
 
     if (!S_ISCHR(sb.st_mode)) {
         virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                        _("USB source %s was not a character device"),
                        src);
-        goto cleanup;
+        return -1;
     }
 
     mode = 0700 | S_IFCHR;
@@ -1641,7 +1627,7 @@ virLXCControllerSetupHostdevSubsysUSB(virDomainDefPtr vmDef,
     if (virFileMakePath(dstdir) < 0) {
         virReportSystemError(errno,
                              _("Unable to create %s"), dstdir);
-        goto cleanup;
+        return -1;
     }
 
     VIR_DEBUG("Creating dev %s (%d,%d)",
@@ -1650,24 +1636,17 @@ virLXCControllerSetupHostdevSubsysUSB(virDomainDefPtr vmDef,
         virReportSystemError(errno,
                              _("Unable to create device %s"),
                              dstfile);
-        goto cleanup;
+        return -1;
     }
 
     if (lxcContainerChown(vmDef, dstfile) < 0)
-        goto cleanup;
+        return -1;
 
     if (virSecurityManagerSetHostdevLabel(securityDriver,
                                           vmDef, def, vroot) < 0)
-        goto cleanup;
-
-    ret = 0;
+        return -1;
 
- cleanup:
-    VIR_FREE(src);
-    VIR_FREE(dstfile);
-    VIR_FREE(dstdir);
-    VIR_FREE(vroot);
-    return ret;
+    return 0;
 }
 
 
@@ -1676,8 +1655,8 @@ virLXCControllerSetupHostdevCapsStorage(virDomainDefPtr vmDef,
                                         virDomainHostdevDefPtr def,
                                         virSecurityManagerPtr securityDriver)
 {
-    char *dst = NULL;
-    char *path = NULL;
+    g_autofree char *dst = NULL;
+    g_autofree char *path = NULL;
     int len = 0;
     int ret = -1;
     struct stat sb;
@@ -1741,8 +1720,6 @@ virLXCControllerSetupHostdevCapsStorage(virDomainDefPtr vmDef,
 
  cleanup:
     def->source.caps.u.storage.block = dev;
-    VIR_FREE(dst);
-    VIR_FREE(path);
     return ret;
 }
 
@@ -1752,8 +1729,8 @@ virLXCControllerSetupHostdevCapsMisc(virDomainDefPtr vmDef,
                                      virDomainHostdevDefPtr def,
                                      virSecurityManagerPtr securityDriver)
 {
-    char *dst = NULL;
-    char *path = NULL;
+    g_autofree char *dst = NULL;
+    g_autofree char *path = NULL;
     int len = 0;
     int ret = -1;
     struct stat sb;
@@ -1817,8 +1794,6 @@ virLXCControllerSetupHostdevCapsMisc(virDomainDefPtr vmDef,
 
  cleanup:
     def->source.caps.u.misc.chardev = dev;
-    VIR_FREE(dst);
-    VIR_FREE(path);
     return ret;
 }
 
@@ -1910,7 +1885,7 @@ static int virLXCControllerSetupDisk(virLXCControllerPtr ctrl,
                                      virDomainDiskDefPtr def,
                                      virSecurityManagerPtr securityDriver)
 {
-    char *dst = NULL;
+    g_autofree char *dst = NULL;
     int ret = -1;
     struct stat sb;
     mode_t mode;
@@ -1979,7 +1954,6 @@ static int virLXCControllerSetupDisk(virLXCControllerPtr ctrl,
 
  cleanup:
     def->src->path = tmpsrc;
-    VIR_FREE(dst);
     return ret;
 }
 
@@ -2125,7 +2099,8 @@ lxcCreateTty(virLXCControllerPtr ctrl, int *ttymaster,
  cleanup:
     if (ret != 0) {
         VIR_FORCE_CLOSE(*ttymaster);
-        VIR_FREE(*ttyName);
+        g_free(*ttyName);
+        *ttyName = NULL;
     }
 
     return ret;
@@ -2233,9 +2208,10 @@ virLXCControllerSetupConsoles(virLXCControllerPtr ctrl,
                               char **containerTTYPaths)
 {
     size_t i;
-    g_autofree char *ttyHostPath = NULL;
 
     for (i = 0; i < ctrl->nconsoles; i++) {
+        g_autofree char *ttyHostPath = NULL;
+
         VIR_DEBUG("Opening tty on private %s", ctrl->devptmx);
         if (lxcCreateTty(ctrl,
                          &ctrl->consoles[i].contFd,
@@ -2248,8 +2224,6 @@ virLXCControllerSetupConsoles(virLXCControllerPtr ctrl,
         /* Change the owner of tty device to the root user of container */
         if (lxcContainerChown(ctrl->def, ttyHostPath) < 0)
             return -1;
-
-        VIR_FREE(ttyHostPath);
     }
 
     return 0;
@@ -2484,8 +2458,8 @@ virLXCControllerRun(virLXCControllerPtr ctrl)
     VIR_FORCE_CLOSE(containerhandshake[1]);
 
     for (i = 0; i < ctrl->nconsoles; i++)
-        VIR_FREE(containerTTYPaths[i]);
-    VIR_FREE(containerTTYPaths);
+        g_free(containerTTYPaths[i]);
+    g_free(containerTTYPaths);
 
     virLXCControllerStopInit(ctrl);
 
@@ -2517,9 +2491,9 @@ int main(int argc, char *argv[])
         { "help", 0, NULL, 'h' },
         { 0, 0, 0, 0 },
     };
-    int *ttyFDs = NULL;
+    g_autofree int *ttyFDs = NULL;
     size_t nttyFDs = 0;
-    int *passFDs = NULL;
+    g_autofree int *passFDs = NULL;
     size_t npassFDs = 0;
     virLXCControllerPtr ctrl = NULL;
     size_t i;
@@ -2760,10 +2734,8 @@ int main(int argc, char *argv[])
         virLXCControllerDeleteInterfaces(ctrl);
     for (i = 0; i < nttyFDs; i++)
         VIR_FORCE_CLOSE(ttyFDs[i]);
-    VIR_FREE(ttyFDs);
     for (i = 0; i < npassFDs; i++)
         VIR_FORCE_CLOSE(passFDs[i]);
-    VIR_FREE(passFDs);
 
     virLXCControllerFree(ctrl);
 
diff --git a/src/lxc/lxc_domain.c b/src/lxc/lxc_domain.c
index 59f803837a..b9c3b4eec3 100644
--- a/src/lxc/lxc_domain.c
+++ b/src/lxc/lxc_domain.c
@@ -159,7 +159,7 @@ virLXCDomainObjPrivateAlloc(void *opaque G_GNUC_UNUSED)
         return NULL;
 
     if (virLXCDomainObjInitJob(priv) < 0) {
-        VIR_FREE(priv);
+        g_free(priv);
         return NULL;
     }
 
@@ -174,7 +174,7 @@ virLXCDomainObjPrivateFree(void *data)
 
     virCgroupFree(&priv->cgroup);
     virLXCDomainObjFreeJob(priv);
-    VIR_FREE(priv);
+    g_free(priv);
 }
 
 
@@ -200,8 +200,8 @@ lxcDomainDefNamespaceFree(void *nsdata)
     size_t i;
     lxcDomainDefPtr lxcDef = nsdata;
     for (i = 0; i < VIR_LXC_DOMAIN_NAMESPACE_LAST; i++)
-        VIR_FREE(lxcDef->ns_val[i]);
-    VIR_FREE(nsdata);
+        g_free(lxcDef->ns_val[i]);
+    g_free(nsdata);
 }
 
 static int
@@ -209,12 +209,11 @@ lxcDomainDefNamespaceParse(xmlXPathContextPtr ctxt,
                            void **data)
 {
     lxcDomainDefPtr lxcDef = NULL;
-    xmlNodePtr *nodes = NULL;
+    g_autofree xmlNodePtr *nodes = NULL;
     bool uses_lxc_ns = false;
     xmlNodePtr node;
     int feature;
     int n;
-    char *tmp = NULL;
     size_t i;
 
     if (VIR_ALLOC(lxcDef) < 0)
@@ -226,6 +225,7 @@ lxcDomainDefNamespaceParse(xmlXPathContextPtr ctxt,
     uses_lxc_ns |= n > 0;
 
     for (i = 0; i < n; i++) {
+        g_autofree char *tmp = NULL;
         if ((feature = virLXCDomainNamespaceTypeFromString(
                  (const char *)nodes[i]->name)) < 0) {
             virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
@@ -246,10 +246,8 @@ lxcDomainDefNamespaceParse(xmlXPathContextPtr ctxt,
             virReportError(VIR_ERR_INTERNAL_ERROR,
                            _("Unknown LXC namespace source '%s'"),
                            tmp);
-            VIR_FREE(tmp);
             goto error;
         }
-        VIR_FREE(tmp);
 
         if (!(lxcDef->ns_val[feature] =
               virXMLPropString(nodes[i], "value"))) {
@@ -258,15 +256,13 @@ lxcDomainDefNamespaceParse(xmlXPathContextPtr ctxt,
             goto error;
         }
     }
-    VIR_FREE(nodes);
     ctxt->node = node;
     if (uses_lxc_ns)
         *data = lxcDef;
     else
-        VIR_FREE(lxcDef);
+        g_free(lxcDef);
     return 0;
  error:
-    VIR_FREE(nodes);
     lxcDomainDefNamespaceFree(lxcDef);
     return -1;
 }
@@ -496,7 +492,9 @@ virLXCDomainSetRunlevel(virDomainObjPtr vm,
                                         lxcDomainInitctlCallback,
                                         &data);
  cleanup:
-    VIR_FREE(data.st);
-    VIR_FREE(data.st_valid);
+    g_free(data.st);
+    data.st = NULL;
+    g_free(data.st_valid);
+    data.st_valid = NULL;
     return ret;
 }
diff --git a/src/lxc/lxc_driver.c b/src/lxc/lxc_driver.c
index 3111562568..2c63b0f3a3 100644
--- a/src/lxc/lxc_driver.c
+++ b/src/lxc/lxc_driver.c
@@ -1496,7 +1496,8 @@ static int lxcStateInitialize(bool privileged,
         return VIR_DRV_STATE_INIT_ERROR;
     lxc_driver->lockFD = -1;
     if (virMutexInit(&lxc_driver->lock) < 0) {
-        VIR_FREE(lxc_driver);
+        g_free(lxc_driver);
+        lxc_driver = NULL;
         return VIR_DRV_STATE_INIT_ERROR;
     }
 
@@ -1633,7 +1634,8 @@ static int lxcStateCleanup(void)
 
     virObjectUnref(lxc_driver->config);
     virMutexDestroy(&lxc_driver->lock);
-    VIR_FREE(lxc_driver);
+    g_free(lxc_driver);
+    lxc_driver = NULL;
 
     return 0;
 }
@@ -2473,7 +2475,8 @@ static int lxcDomainSetAutostart(virDomainPtr dom,
 {
     virLXCDriverPtr driver = dom->conn->privateData;
     virDomainObjPtr vm;
-    char *configFile = NULL, *autostartLink = NULL;
+    g_autofree char *configFile = NULL;
+    g_autofree char *autostartLink = NULL;
     int ret = -1;
     virLXCDriverConfigPtr cfg = virLXCDriverGetConfig(driver);
 
@@ -2538,8 +2541,6 @@ static int lxcDomainSetAutostart(virDomainPtr dom,
     virLXCDomainObjEndJob(driver, vm);
 
  cleanup:
-    VIR_FREE(configFile);
-    VIR_FREE(autostartLink);
     virDomainObjEndAPI(&vm);
     virObjectUnref(cfg);
     return ret;
@@ -2551,8 +2552,7 @@ static int lxcFreezeContainer(virDomainObjPtr vm)
     int check_interval = 1; /* In milliseconds */
     int exp = 10;
     int waited_time = 0;
-    int ret = -1;
-    char *state = NULL;
+    g_autofree char *state = NULL;
     virLXCDomainObjPrivatePtr priv = vm->privateData;
 
     while (waited_time < timeout) {
@@ -2599,10 +2599,8 @@ static int lxcFreezeContainer(virDomainObjPtr vm)
         }
         VIR_DEBUG("Read freezer.state: %s", state);
 
-        if (STREQ(state, "FROZEN")) {
-            ret = 0;
-            goto cleanup;
-        }
+        if (STREQ(state, "FROZEN"))
+            return 0;
 
         waited_time += check_interval;
         /*
@@ -2614,7 +2612,6 @@ static int lxcFreezeContainer(virDomainObjPtr vm)
          * In that case, eager polling will just waste CPU time.
          */
         check_interval *= exp;
-        VIR_FREE(state);
     }
     VIR_DEBUG("lxcFreezeContainer timeout");
  error:
@@ -2624,11 +2621,7 @@ static int lxcFreezeContainer(virDomainObjPtr vm)
      * This is likely to fall the group back again gracefully.
      */
     virCgroupSetFreezerState(priv->cgroup, "THAWED");
-    ret = -1;
-
- cleanup:
-    VIR_FREE(state);
-    return ret;
+    return -1;
 }
 
 static int lxcDomainSuspend(virDomainPtr dom)
@@ -3345,7 +3338,7 @@ lxcDomainAttachDeviceDiskLive(virLXCDriverPtr driver,
     virDomainDiskDefPtr def = dev->data.disk;
     int ret = -1;
     struct stat sb;
-    char *file = NULL;
+    g_autofree char *file = NULL;
     int perms;
     const char *src = NULL;
 
@@ -3433,7 +3426,6 @@ lxcDomainAttachDeviceDiskLive(virLXCDriverPtr driver,
  cleanup:
     if (src)
         virDomainAuditDisk(vm, NULL, def->src, "attach", ret == 0);
-    VIR_FREE(file);
     return ret;
 }
 
@@ -3584,7 +3576,7 @@ lxcDomainAttachDeviceHostdevSubsysUSBLive(virLXCDriverPtr driver,
     virLXCDomainObjPrivatePtr priv = vm->privateData;
     virDomainHostdevDefPtr def = dev->data.hostdev;
     int ret = -1;
-    char *src = NULL;
+    g_autofree char *src = NULL;
     struct stat sb;
     virUSBDevicePtr usb = NULL;
     virDomainHostdevSubsysUSBPtr usbsrc;
@@ -3643,7 +3635,6 @@ lxcDomainAttachDeviceHostdevSubsysUSBLive(virLXCDriverPtr driver,
  cleanup:
     virDomainAuditHostdev(vm, def, "attach", ret == 0);
     virUSBDeviceFree(usb);
-    VIR_FREE(src);
     return ret;
 }
 
@@ -3910,14 +3901,14 @@ lxcDomainDetachDeviceDiskLive(virDomainObjPtr vm,
 {
     virLXCDomainObjPrivatePtr priv = vm->privateData;
     virDomainDiskDefPtr def = NULL;
-    int idx, ret = -1;
-    char *dst = NULL;
+    int idx;
+    g_autofree char *dst = NULL;
     const char *src;
 
     if (!priv->initpid) {
         virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                        _("Cannot attach disk until init PID is known"));
-        goto cleanup;
+        return -1;
     }
 
     if ((idx = virDomainDiskIndexByName(vm->def,
@@ -3925,7 +3916,7 @@ lxcDomainDetachDeviceDiskLive(virDomainObjPtr vm,
                                         false)) < 0) {
         virReportError(VIR_ERR_OPERATION_FAILED,
                        _("disk %s not found"), dev->data.disk->dst);
-        goto cleanup;
+        return -1;
     }
 
     def = vm->def->disks[idx];
@@ -3936,12 +3927,12 @@ lxcDomainDetachDeviceDiskLive(virDomainObjPtr vm,
     if (!virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_DEVICES)) {
         virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                        _("devices cgroup isn't mounted"));
-        goto cleanup;
+        return -1;
     }
 
     if (lxcDomainAttachDeviceUnlink(vm, dst) < 0) {
         virDomainAuditDisk(vm, def->src, NULL, "detach", false);
-        goto cleanup;
+        return -1;
     }
     virDomainAuditDisk(vm, def->src, NULL, "detach", true);
 
@@ -3953,11 +3944,7 @@ lxcDomainDetachDeviceDiskLive(virDomainObjPtr vm,
     virDomainDiskRemove(vm->def, idx);
     virDomainDiskDefFree(def);
 
-    ret = 0;
-
- cleanup:
-    VIR_FREE(dst);
-    return ret;
+    return 0;
 }
 
 
@@ -4055,7 +4042,7 @@ lxcDomainDetachDeviceHostdevUSBLive(virLXCDriverPtr driver,
     virLXCDomainObjPrivatePtr priv = vm->privateData;
     virDomainHostdevDefPtr def = NULL;
     int idx, ret = -1;
-    char *dst = NULL;
+    g_autofree char *dst = NULL;
     virUSBDevicePtr usb = NULL;
     virHostdevManagerPtr hostdev_mgr = driver->hostdevMgr;
     virDomainHostdevSubsysUSBPtr usbsrc;
@@ -4103,7 +4090,6 @@ lxcDomainDetachDeviceHostdevUSBLive(virLXCDriverPtr driver,
 
  cleanup:
     virUSBDeviceFree(usb);
-    VIR_FREE(dst);
     return ret;
 }
 
@@ -4948,8 +4934,6 @@ lxcDomainGetHostname(virDomainPtr dom,
     virDomainObjPtr vm = NULL;
     char macaddr[VIR_MAC_STRING_BUFLEN];
     g_autoptr(virConnect) conn = NULL;
-    virNetworkDHCPLeasePtr *leases = NULL;
-    int n_leases;
     size_t i, j;
     char *hostname = NULL;
 
@@ -4973,6 +4957,8 @@ lxcDomainGetHostname(virDomainPtr dom,
     for (i = 0; i < vm->def->nnets; i++) {
         g_autoptr(virNetwork) network = NULL;
         virDomainNetDefPtr net = vm->def->nets[i];
+        g_autofree virNetworkDHCPLeasePtr *leases = NULL;
+        int n_leases;
 
         if (net->type != VIR_DOMAIN_NET_TYPE_NETWORK)
             continue;
@@ -4996,8 +4982,6 @@ lxcDomainGetHostname(virDomainPtr dom,
             virNetworkDHCPLeaseFree(lease);
         }
 
-        VIR_FREE(leases);
-
         if (hostname)
             goto endjob;
     }
diff --git a/src/lxc/lxc_fuse.c b/src/lxc/lxc_fuse.c
index e73b4d0690..c4223f4e06 100644
--- a/src/lxc/lxc_fuse.c
+++ b/src/lxc/lxc_fuse.c
@@ -326,10 +326,10 @@ int lxcSetupFuse(virLXCFusePtr *f, virDomainDefPtr def)
     *f = fuse;
     return ret;
  cleanup1:
-    VIR_FREE(fuse->mountpoint);
+    g_free(fuse->mountpoint);
     virMutexDestroy(&fuse->lock);
  cleanup2:
-    VIR_FREE(fuse);
+    g_free(fuse);
     goto cleanup;
 }
 
@@ -356,8 +356,8 @@ void lxcFreeFuse(virLXCFusePtr *f)
             fuse_exit(fuse->fuse);
         virMutexUnlock(&fuse->lock);
 
-        VIR_FREE(fuse->mountpoint);
-        VIR_FREE(*f);
+        g_free(fuse->mountpoint);
+        g_free(*f);
     }
 }
 #else
diff --git a/src/lxc/lxc_monitor.c b/src/lxc/lxc_monitor.c
index 479125374b..e0d03f9d4c 100644
--- a/src/lxc/lxc_monitor.c
+++ b/src/lxc/lxc_monitor.c
@@ -143,7 +143,7 @@ virLXCMonitorPtr virLXCMonitorNew(virDomainObjPtr vm,
                                   virLXCMonitorCallbacksPtr cb)
 {
     virLXCMonitorPtr mon;
-    char *sockpath = NULL;
+    g_autofree char *sockpath = NULL;
 
     if (virLXCMonitorInitialize() < 0)
         return NULL;
@@ -180,14 +180,11 @@ virLXCMonitorPtr virLXCMonitorNew(virDomainObjPtr vm,
     mon->vm = virObjectRef(vm);
     memcpy(&mon->cb, cb, sizeof(mon->cb));
 
- cleanup:
-    VIR_FREE(sockpath);
     return mon;
 
  error:
     virObjectUnref(mon);
-    mon = NULL;
-    goto cleanup;
+    return NULL;
 }
 
 
diff --git a/src/lxc/lxc_native.c b/src/lxc/lxc_native.c
index 4545d8cde1..622d506398 100644
--- a/src/lxc/lxc_native.c
+++ b/src/lxc/lxc_native.c
@@ -93,11 +93,11 @@ lxcFstabFree(lxcFstabPtr fstab)
         lxcFstabPtr next = NULL;
         next = fstab->next;
 
-        VIR_FREE(fstab->src);
-        VIR_FREE(fstab->dst);
-        VIR_FREE(fstab->type);
-        VIR_FREE(fstab->options);
-        VIR_FREE(fstab);
+        g_free(fstab->src);
+        g_free(fstab->dst);
+        g_free(fstab->type);
+        g_free(fstab->options);
+        g_free(fstab);
 
         fstab = next;
     }
@@ -105,7 +105,7 @@ lxcFstabFree(lxcFstabPtr fstab)
 
 static char ** lxcStringSplit(const char *string)
 {
-    char *tmp;
+    g_autofree char *tmp = NULL;
     size_t i;
     size_t ntokens = 0;
     char **parts;
@@ -136,12 +136,10 @@ static char ** lxcStringSplit(const char *string)
         result[ntokens - 2] = g_strdup(parts[i]);
     }
 
-    VIR_FREE(tmp);
     virStringListFree(parts);
     return result;
 
  error:
-    VIR_FREE(tmp);
     virStringListFree(parts);
     virStringListFree(result);
     return NULL;
@@ -257,7 +255,7 @@ static int
 lxcAddFstabLine(virDomainDefPtr def, lxcFstabPtr fstab)
 {
     const char *src = NULL;
-    char *dst = NULL;
+    g_autofree char *dst = NULL;
     char **options = virStringSplit(fstab->options, ",", 0);
     bool readonly;
     int type = VIR_DOMAIN_FS_TYPE_MOUNT;
@@ -313,7 +311,6 @@ lxcAddFstabLine(virDomainDefPtr def, lxcFstabPtr fstab)
     ret = 1;
 
  cleanup:
-    VIR_FREE(dst);
     virStringListFree(options);
     return ret;
 }
@@ -444,8 +441,8 @@ lxcAddNetworkRouteDefinition(const char *address,
                              size_t *nroutes)
 {
     virNetDevIPRoutePtr route = NULL;
-    char *familyStr = NULL;
-    char *zero = NULL;
+    g_autofree char *familyStr = NULL;
+    g_autofree char *zero = NULL;
 
     zero = g_strdup(family == AF_INET ? VIR_SOCKET_ADDR_IPV4_ALL : VIR_SOCKET_ADDR_IPV6_ALL);
 
@@ -459,14 +456,9 @@ lxcAddNetworkRouteDefinition(const char *address,
     if (VIR_APPEND_ELEMENT(*routes, *nroutes, route) < 0)
         goto error;
 
-    VIR_FREE(familyStr);
-    VIR_FREE(zero);
-
     return 0;
 
  error:
-    VIR_FREE(familyStr);
-    VIR_FREE(zero);
     virNetDevIPRouteFree(route);
     return -1;
 }
@@ -499,7 +491,7 @@ lxcAddNetworkDefinition(virDomainDefPtr def, lxcNetworkParseData *data)
         /* This still requires the user to manually setup the vlan interface
          * on the host */
         if (isVlan && data->vlanid) {
-            VIR_FREE(hostdev->source.caps.u.net.ifname);
+            g_free(hostdev->source.caps.u.net.ifname);
             hostdev->source.caps.u.net.ifname = g_strdup_printf("%s.%s",
                                                                 data->link,
                                                                 data->vlanid);
@@ -553,8 +545,9 @@ lxcAddNetworkDefinition(virDomainDefPtr def, lxcNetworkParseData *data)
 
  error:
     for (i = 0; i < data->nips; i++)
-        VIR_FREE(data->ips[i]);
-    VIR_FREE(data->ips);
+        g_free(data->ips[i]);
+    g_free(data->ips);
+    data->ips = NULL;
     virDomainNetDefFree(net);
     virDomainHostdevDefFree(hostdev);
     return -1;
@@ -568,7 +561,7 @@ lxcNetworkParseDataIPs(const char *name,
 {
     int family = AF_INET;
     char **ipparts = NULL;
-    virNetDevIPAddrPtr ip = NULL;
+    g_autofree virNetDevIPAddrPtr ip = NULL;
 
     if (VIR_ALLOC(ip) < 0)
         return -1;
@@ -585,16 +578,13 @@ lxcNetworkParseDataIPs(const char *name,
                        _("Invalid CIDR address: '%s'"), value->str);
 
         virStringListFree(ipparts);
-        VIR_FREE(ip);
         return -1;
     }
 
     virStringListFree(ipparts);
 
-    if (VIR_APPEND_ELEMENT(parseData->ips, parseData->nips, ip) < 0) {
-        VIR_FREE(ip);
+    if (VIR_APPEND_ELEMENT(parseData->ips, parseData->nips, ip) < 0)
         return -1;
-    }
 
     return 0;
 }
@@ -792,16 +782,18 @@ lxcConvertNetworkSettings(virDomainDefPtr def, virConfPtr properties)
 
  cleanup:
     for (i = 0; i < networks.ndata; i++)
-        VIR_FREE(networks.parseData[i]);
-    VIR_FREE(networks.parseData);
+        g_free(networks.parseData[i]);
+    g_free(networks.parseData);
+    networks.parseData = NULL;
     return ret;
 
  error:
     for (i = 0; i < networks.ndata; i++) {
         lxcNetworkParseDataPtr data = networks.parseData[i];
         for (j = 0; j < data->nips; j++)
-            VIR_FREE(data->ips[j]);
-        VIR_FREE(data->ips);
+            g_free(data->ips[j]);
+        g_free(data->ips);
+        data->ips = NULL;
     }
     goto cleanup;
 }
@@ -905,7 +897,8 @@ lxcSetMemTune(virDomainDefPtr def, virConfPtr properties)
         size = size / 1024;
         virDomainDefSetMemoryTotal(def, size);
         def->mem.hard_limit = virMemoryLimitTruncate(size);
-        VIR_FREE(value);
+        g_free(value);
+        value = NULL;
     }
 
     if (virConfGetValueString(properties,
@@ -914,7 +907,8 @@ lxcSetMemTune(virDomainDefPtr def, virConfPtr properties)
         if (lxcConvertSize(value, &size) < 0)
             return -1;
         def->mem.soft_limit = virMemoryLimitTruncate(size / 1024);
-        VIR_FREE(value);
+        g_free(value);
+        value = NULL;
     }
 
     if (virConfGetValueString(properties,
@@ -937,14 +931,16 @@ lxcSetCpuTune(virDomainDefPtr def, virConfPtr properties)
         if (virStrToLong_ull(value, NULL, 10, &def->cputune.shares) < 0)
             goto error;
         def->cputune.sharesSpecified = true;
-        VIR_FREE(value);
+        g_free(value);
+        value = NULL;
     }
 
     if (virConfGetValueString(properties, "lxc.cgroup.cpu.cfs_quota_us",
                               &value) > 0) {
         if (virStrToLong_ll(value, NULL, 10, &def->cputune.quota) < 0)
             goto error;
-        VIR_FREE(value);
+        g_free(value);
+        value = NULL;
     }
 
     if (virConfGetValueString(properties, "lxc.cgroup.cpu.cfs_period_us",
@@ -972,7 +968,8 @@ lxcSetCpusetTune(virDomainDefPtr def, virConfPtr properties)
         if (virBitmapParse(value, &def->cpumask, VIR_DOMAIN_CPUMASK_LEN) < 0)
             return -1;
         def->placement_mode = VIR_DOMAIN_CPU_PLACEMENT_MODE_STATIC;
-        VIR_FREE(value);
+        g_free(value);
+        value = NULL;
     }
 
     if (virConfGetValueString(properties, "lxc.cgroup.cpuset.mems",
@@ -1001,7 +998,7 @@ lxcBlkioDeviceWalkCallback(const char *name, virConfValuePtr value, void *data)
     virBlkioDevicePtr device = NULL;
     virDomainDefPtr def = data;
     size_t i = 0;
-    char *path = NULL;
+    g_autofree char *path = NULL;
     int ret = -1;
 
     if (!STRPREFIX(name, "lxc.cgroup.blkio.") ||
@@ -1077,8 +1074,6 @@ lxcBlkioDeviceWalkCallback(const char *name, virConfValuePtr value, void *data)
 
  cleanup:
     virStringListFree(parts);
-    VIR_FREE(path);
-
     return ret;
 }
 
@@ -1172,7 +1167,8 @@ lxcParseConfigString(const char *config,
         else if (arch == VIR_ARCH_NONE && STREQ(value, "amd64"))
             arch = VIR_ARCH_X86_64;
         vmdef->os.arch = arch;
-        VIR_FREE(value);
+        g_free(value);
+        value = NULL;
     }
 
     vmdef->os.init = g_strdup("/sbin/init");
diff --git a/src/lxc/lxc_process.c b/src/lxc/lxc_process.c
index 5199f3806e..998754062c 100644
--- a/src/lxc/lxc_process.c
+++ b/src/lxc/lxc_process.c
@@ -135,14 +135,13 @@ static void
 lxcProcessRemoveDomainStatus(virLXCDriverConfigPtr cfg,
                               virDomainObjPtr vm)
 {
-    char *file = NULL;
-
-    file = g_strdup_printf("%s/%s.xml", cfg->stateDir, vm->def->name);
+    g_autofree char *file = g_strdup_printf("%s/%s.xml",
+                                            cfg->stateDir,
+                                            vm->def->name);
 
     if (unlink(file) < 0 && errno != ENOENT && errno != ENOTDIR)
         VIR_WARN("Failed to remove domain XML for %s: %s",
                  vm->def->name, g_strerror(errno));
-    VIR_FREE(file);
 }
 
 
@@ -170,13 +169,12 @@ static void virLXCProcessCleanup(virLXCDriverPtr driver,
 
     /* now that we know it's stopped call the hook if present */
     if (virHookPresent(VIR_HOOK_DRIVER_LXC)) {
-        char *xml = virDomainDefFormat(vm->def, driver->xmlopt, 0);
+        g_autofree char *xml = virDomainDefFormat(vm->def, driver->xmlopt, 0);
 
         /* we can't stop the operation even if the script raised an error */
         virHookCall(VIR_HOOK_DRIVER_LXC, vm->def->name,
                     VIR_HOOK_LXC_OP_STOPPED, VIR_HOOK_SUBOP_END,
                     NULL, xml, NULL);
-        VIR_FREE(xml);
     }
 
     virSecurityManagerRestoreAllLabel(driver->securityManager,
@@ -185,9 +183,12 @@ static void virLXCProcessCleanup(virLXCDriverPtr driver,
     /* Clear out dynamically assigned labels */
     if (vm->def->nseclabels &&
         vm->def->seclabels[0]->type == VIR_DOMAIN_SECLABEL_DYNAMIC) {
-        VIR_FREE(vm->def->seclabels[0]->model);
-        VIR_FREE(vm->def->seclabels[0]->label);
-        VIR_FREE(vm->def->seclabels[0]->imagelabel);
+        g_free(vm->def->seclabels[0]->model);
+        g_free(vm->def->seclabels[0]->label);
+        g_free(vm->def->seclabels[0]->imagelabel);
+        vm->def->seclabels[0]->model = NULL;
+        vm->def->seclabels[0]->label = NULL;
+        vm->def->seclabels[0]->imagelabel = NULL;
     }
 
     /* Stop autodestroy in case guest is restarted */
@@ -243,17 +244,17 @@ static void virLXCProcessCleanup(virLXCDriverPtr driver,
      * the bug we are working around here.
      */
     virCgroupTerminateMachine(priv->machineName);
-    VIR_FREE(priv->machineName);
+    g_free(priv->machineName);
+    priv->machineName = NULL;
 
     /* The "release" hook cleans up additional resources */
     if (virHookPresent(VIR_HOOK_DRIVER_LXC)) {
-        char *xml = virDomainDefFormat(vm->def, driver->xmlopt, 0);
+        g_autofree char *xml = virDomainDefFormat(vm->def, driver->xmlopt, 0);
 
         /* we can't stop the operation even if the script raised an error */
         virHookCall(VIR_HOOK_DRIVER_LXC, vm->def->name,
                     VIR_HOOK_LXC_OP_RELEASE, VIR_HOOK_SUBOP_END,
                     NULL, xml, NULL);
-        VIR_FREE(xml);
     }
 
     virDomainObjRemoveTransientDef(vm);
@@ -442,12 +443,9 @@ static int virLXCProcessSetupNamespaceName(virLXCDriverPtr driver,
 
 static int virLXCProcessSetupNamespacePID(int ns_type, const char *name)
 {
-    int fd;
-    char *path;
-
-    path = g_strdup_printf("/proc/%s/ns/%s", name, nsInfoLocal[ns_type]);
-    fd = open(path, O_RDONLY);
-    VIR_FREE(path);
+    g_autofree char *path = g_strdup_printf("/proc/%s/ns/%s",
+                                            name, nsInfoLocal[ns_type]);
+    int fd = open(path, O_RDONLY);
     if (fd < 0) {
         virReportSystemError(errno,
                              _("failed to open ns %s"),
@@ -460,7 +458,7 @@ static int virLXCProcessSetupNamespacePID(int ns_type, const char *name)
 
 static int virLXCProcessSetupNamespaceNet(int ns_type, const char *name)
 {
-    char *path;
+    g_autofree char *path = NULL;
     int fd;
     if (ns_type != VIR_LXC_DOMAIN_NAMESPACE_SHARENET) {
         virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
@@ -471,7 +469,6 @@ static int virLXCProcessSetupNamespaceNet(int ns_type, const char *name)
 
     path = g_strdup_printf("%s/netns/%s", RUNSTATEDIR, name);
     fd = open(path, O_RDONLY);
-    VIR_FREE(path);
     if (fd < 0) {
         virReportSystemError(errno,
                              _("failed to open netns %s"), name);
@@ -669,7 +666,8 @@ virLXCProcessCleanInterfaces(virDomainDefPtr def)
     size_t i;
 
     for (i = 0; i < def->nnets; i++) {
-        VIR_FREE(def->nets[i]->ifname_guest_actual);
+        g_free(def->nets[i]->ifname_guest_actual);
+        def->nets[i]->ifname_guest_actual = NULL;
         VIR_DEBUG("Cleared net names: %s", def->nets[i]->ifname_guest);
     }
 }
@@ -757,24 +755,20 @@ virLXCProcessGetNsInode(pid_t pid,
                         const char *nsname,
                         ino_t *inode)
 {
-    char *path = NULL;
+    g_autofree char *path = NULL;
     struct stat sb;
-    int ret = -1;
 
     path = g_strdup_printf("/proc/%lld/ns/%s", (long long)pid, nsname);
 
     if (stat(path, &sb) < 0) {
         virReportSystemError(errno,
                              _("Unable to stat %s"), path);
-        goto cleanup;
+        return -1;
     }
 
     *inode = sb.st_ino;
-    ret = 0;
 
- cleanup:
-    VIR_FREE(path);
-    return ret;
+    return 0;
 }
 
 
@@ -929,8 +923,8 @@ virLXCProcessBuildControllerCmd(virLXCDriverPtr driver,
                                 const char *pidfile)
 {
     size_t i;
-    char *filterstr;
-    char *outputstr;
+    g_autofree char *filterstr = NULL;
+    g_autofree char *outputstr = NULL;
     virCommandPtr cmd;
     virLXCDriverConfigPtr cfg = virLXCDriverGetConfig(driver);
 
@@ -950,7 +944,6 @@ virLXCProcessBuildControllerCmd(virLXCDriverPtr driver,
         }
 
         virCommandAddEnvPair(cmd, "LIBVIRT_LOG_FILTERS", filterstr);
-        VIR_FREE(filterstr);
     }
 
     if (cfg->log_libvirtd) {
@@ -962,7 +955,6 @@ virLXCProcessBuildControllerCmd(virLXCDriverPtr driver,
             }
 
             virCommandAddEnvPair(cmd, "LIBVIRT_LOG_OUTPUTS", outputstr);
-            VIR_FREE(outputstr);
         }
     } else {
         virCommandAddEnvFormat(cmd,
@@ -985,12 +977,11 @@ virLXCProcessBuildControllerCmd(virLXCDriverPtr driver,
 
     for (i = 0; i < VIR_LXC_DOMAIN_NAMESPACE_LAST; i++) {
         if (nsInheritFDs[i] > 0) {
-            char *tmp = NULL;
-            tmp = g_strdup_printf("--share-%s", nsInfoLocal[i]);
+            g_autofree char *tmp = g_strdup_printf("--share-%s",
+                                                   nsInfoLocal[i]);
             virCommandAddArg(cmd, tmp);
             virCommandAddArgFormat(cmd, "%d", nsInheritFDs[i]);
             virCommandPassFD(cmd, nsInheritFDs[i], 0);
-            VIR_FREE(tmp);
         }
     }
 
@@ -1188,15 +1179,15 @@ int virLXCProcessStart(virConnectPtr conn,
 {
     int rc = -1, r;
     size_t nttyFDs = 0;
-    int *ttyFDs = NULL;
+    g_autofree int *ttyFDs = NULL;
     size_t i;
-    char *logfile = NULL;
+    g_autofree char *logfile = NULL;
     int logfd = -1;
     VIR_AUTOSTRINGLIST veths = NULL;
     int handshakefds[2] = { -1, -1 };
     off_t pos = -1;
     char ebuf[1024];
-    char *timestamp;
+    g_autofree char *timestamp = NULL;
     int nsInheritFDs[VIR_LXC_DOMAIN_NAMESPACE_LAST];
     virCommandPtr cmd = NULL;
     virLXCDomainObjPrivatePtr priv = vm->privateData;
@@ -1205,7 +1196,7 @@ int virLXCProcessStart(virConnectPtr conn,
     virLXCDriverConfigPtr cfg = virLXCDriverGetConfig(driver);
     virCgroupPtr selfcgroup;
     int status;
-    char *pidfile = NULL;
+    g_autofree char *pidfile = NULL;
 
     if (virCgroupNewSelf(&selfcgroup) < 0)
         return -1;
@@ -1283,18 +1274,14 @@ int virLXCProcessStart(virConnectPtr conn,
 
     /* Run an early hook to set-up missing devices */
     if (virHookPresent(VIR_HOOK_DRIVER_LXC)) {
-        char *xml = virDomainDefFormat(vm->def, driver->xmlopt, 0);
-        int hookret;
-
-        hookret = virHookCall(VIR_HOOK_DRIVER_LXC, vm->def->name,
-                              VIR_HOOK_LXC_OP_PREPARE, VIR_HOOK_SUBOP_BEGIN,
-                              NULL, xml, NULL);
-        VIR_FREE(xml);
+        g_autofree char *xml = virDomainDefFormat(vm->def, driver->xmlopt, 0);
 
         /*
          * If the script raised an error abort the launch
          */
-        if (hookret < 0)
+        if (virHookCall(VIR_HOOK_DRIVER_LXC, vm->def->name,
+                        VIR_HOOK_LXC_OP_PREPARE, VIR_HOOK_SUBOP_BEGIN,
+                        NULL, xml, NULL) < 0)
             goto cleanup;
     }
 
@@ -1348,10 +1335,10 @@ int virLXCProcessStart(virConnectPtr conn,
             goto cleanup;
         }
 
-        VIR_FREE(vm->def->consoles[i]->source->data.file.path);
+        g_free(vm->def->consoles[i]->source->data.file.path);
         vm->def->consoles[i]->source->data.file.path = ttyPath;
 
-        VIR_FREE(vm->def->consoles[i]->info.alias);
+        g_free(vm->def->consoles[i]->info.alias);
         vm->def->consoles[i]->info.alias = g_strdup_printf("console%zu", i);
     }
 
@@ -1388,18 +1375,14 @@ int virLXCProcessStart(virConnectPtr conn,
 
     /* now that we know it is about to start call the hook if present */
     if (virHookPresent(VIR_HOOK_DRIVER_LXC)) {
-        char *xml = virDomainDefFormat(vm->def, driver->xmlopt, 0);
-        int hookret;
-
-        hookret = virHookCall(VIR_HOOK_DRIVER_LXC, vm->def->name,
-                              VIR_HOOK_LXC_OP_START, VIR_HOOK_SUBOP_BEGIN,
-                              NULL, xml, NULL);
-        VIR_FREE(xml);
+        g_autofree char *xml = virDomainDefFormat(vm->def, driver->xmlopt, 0);
 
         /*
          * If the script raised an error abort the launch
          */
-        if (hookret < 0)
+        if (virHookCall(VIR_HOOK_DRIVER_LXC, vm->def->name,
+                        VIR_HOOK_LXC_OP_START, VIR_HOOK_SUBOP_BEGIN,
+                        NULL, xml, NULL) < 0)
             goto cleanup;
     }
 
@@ -1411,7 +1394,6 @@ int virLXCProcessStart(virConnectPtr conn,
         VIR_WARN("Unable to write timestamp to logfile: %s",
                  g_strerror(errno));
     }
-    VIR_FREE(timestamp);
 
     /* Log generated command line */
     virCommandWriteArgLog(cmd, logfd);
@@ -1530,18 +1512,14 @@ int virLXCProcessStart(virConnectPtr conn,
 
     /* finally we can call the 'started' hook script if any */
     if (virHookPresent(VIR_HOOK_DRIVER_LXC)) {
-        char *xml = virDomainDefFormat(vm->def, driver->xmlopt, 0);
-        int hookret;
-
-        hookret = virHookCall(VIR_HOOK_DRIVER_LXC, vm->def->name,
-                              VIR_HOOK_LXC_OP_STARTED, VIR_HOOK_SUBOP_BEGIN,
-                              NULL, xml, NULL);
-        VIR_FREE(xml);
+        g_autofree char *xml = virDomainDefFormat(vm->def, driver->xmlopt, 0);
 
         /*
          * If the script raised an error abort the launch
          */
-        if (hookret < 0)
+        if (virHookCall(VIR_HOOK_DRIVER_LXC, vm->def->name,
+                        VIR_HOOK_LXC_OP_STARTED, VIR_HOOK_SUBOP_BEGIN,
+                        NULL, xml, NULL) < 0)
             goto cleanup;
     }
 
@@ -1559,11 +1537,8 @@ int virLXCProcessStart(virConnectPtr conn,
     virCommandFree(cmd);
     for (i = 0; i < nttyFDs; i++)
         VIR_FORCE_CLOSE(ttyFDs[i]);
-    VIR_FREE(ttyFDs);
     VIR_FORCE_CLOSE(handshakefds[0]);
     VIR_FORCE_CLOSE(handshakefds[1]);
-    VIR_FREE(pidfile);
-    VIR_FREE(logfile);
     virObjectUnref(cfg);
     virObjectUnref(caps);
 
@@ -1710,15 +1685,12 @@ virLXCProcessReconnectDomain(virDomainObjPtr vm,
 
         /* now that we know it's reconnected call the hook if present */
         if (virHookPresent(VIR_HOOK_DRIVER_LXC)) {
-            char *xml = virDomainDefFormat(vm->def, driver->xmlopt, 0);
-            int hookret;
+            g_autofree char *xml = virDomainDefFormat(vm->def, driver->xmlopt, 0);
 
             /* we can't stop the operation even if the script raised an error */
-            hookret = virHookCall(VIR_HOOK_DRIVER_LXC, vm->def->name,
-                                  VIR_HOOK_LXC_OP_RECONNECT, VIR_HOOK_SUBOP_BEGIN,
-                                  NULL, xml, NULL);
-            VIR_FREE(xml);
-            if (hookret < 0)
+            if (virHookCall(VIR_HOOK_DRIVER_LXC, vm->def->name,
+                            VIR_HOOK_LXC_OP_RECONNECT, VIR_HOOK_SUBOP_BEGIN,
+                            NULL, xml, NULL) < 0)
                 goto error;
         }
 
-- 
2.24.1




More information about the libvir-list mailing list