Skip to contentSkip to conversation

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

PATCH Superseded Amit Machhiwal 3 participants 4 messages May 4 – May 11 KVM

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 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 defined 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(), storing it in the alias table, and then performing the suffix truncation on the mutable copy.

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

No functional change intended.

---
 target/ppc/kvm.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
target/ppc/kvm.c +4 -2
index 25c28ad089c6..e71e5c0117da 100644
@@ -2654,10 +2654,12 @@ 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 *model;
             char *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));
+            ppc_cpu_aliases[i].model = model;
+            suffix = strstr(model, POWERPC_CPU_TYPE_SUFFIX);
             if (suffix) {
                 *suffix = 0;
             }

base-commit: ac0cc20ad2fe0b8df2e5d9458e90a095ac711ab1

Signature · 1 line
2.50.1 (Apple Git-155)
#1 lore raw
Quoted 38 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 defined 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(), storing it in the alias table, and then performing the suffix truncation on the mutable copy.

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

No functional change intended.

---
 target/ppc/kvm.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
target/ppc/kvm.c +1 -0
index 25c28ad089c6..e71e5c0117da 100644
@@ -2654,10 +2654,12 @@ 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 *model;

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

Quoted 10 lines
target/ppc/kvm.c +3 -2
             char *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));
+            ppc_cpu_aliases[i].model = model;
+            suffix = strstr(model, POWERPC_CPU_TYPE_SUFFIX);
             if (suffix) {
                 *suffix = 0;
             }

A const char * variable is ideally supposed to point to an immutable string. But even with this fix, the string that "ppc_cpu_aliases[i].model" points to is being changed after assignment.

Would the below diff (untested) be a better fix?

target/ppc/kvm.c +4 -3
index 41bd03ec2a..a84e4b4636 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 (strcasecmp(ppc_cpu_aliases[i].alias, dc->desc) == 0) {
-            char *suffix;
+            char *suffix, *model;
 
-            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;
         }
     }
#2 lore raw

Hi Gautam,

Thanks for taking a look. Please find my response inline below:

Quoted 40 lines · Gautam Menghani
Quoted 39 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 defined 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(), storing it in the alias table, and then performing the suffix truncation on the mutable copy.

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

No functional change intended.

---
 target/ppc/kvm.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
target/ppc/kvm.c +1 -0
index 25c28ad089c6..e71e5c0117da 100644
@@ -2654,10 +2654,12 @@ 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 *model;
             char *suffix;
 
Quoted 13 lines

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

Quoted 9 lines
target/ppc/kvm.c +3 -2
-            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));
+            ppc_cpu_aliases[i].model = model;
+            suffix = strstr(model, POWERPC_CPU_TYPE_SUFFIX);
             if (suffix) {
                 *suffix = 0;
             }

A const char * variable is ideally supposed to point to an immutable string. But even with this fix, the string that "ppc_cpu_aliases[i].model" points to is being changed after assignment.

Thanks, I get your point. The write in my version is to the mutable buffer returned by g_strdup(), so it is not strictly a const write. I had originally trimmed the duplicated buffer before assigning it to ppc_cpu_aliases[i].model, but later reordered it to stay closer to the existing flow. Still, I agree with the suggested cleaner ordering. I will update it in the next revision.

Thanks,
Amit

Quoted 24 lines

Would the below diff (untested) be a better fix?

target/ppc/kvm.c +4 -3
index 41bd03ec2a..a84e4b4636 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 (strcasecmp(ppc_cpu_aliases[i].alias, dc->desc) == 0) {
-            char *suffix;
+            char *suffix, *model;
 
-            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;
         }
     }
#3 lore raw
In reply to Amit Machhiwal

Hi Amit,

Thanks for reporting this an proposing a fix. My review comments below:

Quoted 7 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 defined 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.

Quoted 7 lines

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);
               ^

This looks more like an aliasing issue for the pointer returned by g_strdup and it being used between const and non-const contexts.

Quoted 6 lines

Fix this by duplicating the model string into a mutable buffer using g_strdup(), storing it in the alias table, and then performing the suffix truncation on the mutable copy.

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

I think the better approach would be to avoid modifying the value returned by g_strdup before its assigned to 'ppc_cpu_alias[i].model' struct member.

Quoted 13 lines

No functional change intended.

---
 target/ppc/kvm.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
target/ppc/kvm.c +0 -0
index 25c28ad089c6..e71e5c0117da 100644

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

Quoted 9 lines
target/ppc/kvm.c +2 -2
@@ -2654,10 +2654,12 @@ 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 *model;
             char *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));

while we are at it lets also handle ENOMEM error and trickle it back to arch_kvm_init()

Quoted 11 lines
target/ppc/kvm.c +2 -0
+            ppc_cpu_aliases[i].model = model;
+            suffix = strstr(model, POWERPC_CPU_TYPE_SUFFIX);
             if (suffix) {
                 *suffix = 0;
             }

base-commit: ac0cc20ad2fe0b8df2e5d9458e90a095ac711ab1

Signature · 1 line
2.50.1 (Apple Git-155)

I have done the above proposed changes in a separate patch which i will post in some time.

Signature · 2 lines
Cheers
~ Vaibhav