@samitouri / QOSamiQemu / commits / f665bd5fd7

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 changing suffix to 'const gchar *' and using g_strstr_len() to locate the suffix, then allocating a new string with g_strndup() (to copy only the prefix) or g_strdup() (to copy the entire name if no suffix exists). This maintains const correctness throughout while preserving the original functionality. No functional change intended. Signed-off-by: Vaibhav Jain <vaibhav@linux.ibm.com> Signed-off-by: Amit Machhiwal <amachhiw@linux.ibm.com> Tested-by: Anushree Mathur <anushree.mathur@linux.ibm.com> Reviewed-by: Aditya Gupta <adityag@linux.ibm.com> Link: https://lore.kernel.org/qemu-devel/20260518172517.12466-2-amachhiw@linux.ibm.com Signed-off-by: Harsh Prateek Bora <harshpb@linux.ibm.com>

Amit Machhiwal committed May 18, 2026 at 22:55 UTC f665bd5fd79e2c915eee1595a1c5c1fa8d549b77
1 file changed +5 -6
target/ppc/kvm.c
+5 -6
@@ -2654,13 +2654,12 @@ static int kvm_ppc_register_host_cpu_type(void)
2654 dc = DEVICE_CLASS(ppc_cpu_get_family_class(pvr_pcc));
2655 for (i = 0; ppc_cpu_aliases[i].alias != NULL; i++) {
2656 if (g_ascii_strcasecmp(ppc_cpu_aliases[i].alias, dc->desc) == 0) {
2657 - char *suffix;
2657 + const gchar *suffix, *cname = object_class_get_name(oc);
2658 +
2659 + suffix = g_strstr_len(cname, -1, POWERPC_CPU_TYPE_SUFFIX);
2660 + ppc_cpu_aliases[i].model = suffix ?
2661 + g_strndup(cname, (gsize)(suffix - cname)) : g_strdup(cname);
2662
2659 - ppc_cpu_aliases[i].model = g_strdup(object_class_get_name(oc));
2660 - suffix = strstr(ppc_cpu_aliases[i].model, POWERPC_CPU_TYPE_SUFFIX);
2661 - if (suffix) {
2662 - *suffix = 0;
2663 - }
2663 break;
2664 }
2665 }