On 9/28/26 4:17 AM, Jonas Hansen via Sourceware Forge wrote:
From: Jonas Hansen <[email protected]>

The call to braced_lists_to_strings in store_init_value does not look
through TARGET_EXPR. A constexpr aggregate initialized through one can
therefore retain a character array CONSTRUCTOR after constant evaluation.
The middle end then treats an element of that array as a single-character
string, causing a false -Wformat-truncation warning for a bounded snprintf
call that reads only the remaining array elements.

Move the existing conversion after constant folding, next to
replace_placeholders, so it sees the folded initializer while remaining
before split_nonconstant_init. Add diagnostic and runtime regressions
covering bounded reads, overreads, truncation, constexpr copy and mutation,
templates, embedded nuls, and implicit zero initialization.

Tested on x86_64-pc-linux-gnu with related C++ format, initialization,
constexpr, narrowing, template, string and #embed tests: 14,435 expected
passes and no unexpected results across two test selections. C++ self-tests
and the runtime regression, including LTO, also passed. A full bootstrap
and complete testsuite run have not been performed.

gcc/cp/ChangeLog:

        PR tree-optimization/127416
        * typeck2.cc (store_init_value): Move braced_lists_to_strings after
        constant folding.

gcc/testsuite/ChangeLog:

        PR tree-optimization/127416
        * g++.dg/warn/Wformat-truncation-nonstring.C: New test.
        * g++.dg/torture/constexpr-array-string.C: New test.
The patch looks good, but do you intend to submit this under an FSF copyright assignment or the DCO? I don't see either currently.

https://gcc.gnu.org/contribute.html#legal

Jason

Reply via email to