Using VIR_AUTOFREE() in all strings of qemu_driver.c make the code a bit tidier and smaller, sparing VIR_FREE() calls and sometimes a whole 'cleanup' label. This is a huge change due to the amount of char * declared in this file, thus let's split it in 3. This is the first part. Signed-off-by: Daniel Henrique Barboza <danielhb413@xxxxxxxxx> --- src/qemu/qemu_driver.c | 103 +++++++++++++---------------------------- 1 file changed, 33 insertions(+), 70 deletions(-) diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index 48eca00777..22c872e912 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -381,11 +381,9 @@ qemuDomainSnapshotLoad(virDomainObjPtr vm, void *data) { char *baseDir = (char *)data; - char *snapDir = NULL; + VIR_AUTOFREE(char *) snapDir = NULL; DIR *dir = NULL; struct dirent *entry; - char *xmlStr; - char *fullpath; virDomainSnapshotDefPtr def = NULL; virDomainMomentObjPtr snap = NULL; virDomainMomentObjPtr current = NULL; @@ -420,6 +418,9 @@ qemuDomainSnapshotLoad(virDomainObjPtr vm, goto cleanup; while ((direrr = virDirRead(dir, &entry, NULL)) > 0) { + VIR_AUTOFREE(char *) xmlStr = NULL; + VIR_AUTOFREE(char *) fullpath = NULL; + /* NB: ignoring errors, so one malformed config doesn't kill the whole process */ VIR_INFO("Loading snapshot file '%s'", entry->d_name); @@ -435,7 +436,6 @@ qemuDomainSnapshotLoad(virDomainObjPtr vm, virReportSystemError(errno, _("Failed to read snapshot file %s"), fullpath); - VIR_FREE(fullpath); continue; } @@ -448,8 +448,6 @@ qemuDomainSnapshotLoad(virDomainObjPtr vm, virReportError(VIR_ERR_INTERNAL_ERROR, _("Failed to parse snapshot XML from file '%s'"), fullpath); - VIR_FREE(fullpath); - VIR_FREE(xmlStr); continue; } @@ -463,9 +461,6 @@ qemuDomainSnapshotLoad(virDomainObjPtr vm, vm->def->name); current = snap; } - - VIR_FREE(fullpath); - VIR_FREE(xmlStr); } if (direrr < 0) virReportError(VIR_ERR_INTERNAL_ERROR, @@ -492,7 +487,6 @@ qemuDomainSnapshotLoad(virDomainObjPtr vm, ret = 0; cleanup: VIR_DIR_CLOSE(dir); - VIR_FREE(snapDir); virObjectUnlock(vm); return ret; } @@ -503,11 +497,9 @@ qemuDomainCheckpointLoad(virDomainObjPtr vm, void *data) { char *baseDir = (char *)data; - char *chkDir = NULL; + VIR_AUTOFREE(char *) chkDir = NULL; DIR *dir = NULL; struct dirent *entry; - char *xmlStr; - char *fullpath; virDomainCheckpointDefPtr def = NULL; virDomainMomentObjPtr chk = NULL; virDomainMomentObjPtr current = NULL; @@ -538,6 +530,9 @@ qemuDomainCheckpointLoad(virDomainObjPtr vm, goto cleanup; while ((direrr = virDirRead(dir, &entry, NULL)) > 0) { + VIR_AUTOFREE(char *) xmlStr = NULL; + VIR_AUTOFREE(char *) fullpath = NULL; + /* NB: ignoring errors, so one malformed config doesn't kill the whole process */ VIR_INFO("Loading checkpoint file '%s'", entry->d_name); @@ -553,7 +548,6 @@ qemuDomainCheckpointLoad(virDomainObjPtr vm, virReportSystemError(errno, _("Failed to read checkpoint file %s"), fullpath); - VIR_FREE(fullpath); continue; } @@ -566,8 +560,6 @@ qemuDomainCheckpointLoad(virDomainObjPtr vm, virReportError(VIR_ERR_INTERNAL_ERROR, _("Failed to parse checkpoint XML from file '%s'"), fullpath); - VIR_FREE(fullpath); - VIR_FREE(xmlStr); virObjectUnref(def); continue; } @@ -575,9 +567,6 @@ qemuDomainCheckpointLoad(virDomainObjPtr vm, chk = virDomainCheckpointAssignDef(vm->checkpoints, def); if (chk == NULL) virObjectUnref(def); - - VIR_FREE(fullpath); - VIR_FREE(xmlStr); } if (direrr < 0) virReportError(VIR_ERR_INTERNAL_ERROR, @@ -602,7 +591,6 @@ qemuDomainCheckpointLoad(virDomainObjPtr vm, ret = 0; cleanup: VIR_DIR_CLOSE(dir); - VIR_FREE(chkDir); virObjectUnlock(vm); return ret; } @@ -660,12 +648,11 @@ qemuStateInitialize(bool privileged, virStateInhibitCallback callback, void *opaque) { - char *driverConf = NULL; + VIR_AUTOFREE(char *) driverConf = NULL; virQEMUDriverConfigPtr cfg; uid_t run_uid = -1; gid_t run_gid = -1; - char *hugepagePath = NULL; - char *memoryBackingPath = NULL; + VIR_AUTOFREE(char *) memoryBackingPath = NULL; size_t i; if (VIR_ALLOC(qemu_driver) < 0) @@ -705,7 +692,6 @@ qemuStateInitialize(bool privileged, if (virQEMUDriverConfigLoadFile(cfg, driverConf, privileged) < 0) goto error; - VIR_FREE(driverConf); if (virQEMUDriverConfigValidate(cfg) < 0) goto error; @@ -829,7 +815,7 @@ qemuStateInitialize(bool privileged, goto error; if (privileged) { - char *channeldir; + VIR_AUTOFREE(char *) channeldir = NULL; if (chown(cfg->libDir, cfg->user, cfg->group) < 0) { virReportSystemError(errno, @@ -882,10 +868,8 @@ qemuStateInitialize(bool privileged, _("unable to set ownership of '%s' to %d:%d"), channeldir, (int)cfg->user, (int)cfg->group); - VIR_FREE(channeldir); goto error; } - VIR_FREE(channeldir); if (chown(cfg->channelTargetDir, cfg->user, cfg->group) < 0) { virReportSystemError(errno, _("unable to set ownership of '%s' to %d:%d"), @@ -936,6 +920,8 @@ qemuStateInitialize(bool privileged, * it, since we can't assume the root mount point has permissions that * will let our spawned QEMU instances use it. */ for (i = 0; i < cfg->nhugetlbfs; i++) { + VIR_AUTOFREE(char *) hugepagePath = NULL; + hugepagePath = qemuGetBaseHugepagePath(&cfg->hugetlbfs[i]); if (!hugepagePath) @@ -951,7 +937,6 @@ qemuStateInitialize(bool privileged, virFileUpdatePerm(cfg->hugetlbfs[i].mnt_dir, 0, S_IXGRP | S_IXOTH) < 0) goto error; - VIR_FREE(hugepagePath); } if (qemuGetMemoryBackingBasePath(cfg, &memoryBackingPath) < 0) @@ -968,7 +953,6 @@ qemuStateInitialize(bool privileged, virFileUpdatePerm(memoryBackingPath, 0, S_IXGRP | S_IXOTH) < 0) goto error; - VIR_FREE(memoryBackingPath); if (!(qemu_driver->closeCallbacks = virCloseCallbacksNew())) goto error; @@ -1033,9 +1017,6 @@ qemuStateInitialize(bool privileged, return VIR_DRV_STATE_INIT_COMPLETE; error: - VIR_FREE(driverConf); - VIR_FREE(hugepagePath); - VIR_FREE(memoryBackingPath); qemuStateCleanup(); return VIR_DRV_STATE_INIT_ERROR; } @@ -1360,8 +1341,8 @@ static int qemuGetSchedInfo(unsigned long long *cpuWait, pid_t pid, pid_t tid) { - char *proc = NULL; - char *data = NULL; + VIR_AUTOFREE(char *) proc = NULL; + VIR_AUTOFREE(char *) data = NULL; char **lines = NULL; size_t i; int ret = -1; @@ -1425,8 +1406,6 @@ qemuGetSchedInfo(unsigned long long *cpuWait, ret = 0; cleanup: - VIR_FREE(data); - VIR_FREE(proc); virStringListFree(lines); return ret; } @@ -1436,7 +1415,7 @@ static int qemuGetProcessInfo(unsigned long long *cpuTime, int *lastCpu, long *vm_rss, pid_t pid, int tid) { - char *proc; + VIR_AUTOFREE(char *) proc = NULL; FILE *pidinfo; unsigned long long usertime = 0, systime = 0; long rss = 0; @@ -1453,7 +1432,6 @@ qemuGetProcessInfo(unsigned long long *cpuTime, int *lastCpu, long *vm_rss, return -1; pidinfo = fopen(proc, "r"); - VIR_FREE(proc); /* See 'man proc' for information about what all these fields are. We're * only interested in a very few of them */ @@ -2905,9 +2883,8 @@ virQEMUSaveDataWrite(virQEMUSaveDataPtr data, size_t len; size_t xml_len; size_t cookie_len = 0; - int ret = -1; size_t zerosLen = 0; - char *zeros = NULL; + VIR_AUTOFREE(char *) zeros = NULL; xml_len = strlen(data->xml) + 1; if (data->cookie) @@ -2919,12 +2896,12 @@ virQEMUSaveDataWrite(virQEMUSaveDataPtr data, if (len > header->data_len) { virReportError(VIR_ERR_OPERATION_FAILED, "%s", _("new xml too large to fit in file")); - goto cleanup; + return -1; } zerosLen = header->data_len - len; if (VIR_ALLOC_N(zeros, zerosLen) < 0) - goto cleanup; + return -1; } else { header->data_len = len; } @@ -2936,14 +2913,14 @@ virQEMUSaveDataWrite(virQEMUSaveDataPtr data, virReportSystemError(errno, _("failed to write header to domain save file '%s'"), path); - goto cleanup; + return -1; } if (safewrite(fd, data->xml, xml_len) != xml_len) { virReportSystemError(errno, _("failed to write domain xml to '%s'"), path); - goto cleanup; + return -1; } if (data->cookie && @@ -2951,21 +2928,17 @@ virQEMUSaveDataWrite(virQEMUSaveDataPtr data, virReportSystemError(errno, _("failed to write cookie to '%s'"), path); - goto cleanup; + return -1; } if (safewrite(fd, zeros, zerosLen) != zerosLen) { virReportSystemError(errno, _("failed to write padding to '%s'"), path); - goto cleanup; + return -1; } - ret = 0; - - cleanup: - VIR_FREE(zeros); - return ret; + return 0; } @@ -3295,7 +3268,7 @@ qemuDomainSaveInternal(virQEMUDriverPtr driver, int compressed, const char *compressedpath, const char *xmlin, unsigned int flags) { - char *xml = NULL; + VIR_AUTOFREE(char *) xml = NULL; bool was_running = false; int ret = -1; virObjectEventPtr event = NULL; @@ -3376,7 +3349,6 @@ qemuDomainSaveInternal(virQEMUDriverPtr driver, if (!(data = virQEMUSaveDataNew(xml, cookie, was_running, compressed, driver->xmlopt))) goto endjob; - xml = NULL; ret = qemuDomainSaveMemory(driver, vm, path, data, compressedpath, flags, QEMU_ASYNC_JOB_SAVE); @@ -3411,7 +3383,6 @@ qemuDomainSaveInternal(virQEMUDriverPtr driver, qemuDomainRemoveInactiveJob(driver, vm); cleanup: - VIR_FREE(xml); virQEMUSaveDataFree(data); virObjectEventStateQueue(driver->domainEventState, event); return ret; @@ -3499,7 +3470,7 @@ qemuDomainSaveFlags(virDomainPtr dom, const char *path, const char *dxml, { virQEMUDriverPtr driver = dom->conn->privateData; int compressed; - char *compressedpath = NULL; + VIR_AUTOFREE(char *) compressedpath = NULL; int ret = -1; virDomainObjPtr vm = NULL; VIR_AUTOUNREF(virQEMUDriverConfigPtr) cfg = NULL; @@ -3528,7 +3499,6 @@ qemuDomainSaveFlags(virDomainPtr dom, const char *path, const char *dxml, cleanup: virDomainObjEndAPI(&vm); - VIR_FREE(compressedpath); return ret; } @@ -3556,9 +3526,9 @@ qemuDomainManagedSave(virDomainPtr dom, unsigned int flags) virQEMUDriverPtr driver = dom->conn->privateData; VIR_AUTOUNREF(virQEMUDriverConfigPtr) cfg = NULL; int compressed; - char *compressedpath = NULL; + VIR_AUTOFREE(char *) compressedpath = NULL; virDomainObjPtr vm; - char *name = NULL; + VIR_AUTOFREE(char *) name = NULL; int ret = -1; virCheckFlags(VIR_DOMAIN_SAVE_BYPASS_CACHE | @@ -3598,8 +3568,6 @@ qemuDomainManagedSave(virDomainPtr dom, unsigned int flags) cleanup: virDomainObjEndAPI(&vm); - VIR_FREE(name); - VIR_FREE(compressedpath); return ret; } @@ -3609,7 +3577,7 @@ qemuDomainManagedSaveLoad(virDomainObjPtr vm, void *opaque) { virQEMUDriverPtr driver = opaque; - char *name; + VIR_AUTOFREE(char *) name = NULL; int ret = -1; virObjectLock(vm); @@ -3622,7 +3590,6 @@ qemuDomainManagedSaveLoad(virDomainObjPtr vm, ret = 0; cleanup: virObjectUnlock(vm); - VIR_FREE(name); return ret; } @@ -3654,7 +3621,7 @@ qemuDomainManagedSaveRemove(virDomainPtr dom, unsigned int flags) virQEMUDriverPtr driver = dom->conn->privateData; virDomainObjPtr vm; int ret = -1; - char *name = NULL; + VIR_AUTOFREE(char *) name = NULL; virCheckFlags(0, -1); @@ -3678,7 +3645,6 @@ qemuDomainManagedSaveRemove(virDomainPtr dom, unsigned int flags) ret = 0; cleanup: - VIR_FREE(name); virDomainObjEndAPI(&vm); return ret; } @@ -3798,7 +3764,7 @@ doCoreDump(virQEMUDriverPtr driver, unsigned int flags = VIR_FILE_WRAPPER_NON_BLOCKING; const char *memory_dump_format = NULL; VIR_AUTOUNREF(virQEMUDriverConfigPtr) cfg = virQEMUDriverGetConfig(driver); - char *compressedpath = NULL; + VIR_AUTOFREE(char *) compressedpath = NULL; /* We reuse "save" flag for "dump" here. Then, we can support the same * format in "save" and "dump". This path doesn't need the compression @@ -3878,7 +3844,6 @@ doCoreDump(virQEMUDriverPtr driver, virFileWrapperFdFree(wrapperFd); if (ret != 0) unlink(path); - VIR_FREE(compressedpath); return ret; } @@ -4004,7 +3969,7 @@ qemuDomainScreenshot(virDomainPtr dom, virQEMUDriverPtr driver = dom->conn->privateData; virDomainObjPtr vm; qemuDomainObjPrivatePtr priv; - char *tmp = NULL; + VIR_AUTOFREE(char *) tmp = NULL; int tmp_fd = -1; size_t i; const char *videoAlias = NULL; @@ -4096,7 +4061,6 @@ qemuDomainScreenshot(virDomainPtr dom, VIR_FORCE_CLOSE(tmp_fd); if (unlink_tmp) unlink(tmp); - VIR_FREE(tmp); qemuDomainObjEndJob(driver, vm); @@ -4110,7 +4074,7 @@ getAutoDumpPath(virQEMUDriverPtr driver, virDomainObjPtr vm) { char *dumpfile = NULL; - char *domname = virDomainDefGetShortName(vm->def); + VIR_AUTOFREE(char *domname) = virDomainDefGetShortName(vm->def); char timestr[100]; struct tm time_info; time_t curtime = time(NULL); @@ -4129,7 +4093,6 @@ getAutoDumpPath(virQEMUDriverPtr driver, domname, timestr)); - VIR_FREE(domname); return dumpfile; } -- 2.21.0 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list