[libvirt PATCH 0/3] build fix: more g_autofree refactors

Ján Tomko (3): virmacmap: Use g_autofree in virMacMapWriteFileLocked vz: use g_autofree in prlsdkConvertCpuInfo aa-helper: use g_autofree in create_profile src/security/virt-aa-helper.c | 45 +++++++++++++---------------------- src/util/virmacmap.c | 12 ++++------ src/vz/vz_sdk.c | 22 +++++++---------- 3 files changed, 30 insertions(+), 49 deletions(-) -- 2.24.1

Signed-off-by: Ján Tomko <jtomko@redhat.com> --- src/util/virmacmap.c | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/util/virmacmap.c b/src/util/virmacmap.c index 0c0ba90def..4a57edede8 100644 --- a/src/util/virmacmap.c +++ b/src/util/virmacmap.c @@ -261,19 +261,15 @@ static int virMacMapWriteFileLocked(virMacMapPtr mgr, const char *file) { - char *str; - int ret = -1; + g_autofree char *str = NULL; if (virMacMapDumpStrLocked(mgr, &str) < 0) - goto cleanup; + return -1; if (virFileRewriteStr(file, 0644, str) < 0) - goto cleanup; + return -1; - ret = 0; - cleanup: - VIR_FREE(str); - return ret; + return 0; } -- 2.24.1

Convert the function to use g_autofree to silence -Wmaybe-uninitialized. Signed-off-by: Ján Tomko <jtomko@redhat.com> --- src/vz/vz_sdk.c | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/src/vz/vz_sdk.c b/src/vz/vz_sdk.c index c49204de1d..9cee6f1fde 100644 --- a/src/vz/vz_sdk.c +++ b/src/vz/vz_sdk.c @@ -1456,44 +1456,40 @@ prlsdkConvertCpuInfo(PRL_HANDLE sdkdom, virDomainDefPtr def, virDomainXMLOptionPtr xmlopt) { - char *buf; + g_autofree char *buf = NULL; int hostcpus; PRL_UINT32 cpuCount; PRL_RESULT pret; - int ret = -1; if ((hostcpus = virHostCPUGetCount()) < 0) - goto cleanup; + return -1; /* get number of CPUs */ pret = PrlVmCfg_GetCpuCount(sdkdom, &cpuCount); - prlsdkCheckRetGoto(pret, cleanup); + prlsdkCheckRetExit(pret, -1); if (cpuCount > hostcpus) cpuCount = hostcpus; if (virDomainDefSetVcpusMax(def, cpuCount, xmlopt) < 0) - goto cleanup; + return -1; if (virDomainDefSetVcpus(def, cpuCount) < 0) - goto cleanup; + return -1; if (!(buf = prlsdkGetStringParamVar(PrlVmCfg_GetCpuMask, sdkdom))) - goto cleanup; + return -1; if (strlen(buf) == 0) { if (!(def->cpumask = virBitmapNew(hostcpus))) - goto cleanup; + return -1; virBitmapSetAll(def->cpumask); } else { if (virBitmapParse(buf, &def->cpumask, hostcpus) < 0) - goto cleanup; + return -1; } - ret = 0; - cleanup: - VIR_FREE(buf); - return ret; + return 0; } static int -- 2.24.1

'template' might be used uninitialized. Use g_autofree for everything and remove all the custom labels. Signed-off-by: Ján Tomko <jtomko@redhat.com> --- src/security/virt-aa-helper.c | 45 +++++++++++++---------------------- 1 file changed, 17 insertions(+), 28 deletions(-) diff --git a/src/security/virt-aa-helper.c b/src/security/virt-aa-helper.c index b6f58efdea..8526b7b985 100644 --- a/src/security/virt-aa-helper.c +++ b/src/security/virt-aa-helper.c @@ -264,22 +264,21 @@ static int create_profile(const char *profile, const char *profile_name, const char *profile_files, int virtType) { - char *template; - char *tcontent = NULL; - char *pcontent = NULL; - char *replace_name = NULL; - char *replace_files = NULL; + g_autofree char *template = NULL; + g_autofree char *tcontent = NULL; + g_autofree char *pcontent = NULL; + g_autofree char *replace_name = NULL; + g_autofree char *replace_files = NULL; char *tmp = NULL; const char *template_name = "\nprofile LIBVIRT_TEMPLATE"; const char *template_end = "\n}"; int tlen, plen; int fd; - int rc = -1; const char *driver_name = NULL; if (virFileExists(profile)) { vah_error(NULL, 0, _("profile exists")); - goto end; + return -1; } switch (virtType) { @@ -296,22 +295,22 @@ create_profile(const char *profile, const char *profile_name, if (!virFileExists(template)) { vah_error(NULL, 0, _("template does not exist")); - goto end; + return -1; } if ((tlen = virFileReadAll(template, MAX_FILE_LEN, &tcontent)) < 0) { vah_error(NULL, 0, _("failed to read AppArmor template")); - goto end; + return -1; } if (strstr(tcontent, template_name) == NULL) { vah_error(NULL, 0, _("no replacement string in template")); - goto clean_tcontent; + return -1; } if (strstr(tcontent, template_end) == NULL) { vah_error(NULL, 0, _("no replacement string in template")); - goto clean_tcontent; + return -1; } /* '\nprofile <profile_name>\0' */ @@ -328,15 +327,15 @@ create_profile(const char *profile, const char *profile_name, if (plen > MAX_FILE_LEN || plen < tlen) { vah_error(NULL, 0, _("invalid length for new profile")); - goto clean_replace; + return -1; } if (!(pcontent = virStringReplace(tcontent, template_name, replace_name))) - goto clean_all; + return -1; if (virtType != VIR_DOMAIN_VIRT_LXC) { if (!(tmp = virStringReplace(pcontent, template_end, replace_files))) - goto clean_all; + return -1; VIR_FREE(pcontent); pcontent = g_steal_pointer(&tmp); } @@ -344,31 +343,21 @@ create_profile(const char *profile, const char *profile_name, /* write the file */ if ((fd = open(profile, O_CREAT | O_EXCL | O_WRONLY, 0644)) == -1) { vah_error(NULL, 0, _("failed to create profile")); - goto clean_all; + return -1; } if (safewrite(fd, pcontent, plen - 1) < 0) { /* don't write the '\0' */ VIR_FORCE_CLOSE(fd); vah_error(NULL, 0, _("failed to write to profile")); - goto clean_all; + return -1; } if (VIR_CLOSE(fd) != 0) { vah_error(NULL, 0, _("failed to close or write to profile")); - goto clean_all; + return -1; } - rc = 0; - clean_all: - VIR_FREE(pcontent); - clean_replace: - VIR_FREE(replace_name); - VIR_FREE(replace_files); - clean_tcontent: - VIR_FREE(tcontent); - end: - VIR_FREE(template); - return rc; + return 0; } /* -- 2.24.1
participants (2)
-
Ján Tomko
-
Peter Krempa