[PATCH v3 0/2] Fix GCC 16 const-correctness violations

Amit Machhiwal posted 2 patches 1 week, 5 days ago
Failed in applying to current master (apply log)
Maintainers: Nicholas Piggin <npiggin@gmail.com>, Harsh Prateek Bora <harshpb@linux.ibm.com>, Chinmay Rath <rathc@linux.ibm.com>, Glenn Miles <milesg@linux.ibm.com>, Paolo Bonzini <pbonzini@redhat.com>, Fabiano Rosas <farosas@suse.de>, Laurent Vivier <lvivier@redhat.com>
target/ppc/kvm.c       | 11 +++++------
tests/qtest/libqtest.c |  9 ++++-----
2 files changed, 9 insertions(+), 11 deletions(-)
[PATCH v3 0/2] Fix GCC 16 const-correctness violations
Posted by Amit Machhiwal 1 week, 5 days ago
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/20260511131018.102168-1-vaibhav@linux.ibm.com/

Changes in v2:
- Avoid pointer aliasing
- Handle memory allocation failure error
- v1: https://lore.kernel.org/all/20260504134344.38958-1-amachhiw@linux.ibm.com

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
-- 
2.50.1 (Apple Git-155)
Re: [PATCH v3 0/2] Fix GCC 16 const-correctness violations
Posted by Aditya Gupta 1 week, 1 day ago
On 18/05/26 22:55, Amit Machhiwal wrote:

> 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.

Reviewed-by: Aditya Gupta <adityag@linux.ibm.com>


Thanks Amit !
Re: [PATCH v3 0/2] Fix GCC 16 const-correctness violations
Posted by Anushree Mathur 1 week, 4 days ago

On 18/05/26 10:55 PM, Amit Machhiwal wrote:
> 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/20260511131018.102168-1-vaibhav@linux.ibm.com/
>
> Changes in v2:
> - Avoid pointer aliasing
> - Handle memory allocation failure error
> - v1: https://lore.kernel.org/all/20260504134344.38958-1-amachhiw@linux.ibm.com
>
> 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

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:

Tested-by: Anushree Mathur <anushree.mathur@linux.ibm.com>

Thank you,
Anushree Mathur