From: Peter Krempa Date: Mon, 7 Apr 2025 11:49:21 +0000 (+0200) Subject: virHostCPUGetInfoPopulateLinux: Use automatic memory freeing X-Git-Tag: v11.3.0-rc1~53 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f883dab020a90b890f0e2cbc8b954168159074e2;p=thirdparty%2Flibvirt.git virHostCPUGetInfoPopulateLinux: Use automatic memory freeing Use 'g_autfree' for the two temporary strings. 'sysfs_cpudir' was used in two places, one of which is in a loop. Add another helper variable for it and declare the other one in the loop. Signed-off-by: Peter Krempa Reviewed-by: Michal Privoznik --- diff --git a/src/util/virhostcpu.c b/src/util/virhostcpu.c index 3b4a11effb..d86a12a390 100644 --- a/src/util/virhostcpu.c +++ b/src/util/virhostcpu.c @@ -644,8 +644,8 @@ virHostCPUGetInfoPopulateLinux(FILE *cpuinfo, int threads_per_subcore = 0; unsigned int node; int ret = -1; - char *sysfs_nodedir = NULL; - char *sysfs_cpudir = NULL; + g_autofree char *sysfs_nodedir = NULL; + g_autofree char *sysfs_cpudir_fallback = NULL; int direrr; *mhz = 0; @@ -707,6 +707,8 @@ virHostCPUGetInfoPopulateLinux(FILE *cpuinfo, threads_per_subcore = 0; while ((direrr = virDirRead(nodedir, &nodedirent, sysfs_nodedir)) > 0) { + g_autofree char *sysfs_cpudir = NULL; + if (sscanf(nodedirent->d_name, "node%u", &node) != 1) continue; @@ -723,8 +725,6 @@ virHostCPUGetInfoPopulateLinux(FILE *cpuinfo, &nodethreads, &offline)) < 0) goto cleanup; - VIR_FREE(sysfs_cpudir); - *cpus += nodecpus; if (nodesockets > *sockets) @@ -744,11 +744,9 @@ virHostCPUGetInfoPopulateLinux(FILE *cpuinfo, goto done; fallback: - VIR_FREE(sysfs_cpudir); - - sysfs_cpudir = g_strdup_printf("%s/cpu", SYSFS_SYSTEM_PATH); + sysfs_cpudir_fallback = g_strdup_printf("%s/cpu", SYSFS_SYSTEM_PATH); - if ((nodecpus = virHostCPUParseNode(sysfs_cpudir, arch, + if ((nodecpus = virHostCPUParseNode(sysfs_cpudir_fallback, arch, present_cpus_map, online_cpus_map, threads_per_subcore, @@ -799,8 +797,6 @@ virHostCPUGetInfoPopulateLinux(FILE *cpuinfo, ret = 0; cleanup: - VIR_FREE(sysfs_nodedir); - VIR_FREE(sysfs_cpudir); return ret; }