Fix GCC 16 const-correctness violations
v3 cover Fix GCC 16 const-correctness violations
GCC 16 has tightened const-correctness checking and now correctly rejects attempts to modify or assign const-qualified pointers without proper qualification. This series fixes two such violations that cause build failures with GCC 16 when using -Werror=discarded-qualifiers.
Both patches follow the same approach: changing local variables to const-qualified types and using GLib string functions (g_strdup, g_strndup, g_strstr_len, g_getenv) that work correctly with const strings, eliminating the need for in-place modifications.
Changes in v3:
- Harsh: g_strndup will abort the program in case of OOM eventually making it a dead code. Additionally, typecase (suffix - cname). Also, unlikely() is not needed.
- Remove error handling (GLib functions abort on allocation failure)
- Remove unlikely() macro (provides no benefit in initialization code)
- Typecast (suffix - cname)
- Update the commit messages accordingly
- v2: https://lore.kernel.org/all/[email protected]/
Changes in v2:
- Avoid pointer aliasing
- Handle memory allocation failure error
- v1: https://lore.kernel.org/all/[email protected]
Tested on:
- GCC 16.1.1 (no warnings with -Werror=discarded-qualifiers)
- GCC 15.2.1 (backward compatible)
Amit Machhiwal (2):
target/ppc/kvm: Fix const violation when trimming CPU alias suffix tests/qtest/libqtest: Fix const violation in qtest_verbose()
target/ppc/kvm.c | 11 +++++------ tests/qtest/libqtest.c | 9 ++++----- 2 files changed, 9 insertions(+), 11 deletions(-)
base-commit: ac6721b88df944ade0048822b2b74210f543d656
Signature · 1 line
2.50.1 (Apple Git-155)
PATCH v3 1/2 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.
--- target/ppc/kvm.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-)
target/ppc/kvm.c +5 -6
@@ -2654,13 +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 *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 ; } }
Signature · 1 line
2.50.1 (Apple Git-155)
PATCH v3 2/2 tests/qtest/libqtest: Fix const violation in qtest_verbose()
In reply to Amit MachhiwalGCC 16 tightens const-correctness checking and reports:
error: assignment discards 'const' qualifier from pointer target type [-Werror=discarded-qualifiers]
found = strstr(log, domain); ^
in qtest_verbose() because strstr() returns a non-const pointer while operating on a const-qualified string from getenv().
While getenv() returns char *, the returned environment string is semantically treated as read-only. Use g_getenv() and g_strstr_len() so that the local string pointers remain const-qualified throughout. This maintains const correctness throughout and aligns with QEMU's use of GLib string functions.
No functional change intended.
--- tests/qtest/libqtest.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)
tests/qtest/libqtest.c +4 -5
@@ -2145,8 +2145,7 @@ bool mkimg(const char *file, const char *fmt, unsigned size_mb)bool qtest_verbose (const char *domain) { -const char *log =getenv ("QTEST_LOG" ); -char *found; +const gchar *found, *log =g_getenv ("QTEST_LOG" );assert (domain);
@@ -2172,11 +2171,11 @@ bool qtest_verbose(const char *domain)* QTEST_LOG=<domain1>,-<domain2> (only false for domain2) * allows other separators, except - and + */ - found =strstr (log, domain); + found =g_strstr_len (log,-1 , domain);if (found) {/* reject options given twice */ -assert (!strstr (found +strlen (domain), domain)); +assert (!g_strstr_len (found +strlen (domain),-1 , domain));if (found > log) {ptrdiff_t i = found - log -1 ;
@@ -2190,7 +2189,7 @@ bool qtest_verbose(const char *domain)* If filtering out a specific domain, all others are * enabled. */ -return !!strstr (log,"-" ); +return !!g_strstr_len (log,-1 ,"-" ); } }
Signature · 1 line
2.50.1 (Apple Git-155)
Quoted 33 lines · Amit Machhiwal
GCC 16 has tightened const-correctness checking and now correctly rejects attempts to modify or assign const-qualified pointers without proper qualification. This series fixes two such violations that cause build failures with GCC 16 when using -Werror=discarded-qualifiers.
Both patches follow the same approach: changing local variables to const-qualified types and using GLib string functions (
g_strdup,g_strndup,g_strstr_len,g_getenv) that work correctly with const strings, eliminating the need for in-place modifications.Changes in v3:
- Harsh:g_strndupwill abort the program in case of OOM eventually making it a dead code. Additionally, typecase (suffix - cname). Also,unlikely()is not needed.
- Remove error handling (GLib functions abort on allocation failure)
- Removeunlikely()macro (provides no benefit in initialization code)
- Typecast (suffix - cname)
- Update the commit messages accordingly
- v2: https://lore.kernel.org/all/[email protected]/Changes in v2:
- Avoid pointer aliasing
- Handle memory allocation failure error
- v1: https://lore.kernel.org/all/[email protected]Tested on:
- GCC 16.1.1 (no warnings with -Werror=discarded-qualifiers)
- GCC 15.2.1 (backward compatible)Amit Machhiwal (2):
target/ppc/kvm: Fix const violation when trimming CPU alias suffix tests/qtest/libqtest: Fix const violation inqtest_verbose()
Quoted 6 lines
target/ppc/kvm.c | 11 +++++------ tests/qtest/libqtest.c | 9 ++++----- 2 files changed, 9 insertions(+), 11 deletions(-)base-commit:
ac6721b88df944ade0048822b2b74210f543d656
Hi Amit,
I have tested this patch and it has fixed the qemu build failure. Here is my analysis:
[root@localhost build]# gcc --version gcc (GCC) 16.1.1 20260501 (Red Hat 16.1.1-1)
Before applying the patch, when I tried to build qemu it failed:
[1431/2891] Compiling C object tests/qtest/libqos/libqos.a.p/.._libqtest.c.o
FAILED: [code=1] tests/qtest/libqos/libqos.a.p/.._libqtest.c.o
cc -m64 -mlittle-endian -Itests/qtest/libqos/libqos.a.p -Itests/qtest/libqos -I../tests/qtest/libqos -I. -Iqapi -Itrace -Iui -Iui/shader -I/usr/include/glib-2.0 -I/usr/lib64/glib-2.0/include -I/usr/include/sysprof-6 -fdiagnostics-color=auto -Wall -Winvalid-pch -Werror -std=gnu11 -O2 -g -fno-link-libatomic -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -fno-strict-aliasing -fno-common -fwrapv -ftrivial-auto-var-init=zero -fzero-init-padding-bits=all -fzero-call-used-regs=used-gpr -fstack-protector-strong -Wempty-body -Wendif-labels -Wexpansion-to-defined -Wformat-overflow=2 -Wformat-security -Wformat-y2k -Wignored-qualifiers -Wimplicit-fallthrough=2 -Winit-self -Wmissing-format-attribute -Wmissing-prototypes -Wnested-externs -Wold-style-declaration -Wold-style-definition -Wredundant-decls -Wshadow=local -Wstrict-prototypes -Wtype-limits -Wundef -Wvla -Wwrite-strings -Wno-missing-include-dirs -Wno-psabi -Wno-shift-negative-value -isystem /root/qemu/linux-headers -isystem linux-headers -iquote . -iquote /root/qemu -iquote /root/qemu/include -iquote /root/qemu/host/include/ppc64 -iquote /root/qemu/host/include/generic -iquote /root/qemu/tcg/ppc64 -pthread -fPIE -MD -MQ tests/qtest/libqos/libqos.a.p/.._libqtest.c.o -MF tests/qtest/libqos/libqos.a.p/.._libqtest.c.o.d -o tests/qtest/libqos/libqos.a.p/.._libqtest.c.o -c ../tests/qtest/libqtest.c ../tests/qtest/libqtest.c: In function ‘qtest_verbose’:../tests/qtest/libqtest.c:2175:15: error: assignment discards ‘const’ qualifier from pointer target type [-Werror=discarded-qualifiers]
2175 | found = strstr(log, domain);
| ^
cc1: all warnings being treated as errors
ninja: build stopped: subcommand failed.
make: *** [Makefile:170: run-ninja] Error 1
After applying the patch, building qemu worked perfectly fine.
Please feel free to add:
Thank you,
Anushree Mathur
Quoted 4 lines · Amit Machhiwal
GCC 16 has tightened const-correctness checking and now correctly rejects attempts to modify or assign const-qualified pointers without proper qualification. This series fixes two such violations that cause build failures with GCC 16 when using -Werror=discarded-qualifiers.
The patches fixes qemu build for me, and looks good to me.
Thanks Amit !