Skip to contentSkip to conversation

target/ppc/kvm: Fix const violation when trimming CPU alias suffix

PATCH Superseded Amit Machhiwal 2 participants 2 messages May 8 KVM

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 target/ppc/kvm: Fix const violation when trimming CPU alias suffix

GCC 16 tightens diagnostics around const correctness and now correctly rejects attempts to modify strings referenced through const-qualified pointers. In kvm_ppc_register_host_cpu_type(), ppc_cpu_aliases[i].model is declared as const char *, but the code was using strstr() on it and then modifying the returned pointer in place to strip POWERPC_CPU_TYPE_SUFFIX.

This results in a write through a pointer derived from const data, triggering a build failure with GCC 16:

error: assignment discards 'const' qualifier from pointer target type [-Werror=discarded-qualifiers]

        suffix = strstr(ppc_cpu_aliases[i].model, POWERPC_CPU_TYPE_SUFFIX);
               ^

Fix this by duplicating the model string into a mutable buffer using g_strdup(), trimming the suffix on that temporary buffer, and only then storing it in the alias table.

This preserves the existing behavior while avoiding modification of const data and ensures compatibility with newer compilers.

No functional change intended.

---
Changes in v2:
* trim the duplicated buffer before assigning it to ppc_cpu_aliases[i].model
---

 target/ppc/kvm.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
target/ppc/kvm.c +4 -3
index 25c28ad089c6..c2843b1421cb 100644
@@ -2654,13 +2654,14 @@ static int kvm_ppc_register_host_cpu_type(void)
     dc = DEVICE_CLASS(ppc_cpu_get_family_class(pvr_pcc));
     for (i = 0; ppc_cpu_aliases[i].alias != NULL; i++) {
         if (g_ascii_strcasecmp(ppc_cpu_aliases[i].alias, dc->desc) == 0) {
-            char *suffix;
+            char *model, *suffix;
 
-            ppc_cpu_aliases[i].model = g_strdup(object_class_get_name(oc));
-            suffix = strstr(ppc_cpu_aliases[i].model, POWERPC_CPU_TYPE_SUFFIX);
+            model = g_strdup(object_class_get_name(oc));
+            suffix = strstr(model, POWERPC_CPU_TYPE_SUFFIX);
             if (suffix) {
                 *suffix = 0;
             }
+            ppc_cpu_aliases[i].model = model;
             break;
         }
     }

base-commit: ee7eb612be8f8886d48c1d0c1f1c65e495138f83

Signature · 1 line
2.50.1 (Apple Git-155)
Reviewed-by on v2 1/1
#1 lore raw
Quoted 45 lines · Amit Machhiwal

GCC 16 tightens diagnostics around const correctness and now correctly rejects attempts to modify strings referenced through const-qualified pointers. In kvm_ppc_register_host_cpu_type(), ppc_cpu_aliases[i].model is declared as const char *, but the code was using strstr() on it and then modifying the returned pointer in place to strip POWERPC_CPU_TYPE_SUFFIX.

This results in a write through a pointer derived from const data, triggering a build failure with GCC 16:

error: assignment discards 'const' qualifier from pointer target type [-Werror=discarded-qualifiers]

        suffix = strstr(ppc_cpu_aliases[i].model, POWERPC_CPU_TYPE_SUFFIX);
               ^

Fix this by duplicating the model string into a mutable buffer using g_strdup(), trimming the suffix on that temporary buffer, and only then storing it in the alias table.

This preserves the existing behavior while avoiding modification of const data and ensures compatibility with newer compilers.

No functional change intended.

---
Changes in v2:
* trim the duplicated buffer before assigning it to ppc_cpu_aliases[i].model
---

 target/ppc/kvm.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
target/ppc/kvm.c +1 -2
index 25c28ad089c6..c2843b1421cb 100644
@@ -2654,13 +2654,14 @@ static int kvm_ppc_register_host_cpu_type(void)
     dc = DEVICE_CLASS(ppc_cpu_get_family_class(pvr_pcc));
     for (i = 0; ppc_cpu_aliases[i].alias != NULL; i++) {
         if (g_ascii_strcasecmp(ppc_cpu_aliases[i].alias, dc->desc) == 0) {
-            char *suffix;
+            char *model, *suffix;
 
-            ppc_cpu_aliases[i].model = g_strdup(object_class_get_name(oc));

target/ppc/kvm.c static int kvm_ppc_register_host_cpu_type(void) · line 2667

Quoted 10 lines
target/ppc/kvm.c +3 -1
-            suffix = strstr(ppc_cpu_aliases[i].model, POWERPC_CPU_TYPE_SUFFIX);
+            model = g_strdup(object_class_get_name(oc));
+            suffix = strstr(model, POWERPC_CPU_TYPE_SUFFIX);
             if (suffix) {
                 *suffix = 0;
             }
+            ppc_cpu_aliases[i].model = model;
             break;
         }
     }
Quoted 5 lines

base-commit: ee7eb612be8f8886d48c1d0c1f1c65e495138f83

Signature · 1 line
2.50.1 (Apple Git-155)