Attached is a minimal patch to avoid an ICE in CHKP upon encountering one form of an initializer for a flexible array member, specifically the empty string:
int f () { struct B { int n; char a[]; }; return ((struct B){ 1, "" }).a[0]; } Although GCC accepts (and doesn't ICE on) non-empty initializers for flexible array members, such as (struct B){ 1, "123" } it generates wrong code for them. This could either be fixed by emitting correct code, or it could be handled by rejecting all initializers for non-static objects with such members. Both approaches seem risky to me at this stage and so I think it's safest to hold off on implementing either until after the release. Martin
PR c++/79986 - [CHKP] ICE in fold_convert_loc with a flexible array gcc/ChangeLog: PR c++/79986 * tree-chkp.c (chkp_process_stmt): Avoid assuming size is non-null. gcc/testsuite/ChangeLog: PR c++/79986 * g++.dg/pr79986.C: New test. diff --git a/gcc/testsuite/g++.dg/pr79986.C b/gcc/testsuite/g++.dg/pr79986.C new file mode 100644 index 0000000..d179cf6 --- /dev/null +++ b/gcc/testsuite/g++.dg/pr79986.C @@ -0,0 +1,10 @@ +/* PR c++/79986 - [CHKP] ICE in fold_convert_loc with a flexible array + { dg-do compile { target { { i?86-*-* x86_64-*-* } && { ! x32 } } } } + { dg-options "-fcheck-pointer-bounds -mmpx" } */ + +int f (int i) +{ + struct A { int n; char a[]; }; + + return ((struct A){ 1, "" }).a[i]; // { dg-error "invalid use of array" } +} diff --git a/gcc/tree-chkp.c b/gcc/tree-chkp.c index b1ff218..780d18f 100644 --- a/gcc/tree-chkp.c +++ b/gcc/tree-chkp.c @@ -4092,6 +4092,10 @@ chkp_process_stmt (gimple_stmt_iterator *iter, tree node, expression to compute it. */ if (!addr_last) { + /* C++ flexible array members have a null size. */ + if (!size) + return; + addr_last = fold_build_pointer_plus_loc (loc, addr_first, size); addr_last = fold_build_pointer_plus_hwi_loc (loc, addr_last, -1); }