From: Martin Sebor Date: Wed, 23 Nov 2016 16:44:16 +0000 (+0000) Subject: PR middle-end/78461 - [7 Regression] ICE: in operator+= X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=aecc521b46ac451c0547d8693bfcfabfb3832401;p=gcc.git PR middle-end/78461 - [7 Regression] ICE: in operator+= gcc/testsuite/ChangeLog: PR middle-end/78461 * gcc.dg/tree-ssa/builtin-sprintf-4.c: New test. * gcc.dg/tree-ssa/builtin-sprintf-warn-2.c: Adjust warning text. gcc/ChangeLog: PR middle-end/78461 * gimple-ssa-sprintf.c (format_string): Correct the maxima and set the minimum number of bytes for an unknown string to zero. From-SVN: r242769 --- diff --git a/gcc/ChangeLog b/gcc/ChangeLog index f082b0aa6cb..98678e2f306 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,9 @@ +2016-11-23 Martin Sebor + + PR middle-end/78461 + * gimple-ssa-sprintf.c (format_string): Correct the maxima and + set the minimum number of bytes for an unknown string to zero. + 2016-11-23 Martin Jambor Martin Liska diff --git a/gcc/gimple-ssa-sprintf.c b/gcc/gimple-ssa-sprintf.c index 3138ad3624f..ead8b0ed5a0 100644 --- a/gcc/gimple-ssa-sprintf.c +++ b/gcc/gimple-ssa-sprintf.c @@ -1533,18 +1533,15 @@ format_string (const conversion_spec &spec, tree arg) fmtresult res; /* The maximum number of bytes for an unknown wide character argument - to a "%lc" directive adjusted for precision but not field width. */ + to a "%lc" directive adjusted for precision but not field width. + 6 is the longest UTF-8 sequence for a single wide character. */ const unsigned HOST_WIDE_INT max_bytes_for_unknown_wc - = (1 == warn_format_length ? 0 <= prec ? prec : 0 - : 2 == warn_format_length ? 0 <= prec ? prec : 1 - : 0 <= prec ? prec : 6 /* Longest UTF-8 sequence. */); + = (0 <= prec ? prec : 1 < warn_format_length ? 6 : 1); /* The maximum number of bytes for an unknown string argument to either a "%s" or "%ls" directive adjusted for precision but not field width. */ const unsigned HOST_WIDE_INT max_bytes_for_unknown_str - = (1 == warn_format_length ? 0 <= prec ? prec : 0 - : 2 == warn_format_length ? 0 <= prec ? prec : 1 - : HOST_WIDE_INT_MAX); + = (0 <= prec ? prec : 1 < warn_format_length); /* The result is bounded unless overriddden for a non-constant string of an unknown length. */ @@ -1648,7 +1645,7 @@ format_string (const conversion_spec &spec, tree arg) if (0 <= prec) { if (slen.range.min >= target_int_max ()) - slen.range.min = max_bytes_for_unknown_str; + slen.range.min = 0; else if ((unsigned)prec < slen.range.min) slen.range.min = prec; diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 6abbd3b4378..d5055da891c 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,9 @@ +2016-11-23 Martin Sebor + + PR middle-end/78461 + * gcc.dg/tree-ssa/builtin-sprintf-4.c: New test. + * gcc.dg/tree-ssa/builtin-sprintf-warn-2.c: Adjust warning text. + 2016-11-23 Jakub Jelinek PR c++/71450 diff --git a/gcc/testsuite/gcc.dg/tree-ssa/builtin-sprintf-4.c b/gcc/testsuite/gcc.dg/tree-ssa/builtin-sprintf-4.c new file mode 100644 index 00000000000..42448749fc7 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/builtin-sprintf-4.c @@ -0,0 +1,69 @@ +/* PR middle-end/78461 - [7 Regression] ICE: in operator+=, at + gimple-ssa-sprintf.c:214 + Disable warnings to exercise code paths through the pass that may + not be exercised when the -Wformat-length option is in effect. */ +/* { dg-compile } + { dg-options "-O2 -fdump-tree-optimized -w" } */ + + +#define CAT(s, n) s ## n +#define FAIL(line) CAT (failure_on_line_, line) + +/* Emit a call to a function named failure_on_line_NNN when EXPR is false. */ +#define ASSERT(expr) \ + do { \ + extern void FAIL (__LINE__)(void); \ + if (!(expr)) FAIL (__LINE__)(); \ + } while (0) + +#define KEEP(line) CAT (keep_call_on_line_, line) + +/* Emit a call to a function named keep_call_on_line_NNN when EXPR is true. + Used to verify that the expression need not be the only one that holds. */ +#define ASSERT_MAYBE(expr) \ + do { \ + extern void KEEP (__LINE__)(void); \ + if (expr) KEEP (__LINE__)(); \ + } while (0) + +int f0 (const char *s) +{ + int n = __builtin_snprintf (0, 0, "%.*s%08x", 1, s, 1); + + ASSERT (7 < n && n < 10); + + ASSERT_MAYBE (8 == n); + ASSERT_MAYBE (9 == n); + + return n; +} + +char buf[64]; + +int f1 (const char *s) +{ + int n = __builtin_snprintf (buf, 64, "%.*s%08x", 1, s, 1); + + ASSERT (7 < n && n < 10); + + ASSERT_MAYBE (8 == n); + ASSERT_MAYBE (9 == n); + + return n; +} + +int f2 (const char *s) +{ + int n = __builtin_snprintf (0, 0, "%.*s", 2, s); + + ASSERT (0 <= n && n <= 2); + + ASSERT_MAYBE (0 == n); + ASSERT_MAYBE (1 == n); + ASSERT_MAYBE (2 == n); + + return n; +} + +/* { dg-final { scan-tree-dump-not "failure_on_line" "optimized"} } + { dg-final { scan-tree-dump-times "keep_call_on_line" 7 "optimized"} } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/builtin-sprintf-warn-2.c b/gcc/testsuite/gcc.dg/tree-ssa/builtin-sprintf-warn-2.c index e19768e1b65..3b57c0e076e 100644 --- a/gcc/testsuite/gcc.dg/tree-ssa/builtin-sprintf-warn-2.c +++ b/gcc/testsuite/gcc.dg/tree-ssa/builtin-sprintf-warn-2.c @@ -93,10 +93,10 @@ void test_s_nonconst (const char *s, const wchar_t *ws, struct Arrays *a) T (1, "%s", s); /* { dg-warning "nul past the end" "sprintf transformed into strcpy" { xfail *-*-* } } */ T (1, "%1s", s); /* { dg-warning "nul past the end" } */ T (1, "%.0s", s); - T (1, "%.1s", s); /* { dg-warning "writing a terminating nul" } */ + T (1, "%.1s", s); /* { dg-warning "may write a terminating nul" } */ T (1, "%.0ls", ws); - T (1, "%.1ls", ws); /* { dg-warning "writing a terminating nul" } */ + T (1, "%.1ls", ws); /* { dg-warning "may write a terminating nul" } */ T (1, "%ls", ws); /* { dg-warning "writing a terminating nul" } */ /* Verify that the size of the array is used in lieu of its length.