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>
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;
+ const gchar *suffix, *cname = object_class_get_name(oc);
+
+ suffix = g_strstr_len(cname, -1, POWERPC_CPU_TYPE_SUFFIX);
+ ppc_cpu_aliases[i].model = suffix ?
+ g_strndup(cname, (gsize)(suffix - cname)) : g_strdup(cname);
- ppc_cpu_aliases[i].model = g_strdup(object_class_get_name(oc));
- suffix = strstr(ppc_cpu_aliases[i].model, POWERPC_CPU_TYPE_SUFFIX);
- if (suffix) {
- *suffix = 0;
- }
break;
}
}