* [PATCH v2] target/ppc/kvm: Fix const violation when trimming CPU alias suffix
@ 2026-05-08 8:14 Amit Machhiwal
2026-05-08 14:40 ` Gautam Menghani
0 siblings, 1 reply; 2+ messages in thread
From: Amit Machhiwal @ 2026-05-08 8:14 UTC (permalink / raw)
To: Harsh Prateek Bora, qemu-ppc
Cc: Vaibhav Jain, Amit Machhiwal, Chinmay Rath, Glenn Miles,
Paolo Bonzini, Nicholas Piggin, qemu-devel, kvm
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.
Signed-off-by: Amit Machhiwal <amachhiw@linux.ibm.com>
---
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(-)
diff --git a/target/ppc/kvm.c b/target/ppc/kvm.c
index 25c28ad089c6..c2843b1421cb 100644
--- a/target/ppc/kvm.c
+++ b/target/ppc/kvm.c
@@ -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
--
2.50.1 (Apple Git-155)
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH v2] target/ppc/kvm: Fix const violation when trimming CPU alias suffix
2026-05-08 8:14 [PATCH v2] target/ppc/kvm: Fix const violation when trimming CPU alias suffix Amit Machhiwal
@ 2026-05-08 14:40 ` Gautam Menghani
0 siblings, 0 replies; 2+ messages in thread
From: Gautam Menghani @ 2026-05-08 14:40 UTC (permalink / raw)
To: Amit Machhiwal
Cc: Harsh Prateek Bora, qemu-ppc, Vaibhav Jain, Chinmay Rath,
Glenn Miles, Paolo Bonzini, Nicholas Piggin, qemu-devel, kvm
On Fri, May 08, 2026 at 01:44:16PM +0530, Amit Machhiwal wrote:
> 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.
>
> Signed-off-by: Amit Machhiwal <amachhiw@linux.ibm.com>
> ---
> 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(-)
>
> diff --git a/target/ppc/kvm.c b/target/ppc/kvm.c
> index 25c28ad089c6..c2843b1421cb 100644
> --- a/target/ppc/kvm.c
> +++ b/target/ppc/kvm.c
> @@ -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;
> }
> }
>
Reviewed-by: Gautam Menghani <gautam@linux.ibm.com>
> base-commit: ee7eb612be8f8886d48c1d0c1f1c65e495138f83
> --
> 2.50.1 (Apple Git-155)
>
>
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-05-08 14:40 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-05-08 8:14 [PATCH v2] target/ppc/kvm: Fix const violation when trimming CPU alias suffix Amit Machhiwal
2026-05-08 14:40 ` Gautam Menghani
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox