Originally reported by cpu in https://bugs.gentoo.org/844091. Here is my attempt at minimizing it: //$ cat main.c struct { long users; long size; char *data; } * main_trans; void *main___trans_tmp_1; int sprintf(char *, char *, ...); int main() { int users = 0; struct { long users; long size; char *data; int links[users]; char buf[]; } *trans = trans; trans->data = trans->buf; main___trans_tmp_1 = trans; main_trans = main___trans_tmp_1; sprintf(main_trans->data, "test"); } $ /tmp/gb/gcc/xgcc -B/tmp/gb/gcc -c -Wall -O2 -pipe -fomit-frame-pointer main.c during GIMPLE pass: strlen main.c: In function ‘main’: main.c:8:5: internal compiler error: in tree_to_shwi, at tree.cc:6369 8 | int main() { | ^~~~ 0x1116ade tree_to_shwi(tree_node const*) gcc/tree.cc:6369 0x1116b36 int_byte_position(tree_node const*) gcc/tree.cc:3616 0x1cd1ce8 get_origin_and_offset_r gcc/gimple-ssa-sprintf.cc:2354 0x1cd1845 get_origin_and_offset_r gcc/gimple-ssa-sprintf.cc:2307 0x1cd1f29 get_origin_and_offset_r gcc/gimple-ssa-sprintf.cc:2370 0x1cd64ce get_origin_and_offset gcc/gimple-ssa-sprintf.cc:2427 0x1cd64ce handle_printf_call(gimple_stmt_iterator*, pointer_query&) gcc/gimple-ssa-sprintf.cc:4703 0x103ff10 strlen_pass::check_and_optimize_call(bool*) gcc/tree-ssa-strlen.cc:5461 0x103ffb3 strlen_pass::check_and_optimize_stmt(bool*) gcc/tree-ssa-strlen.cc:5665 0x1040326 strlen_pass::before_dom_children(basic_block_def*) gcc/tree-ssa-strlen.cc:5849 0x1c5d56b dom_walker::walk(basic_block_def*) gcc/domwalk.cc:309 0x10409cf printf_strlen_execute gcc/tree-ssa-strlen.cc:5908 0x1040c58 execute gcc/tree-ssa-strlen.cc:6007 I seem to be able to reproduce ICE on: 10.3.0, 11.3.0, 12.1.0. This does not ICE: 9.3.0 $ /tmp/gb/gcc/xgcc -B/tmp/gb/gcc -v Reading specs from /tmp/gb/gcc/specs COLLECT_GCC=/tmp/gb/gcc/xgcc COLLECT_LTO_WRAPPER=/tmp/gb/gcc/lto-wrapper Target: x86_64-pc-linux-gnu Configured with: /home/slyfox/dev/git/gcc/configure --disable-multilib --disable-bootstrap --with-native-system-header-dir=/<<NIX>>/glibc-2.34-115-dev/include --prefix=/tmp/gb/__td__ CFLAGS='-O1 -ggdb3' CXXFLAGS='-O1 -ggdb3' LDFLAGS='-O1 -ggdb3' Thread model: posix Supported LTO compression algorithms: zlib gcc version 13.0.0 20220514 (experimental) (GCC)
Feels like someone forgot to check if the type had a non constant size/offsets.
Started with r10-4571-g937a86b49e4ed8ef.
Not only that.
The master branch has been updated by Martin Sebor <msebor@gcc.gnu.org>: https://gcc.gnu.org/g:10d1986aee47c592f903527bb68546efc557735d commit r13-750-g10d1986aee47c592f903527bb68546efc557735d Author: Martin Sebor <msebor@redhat.com> Date: Tue May 24 16:01:12 2022 -0600 PR middle-end/105604 - ICE: in tree_to_shwi with vla in struct and sprintf gcc/ChangeLog: PR middle-end/105604 * gimple-ssa-sprintf.cc (set_aggregate_size_and_offset): Add comments. (get_origin_and_offset_r): Remove null handling. Handle variable array sizes. (get_origin_and_offset): Handle null argument here. Simplify. (alias_offset): Update comment. * pointer-query.cc (field_at_offset): Update comment. Handle members of variable-length types. gcc/testsuite/ChangeLog: PR middle-end/105604 * gcc.dg/Wrestrict-24.c: New test. * gcc.dg/Wrestrict-25.c: New test. * gcc.dg/Wrestrict-26.c: New test. Co-authored-by: Richard Biener <rguenther@suse.de>
Fixed on trunk. The changes should be safe to backport after some time.
The releases/gcc-12 branch has been updated by Richard Biener <rguenth@gcc.gnu.org>: https://gcc.gnu.org/g:740771a620f661800b99dfc039b8896e2ddb9265 commit r12-8450-g740771a620f661800b99dfc039b8896e2ddb9265 Author: Martin Sebor <msebor@redhat.com> Date: Tue May 24 16:01:12 2022 -0600 PR middle-end/105604 - ICE: in tree_to_shwi with vla in struct and sprintf gcc/ChangeLog: PR middle-end/105604 * gimple-ssa-sprintf.cc (set_aggregate_size_and_offset): Add comments. (get_origin_and_offset_r): Remove null handling. Handle variable array sizes. (get_origin_and_offset): Handle null argument here. Simplify. (alias_offset): Update comment. * pointer-query.cc (field_at_offset): Update comment. Handle members of variable-length types. gcc/testsuite/ChangeLog: PR middle-end/105604 * gcc.dg/Wrestrict-24.c: New test. * gcc.dg/Wrestrict-25.c: New test. * gcc.dg/Wrestrict-26.c: New test. Co-authored-by: Richard Biener <rguenther@suse.de> (cherry picked from commit 10d1986aee47c592f903527bb68546efc557735d)
Originally (gentoo) reported against 11.2, but the fix doesn't cherry-pick nicely.
GCC 10.4 is being released, retargeting bugs to GCC 10.5.
GCC 10 branch is being closed.