Kernel KVM-PPC virtualization development
 help / color / mirror / Atom feed
From: Vaibhav Jain <vaibhav@linux.ibm.com>
To: qemu-devel@nongnu.org, kvm-ppc@vger.kernel.org, qemu-ppc@nongnu.org
Cc: Vaibhav Jain <vaibhav@linux.ibm.com>,
	Amit Machhiwal <amachhiw@linux.ibm.com>,
	Chinmay Rath <rathc@linux.ibm.com>,
	Glenn Miles <milesg@linux.ibm.com>,
	Paolo Bonzini <pbonzini@redhat.com>,
	Shivaprasad G Bhat <sbhat@linux.ibm.com>,
	Harsh Prateek Bora <harshpb@linux.ibm.com>
Subject: [PATCH v2] target/ppc/kvm: Fix const violation when trimming CPU alias suffix
Date: Mon, 11 May 2026 18:40:16 +0530	[thread overview]
Message-ID: <20260511131018.102168-1-vaibhav@linux.ibm.com> (raw)

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 copying only the needed suffix from the object class-name to
'ppc_cpu_aliases.model' using g_strdup and g_strndup, which maintains the
constness of the allocated buffer assigned to 'ppc_cpu_aliases.model'
member of the 'ppc_cpu_aliases' struct array.

The patch also adds error handling for possible memory allocation failure
while calling g_str{n}dup functions so that the error is properly
propogated back from kvm_arch_init().

Signed-off-by: Amit Machhiwal <amachhiw@linux.ibm.com>
Signed-off-by: Vaibhav Jain (IBM) <vaibhav@linux.ibm.com>

---
Change-log:

v1->v2:
https://lore.kernel.org/all/20260504134344.38958-1-amachhiw@linux.ibm.com
* Avoid pointer aliasing
* Handle memory allocation failure error
---
 target/ppc/kvm.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)

diff --git a/target/ppc/kvm.c b/target/ppc/kvm.c
index 25c28ad089..42cd4f4fff 100644
--- a/target/ppc/kvm.c
+++ b/target/ppc/kvm.c
@@ -170,9 +170,8 @@ int kvm_arch_init(MachineState *ms, KVMState *s)
 
     cap_rpt_invalidate = kvm_vm_check_extension(s, KVM_CAP_PPC_RPT_INVALIDATE);
     cap_ail_mode_3 = kvm_vm_check_extension(s, KVM_CAP_PPC_AIL_MODE_3);
-    kvm_ppc_register_host_cpu_type();
 
-    return 0;
+    return kvm_ppc_register_host_cpu_type();
 }
 
 int kvm_arch_irqchip_create(KVMState *s)
@@ -2654,14 +2653,17 @@ 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;
+            const gchar *suffix, *cname = object_class_get_name(oc);
+
+            suffix = g_strstr_len(cname, -1, POWERPC_CPU_TYPE_SUFFIX);
+            ppc_cpu_aliases[i].model = unlikely(suffix) ?
+                g_strndup(cname, 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;
+            if (!ppc_cpu_aliases[i].model) {
+                return -ENOMEM;
+            } else {
+                return 0;
             }
-            break;
         }
     }
 
-- 
2.54.0


             reply	other threads:[~2026-05-11 13:10 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-11 13:10 Vaibhav Jain [this message]
2026-05-12  8:38 ` [PATCH v2] target/ppc/kvm: Fix const violation when trimming CPU alias suffix Harsh Prateek Bora

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260511131018.102168-1-vaibhav@linux.ibm.com \
    --to=vaibhav@linux.ibm.com \
    --cc=amachhiw@linux.ibm.com \
    --cc=harshpb@linux.ibm.com \
    --cc=kvm-ppc@vger.kernel.org \
    --cc=milesg@linux.ibm.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-ppc@nongnu.org \
    --cc=rathc@linux.ibm.com \
    --cc=sbhat@linux.ibm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox