On 9/29/26 12:14 PM, Jonas Hansen wrote:
On 29 Sep 2026, at 17.43, Jason Merrill <[email protected]> wrote:
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
I’m contributing this patch under DCO 1.1 and certify its terms.
Signed-off-by: Jonas Hansen <[email protected]>
Forge shows PR #232 as merged, although the change appears absent from current
upstream trunk. Could you confirm its status and whether this emailed sign-off
is sufficient, or whether you’d prefer an updated submission?
I'm pretty new to the forge, so I was surprised when the button I pushed
caused it to claim that the patch was merged; I don't know what that
actually means, as I didn't see any effect on even the forge trunk.
I've now added your sign-off to the patch and pushed it. Thanks!
Jason