Commit 01b0acb7 by Martin Sebor Committed by Jeff Law

builtins.c (unterminated_array): Handle ARRAY_REF.

	* builtins.c (unterminated_array): Handle ARRAY_REF.
	(expand_builtin_stpcpy_1): Detect unterminated char arrays.
	* builtins.h (unterminated_array): Declare extern.
	* gimple-fold.c (gimple_fold_builtin_stpcpy): Detect unterminated
	  arrays.
	(gimple_fold_builtin_sprintf): Propagate NO_WARNING to transformed
	calls.

	* gcc.dg/warn-stpcpy-no-nul.c: New test.

From-SVN: r264328
parent e08341bb
2018-09-14 Martin Sebor <msebor@redhat.com> 2018-09-14 Martin Sebor <msebor@redhat.com>
* builtins.c (unterminated_array): Handle ARRAY_REF.
(expand_builtin_stpcpy_1): Detect unterminated char arrays.
* builtins.h (unterminated_array): Declare extern.
* gimple-fold.c (gimple_fold_builtin_stpcpy): Detect unterminated
arrays.
(gimple_fold_builtin_sprintf): Propagate NO_WARNING to transformed
calls.
2018-09-14 Martin Sebor <msebor@redhat.com>
Jeff Law <law@redhat.com> Jeff Law <law@redhat.com>
* builtins.c (unterminated_array): New. * builtins.c (unterminated_array): New.
......
...@@ -567,7 +567,7 @@ warn_string_no_nul (location_t loc, const char *fn, tree arg, tree decl) ...@@ -567,7 +567,7 @@ warn_string_no_nul (location_t loc, const char *fn, tree arg, tree decl)
the declaration of the object of which the array is a member or the declaration of the object of which the array is a member or
element. Otherwise return null. */ element. Otherwise return null. */
static tree tree
unterminated_array (tree exp) unterminated_array (tree exp)
{ {
if (TREE_CODE (exp) == SSA_NAME) if (TREE_CODE (exp) == SSA_NAME)
...@@ -578,7 +578,10 @@ unterminated_array (tree exp) ...@@ -578,7 +578,10 @@ unterminated_array (tree exp)
tree rhs1 = gimple_assign_rhs1 (stmt); tree rhs1 = gimple_assign_rhs1 (stmt);
tree_code code = gimple_assign_rhs_code (stmt); tree_code code = gimple_assign_rhs_code (stmt);
if (code != POINTER_PLUS_EXPR) if (code == ADDR_EXPR
&& TREE_CODE (TREE_OPERAND (rhs1, 0)) == ARRAY_REF)
rhs1 = rhs1;
else if (code != POINTER_PLUS_EXPR)
return NULL_TREE; return NULL_TREE;
exp = rhs1; exp = rhs1;
...@@ -3981,9 +3984,14 @@ expand_builtin_stpcpy_1 (tree exp, rtx target, machine_mode mode) ...@@ -3981,9 +3984,14 @@ expand_builtin_stpcpy_1 (tree exp, rtx target, machine_mode mode)
compile-time, not an expression containing a string. This is compile-time, not an expression containing a string. This is
because the latter will potentially produce pessimized code because the latter will potentially produce pessimized code
when used to produce the return value. */ when used to produce the return value. */
if (! c_getstr (src) || ! (len = c_strlen (src, 0))) tree nonstr = NULL_TREE;
if (!c_getstr (src, NULL)
|| !(len = c_strlen (src, 0, &nonstr, 1)))
return expand_movstr (dst, src, target, /*endp=*/2); return expand_movstr (dst, src, target, /*endp=*/2);
if (nonstr && !TREE_NO_WARNING (exp))
warn_string_no_nul (EXPR_LOCATION (exp), "stpcpy", src, nonstr);
lenp1 = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1)); lenp1 = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
ret = expand_builtin_mempcpy_args (dst, src, lenp1, ret = expand_builtin_mempcpy_args (dst, src, lenp1,
target, exp, /*endp=*/2); target, exp, /*endp=*/2);
......
...@@ -104,6 +104,7 @@ extern internal_fn associated_internal_fn (tree); ...@@ -104,6 +104,7 @@ extern internal_fn associated_internal_fn (tree);
extern internal_fn replacement_internal_fn (gcall *); extern internal_fn replacement_internal_fn (gcall *);
extern void warn_string_no_nul (location_t, const char *, tree, tree); extern void warn_string_no_nul (location_t, const char *, tree, tree);
extern tree unterminated_array (tree);
extern tree max_object_size (); extern tree max_object_size ();
#endif /* GCC_BUILTINS_H */ #endif /* GCC_BUILTINS_H */
...@@ -2798,7 +2798,7 @@ gimple_fold_builtin_stpcpy (gimple_stmt_iterator *gsi) ...@@ -2798,7 +2798,7 @@ gimple_fold_builtin_stpcpy (gimple_stmt_iterator *gsi)
location_t loc = gimple_location (stmt); location_t loc = gimple_location (stmt);
tree dest = gimple_call_arg (stmt, 0); tree dest = gimple_call_arg (stmt, 0);
tree src = gimple_call_arg (stmt, 1); tree src = gimple_call_arg (stmt, 1);
tree fn, len, lenp1; tree fn, lenp1;
/* If the result is unused, replace stpcpy with strcpy. */ /* If the result is unused, replace stpcpy with strcpy. */
if (gimple_call_lhs (stmt) == NULL_TREE) if (gimple_call_lhs (stmt) == NULL_TREE)
...@@ -2811,10 +2811,25 @@ gimple_fold_builtin_stpcpy (gimple_stmt_iterator *gsi) ...@@ -2811,10 +2811,25 @@ gimple_fold_builtin_stpcpy (gimple_stmt_iterator *gsi)
return true; return true;
} }
len = c_strlen (src, 1); /* Set to non-null if ARG refers to an unterminated array. */
tree nonstr = NULL;
tree len = c_strlen (src, 1, &nonstr, 1);
if (!len if (!len
|| TREE_CODE (len) != INTEGER_CST) || TREE_CODE (len) != INTEGER_CST)
return false; {
nonstr = unterminated_array (src);
if (!nonstr)
return false;
}
if (nonstr)
{
/* Avoid folding calls with unterminated arrays. */
if (!gimple_no_warning_p (stmt))
warn_string_no_nul (loc, "stpcpy", src, nonstr);
gimple_set_no_warning (stmt, true);
return false;
}
if (optimize_function_for_size_p (cfun) if (optimize_function_for_size_p (cfun)
/* If length is zero it's small enough. */ /* If length is zero it's small enough. */
...@@ -3077,6 +3092,12 @@ gimple_fold_builtin_sprintf (gimple_stmt_iterator *gsi) ...@@ -3077,6 +3092,12 @@ gimple_fold_builtin_sprintf (gimple_stmt_iterator *gsi)
'format' is known to contain no % formats. */ 'format' is known to contain no % formats. */
gimple_seq stmts = NULL; gimple_seq stmts = NULL;
gimple *repl = gimple_build_call (fn, 2, dest, fmt); gimple *repl = gimple_build_call (fn, 2, dest, fmt);
/* Propagate the NO_WARNING bit to avoid issuing the same
warning more than once. */
if (gimple_no_warning_p (stmt))
gimple_set_no_warning (repl, true);
gimple_seq_add_stmt_without_update (&stmts, repl); gimple_seq_add_stmt_without_update (&stmts, repl);
if (gimple_call_lhs (stmt)) if (gimple_call_lhs (stmt))
{ {
...@@ -3125,6 +3146,12 @@ gimple_fold_builtin_sprintf (gimple_stmt_iterator *gsi) ...@@ -3125,6 +3146,12 @@ gimple_fold_builtin_sprintf (gimple_stmt_iterator *gsi)
/* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */ /* Convert sprintf (str1, "%s", str2) into strcpy (str1, str2). */
gimple_seq stmts = NULL; gimple_seq stmts = NULL;
gimple *repl = gimple_build_call (fn, 2, dest, orig); gimple *repl = gimple_build_call (fn, 2, dest, orig);
/* Propagate the NO_WARNING bit to avoid issuing the same
warning more than once. */
if (gimple_no_warning_p (stmt))
gimple_set_no_warning (repl, true);
gimple_seq_add_stmt_without_update (&stmts, repl); gimple_seq_add_stmt_without_update (&stmts, repl);
if (gimple_call_lhs (stmt)) if (gimple_call_lhs (stmt))
{ {
......
2018-09-14 Martin Sebor <msebor@redhat.com> 2018-09-14 Martin Sebor <msebor@redhat.com>
* gcc.dg/warn-stpcpy-no-nul.c: New test.
2018-09-14 Martin Sebor <msebor@redhat.com>
Jeff Law <law@redhat.com> Jeff Law <law@redhat.com>
* gcc.dg/warn-strcpy-no-nul.c: New test. * gcc.dg/warn-strcpy-no-nul.c: New test.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment