From 2c8861b7505cdc04377cd125565bb5ccadb29529 Mon Sep 17 00:00:00 2001 From: Jakub Jelinek Date: Tue, 28 Aug 2018 13:13:04 +0200 Subject: [PATCH] re PR middle-end/87099 (internal compiler error: segmentation fault) PR middle-end/87099 * calls.c (maybe_warn_nonstring_arg): Punt early if warn_stringop_overflow is zero. Don't call get_range_strlen on 3rd argument, keep iterating until lenrng[1] is INTEGER_CST. Swap comparison operands to have constants on rhs. Only use lenrng[1] if non-NULL and INTEGER_CST. Don't uselessly increment lenrng[0]. * gcc.dg/pr87099.c: New test. From-SVN: r263915 --- gcc/ChangeLog | 10 ++++++++++ gcc/calls.c | 16 ++++++++-------- gcc/testsuite/ChangeLog | 5 +++++ gcc/testsuite/gcc.dg/pr87099.c | 21 +++++++++++++++++++++ 4 files changed, 44 insertions(+), 8 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/pr87099.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 0e2f9d013c3..66396a0a4d3 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,13 @@ +2018-08-28 Jakub Jelinek + + PR middle-end/87099 + * calls.c (maybe_warn_nonstring_arg): Punt early if + warn_stringop_overflow is zero. Don't call get_range_strlen + on 3rd argument, keep iterating until lenrng[1] is INTEGER_CST. + Swap comparison operands to have constants on rhs. Only use + lenrng[1] if non-NULL and INTEGER_CST. Don't uselessly + increment lenrng[0]. + 2018-08-28 Richard Sandiford * tree-ssa-sccvn.c (fully_constant_vn_reference_p): Fix unguarded diff --git a/gcc/calls.c b/gcc/calls.c index f57ecf91636..87e3f000eff 100644 --- a/gcc/calls.c +++ b/gcc/calls.c @@ -1545,7 +1545,7 @@ maybe_warn_nonstring_arg (tree fndecl, tree exp) if (!fndecl || !fndecl_built_in_p (fndecl, BUILT_IN_NORMAL)) return; - if (TREE_NO_WARNING (exp)) + if (TREE_NO_WARNING (exp) || !warn_stringop_overflow) return; unsigned nargs = call_expr_nargs (exp); @@ -1573,7 +1573,9 @@ maybe_warn_nonstring_arg (tree fndecl, tree exp) the range of their known or possible lengths and use it conservatively as the bound for the unbounded function, and to adjust the range of the bound of the bounded ones. */ - for (unsigned argno = 0; argno < nargs && !*lenrng; argno ++) + for (unsigned argno = 0; + argno < MIN (nargs, 2) + && !(lenrng[1] && TREE_CODE (lenrng[1]) == INTEGER_CST); argno++) { tree arg = CALL_EXPR_ARG (exp, argno); if (!get_attr_nonstring_decl (arg)) @@ -1585,12 +1587,12 @@ maybe_warn_nonstring_arg (tree fndecl, tree exp) case BUILT_IN_STRNCAT: case BUILT_IN_STPNCPY: case BUILT_IN_STRNCPY: - if (2 < nargs) + if (nargs > 2) bound = CALL_EXPR_ARG (exp, 2); break; case BUILT_IN_STRNDUP: - if (1 < nargs) + if (nargs > 1) bound = CALL_EXPR_ARG (exp, 1); break; @@ -1600,7 +1602,7 @@ maybe_warn_nonstring_arg (tree fndecl, tree exp) if (!get_attr_nonstring_decl (arg)) get_range_strlen (arg, lenrng); - if (1 < nargs) + if (nargs > 1) bound = CALL_EXPR_ARG (exp, 1); break; } @@ -1640,11 +1642,9 @@ maybe_warn_nonstring_arg (tree fndecl, tree exp) } } - if (*lenrng) + if (lenrng[1] && TREE_CODE (lenrng[1]) == INTEGER_CST) { /* Add one for the nul. */ - lenrng[0] = const_binop (PLUS_EXPR, TREE_TYPE (lenrng[0]), - lenrng[0], size_one_node); lenrng[1] = const_binop (PLUS_EXPR, TREE_TYPE (lenrng[1]), lenrng[1], size_one_node); diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 5dea5f7e994..c62104902f5 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2018-08-28 Jakub Jelinek + + PR middle-end/87099 + * gcc.dg/pr87099.c: New test. + 2018-08-28 Richard Sandiford PR testsuite/87078 diff --git a/gcc/testsuite/gcc.dg/pr87099.c b/gcc/testsuite/gcc.dg/pr87099.c new file mode 100644 index 00000000000..599d721da31 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr87099.c @@ -0,0 +1,21 @@ +/* PR middle-end/87099 */ +/* { dg-do compile } */ +/* { dg-options "-Wstringop-overflow" } */ + +void bar (char *); + +int +foo (int n) +{ + char v[n]; + bar (v); + return __builtin_strncmp (&v[1], "aaa", 3); +} + +int +baz (int n, char *s) +{ + char v[n]; + bar (v); + return __builtin_strncmp (&v[1], s, 3); +} -- 2.30.2