]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
virHostCPUGetStatsLinux: Avoid 'strcpy'
authorPeter Krempa <pkrempa@redhat.com>
Wed, 3 Mar 2021 10:14:33 +0000 (11:14 +0100)
committerPeter Krempa <pkrempa@redhat.com>
Fri, 5 Mar 2021 14:33:34 +0000 (15:33 +0100)
Use an allocated buffer for 'cpu_header' so that g_strdup(_printf) can
be used to fill it.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
src/util/virhostcpu.c

index 4f6c3390ce758789589e61dc92c5a475a38582ff..2446948bc14d693549beef4ad06afcca0d38330a 100644 (file)
@@ -787,7 +787,7 @@ virHostCPUGetStatsLinux(FILE *procstat,
     char line[1024];
     unsigned long long usr, ni, sys, idle, iowait;
     unsigned long long irq, softirq, steal, guest, guest_nice;
-    char cpu_header[4 + VIR_INT64_STR_BUFLEN];
+    g_autofree char *cpu_header = NULL;
 
     if ((*nparams) == 0) {
         /* Current number of cpu stats supported by linux */
@@ -803,9 +803,9 @@ virHostCPUGetStatsLinux(FILE *procstat,
     }
 
     if (cpuNum == VIR_NODE_CPU_STATS_ALL_CPUS) {
-        strcpy(cpu_header, "cpu ");
+        cpu_header = g_strdup("cpu ");
     } else {
-        g_snprintf(cpu_header, sizeof(cpu_header), "cpu%d ", cpuNum);
+        cpu_header = g_strdup_printf("cpu%d ", cpuNum);
     }
 
     while (fgets(line, sizeof(line), procstat) != NULL) {