From 31dd5cd6344bfbbe122fb512993b128e11236d35 Mon Sep 17 00:00:00 2001 From: Marek Polacek Date: Wed, 16 Sep 2020 09:27:29 -0400 Subject: [PATCH] preprocessor: Fix ICE with too long line in fmtwarn [PR96935] Here we ICE in char_span::subspan because the offset it gets is -1. It's -1 because get_substring_ranges_for_loc gets a location whose column was 0. That only happens in testcases like the attached where we're dealing with extremely long lines (at least 4065 chars it seems). This does happen in practice, though, so it's not just a theoretical problem (e.g. when building the SU2 suite). Fixed by checking that the column get_substring_ranges_for_loc gets is sane, akin to other checks in that function. gcc/ChangeLog: PR preprocessor/96935 * input.c (get_substring_ranges_for_loc): Return if start.column is less than 1. gcc/testsuite/ChangeLog: PR preprocessor/96935 * gcc.dg/format/pr96935.c: New test. --- gcc/input.c | 2 ++ gcc/testsuite/gcc.dg/format/pr96935.c | 9 +++++++++ 2 files changed, 11 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/format/pr96935.c diff --git a/gcc/input.c b/gcc/input.c index d573b90341a..29d10f06b86 100644 --- a/gcc/input.c +++ b/gcc/input.c @@ -1461,6 +1461,8 @@ get_substring_ranges_for_loc (cpp_reader *pfile, size_t literal_length = finish.column - start.column + 1; /* Ensure that we don't crash if we got the wrong location. */ + if (start.column < 1) + return "zero start column"; if (line.length () < (start.column - 1 + literal_length)) return "line is not wide enough"; diff --git a/gcc/testsuite/gcc.dg/format/pr96935.c b/gcc/testsuite/gcc.dg/format/pr96935.c new file mode 100644 index 00000000000..c31109704be --- /dev/null +++ b/gcc/testsuite/gcc.dg/format/pr96935.c @@ -0,0 +1,9 @@ +/* PR preprocessor/96935 */ +/* { dg-do compile } */ +/* { dg-options "-O" } */ + +void +foo (char const *name) +{ + /* xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx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