https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84468
--- Comment #8 from Romain Geissler <romain.geissler at amadeus dot com> --- I am currently testing a little variant of your patch (check that "nextbb" if not NULL before trying to use it): Index: gcc/tree-ssa-strlen.c =================================================================== --- gcc/tree-ssa-strlen.c (revision 257796) +++ gcc/tree-ssa-strlen.c (working copy) @@ -1851,8 +1851,21 @@ maybe_diag_stxncpy_trunc (gimple_stmt_iterator gsi avoid the truncation warning. */ gsi_next_nondebug (&gsi); gimple *next_stmt = gsi_stmt (gsi); + if (!next_stmt) + { + /* When there is no statement in the same basic block check + the immediate successor block. */ + basic_block bb = gimple_bb (stmt); + basic_block nextbb + = EDGE_COUNT (bb->succs) ? EDGE_SUCC (bb, 0)->dest : NULL; + if (nextbb) + { + gimple_stmt_iterator it = gsi_start_bb (nextbb); + next_stmt = gsi_stmt (it); + } + } - if (!gsi_end_p (gsi) && is_gimple_assign (next_stmt)) + if (next_stmt && is_gimple_assign (next_stmt)) { tree lhs = gimple_assign_lhs (next_stmt); tree_code code = TREE_CODE (lhs); If it doesn't work, I will provide you with the translation unit.