+2018-08-28 Jakub Jelinek <jakub@redhat.com>
+
+ 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 <richard.sandiford@arm.com>
* tree-ssa-sccvn.c (fully_constant_vn_reference_p): Fix unguarded
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);
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))
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;
if (!get_attr_nonstring_decl (arg))
get_range_strlen (arg, lenrng);
- if (1 < nargs)
+ if (nargs > 1)
bound = CALL_EXPR_ARG (exp, 1);
break;
}
}
}
- 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);
--- /dev/null
+/* 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);
+}