diff options
author | Martin Sebor <msebor@redhat.com> | 2019-11-22 16:47:22 +0000 |
---|---|---|
committer | Martin Sebor <msebor@gcc.gnu.org> | 2019-11-22 09:47:22 -0700 |
commit | b5338fb359ea3480d6ed37bbc52fe2df49b82fb9 (patch) | |
tree | 2216f90a2e8eee2cdeb3d613c5d53dc9e791f4a9 /gcc/tree-ssa-strlen.c | |
parent | 86b0eb81837f4912d3f10bcfad35086cc54a1841 (diff) | |
download | gcc-b5338fb359ea3480d6ed37bbc52fe2df49b82fb9.zip gcc-b5338fb359ea3480d6ed37bbc52fe2df49b82fb9.tar.gz gcc-b5338fb359ea3480d6ed37bbc52fe2df49b82fb9.tar.bz2 |
PR middle-end/88226 - missing warning on fprintf, fputs, and puts with an unterminated array
gcc/ChangeLog:
PR middle-end/88226
* builtins.c (check_nul_terminated_array): New function.
(fold_builtin_0): Remove declaration.
(fold_builtin_1): Same.
(fold_builtin_2): Same.
(fold_builtin_3): Same.
(fold_builtin_strpbrk): Add argument.
(fold_builtin_strspn): Same.
(fold_builtin_strcspn): Same.
(expand_builtin_strcat): Call it. Remove unused argument.
(expand_builtin_stpncpy): Same.
(expand_builtin_strncat): Same.
(expand_builtin_strncpy): Same. Adjust indentation.
(expand_builtin_strcmp): Same.
(expand_builtin_strncmp): Same.
(expand_builtin_fork_or_exec): Same.
(expand_builtin): Handle more built-ins.
(fold_builtin_2): Add argument.
(fold_builtin_n): Make static. Add argument.
(fold_call_expr): Pass new argument to fold_builtin_n and fold_builtin_2.
(fold_builtin_call_array): Pass new argument to fold_builtin_n.
(fold_builtin_strpbrk): Add argument. Call check_nul_terminated_array.
(fold_call_stmt): Pass new argument to fold_builtin_n.
* builtins.h: Correct a comment.
* gimple-fold.c (gimple_fold_builtin_strchr): Call
check_nul_terminated_array.
* tree-ssa-strlen.c (handle_builtin_strlen): Call
check_nul_terminated_array.
(handle_builtin_strchr): Same.
(handle_builtin_string_cmp): Same.
gcc/testsuite/ChangeLog:
PR middle-end/88226
* gcc.dg/Wstringop-overflow-22.c: New test.
* gcc.dg/tree-ssa/builtin-fprintf-warn-1.c: Remove xfails.
From-SVN: r278623
Diffstat (limited to 'gcc/tree-ssa-strlen.c')
-rw-r--r-- | gcc/tree-ssa-strlen.c | 22 |
1 files changed, 16 insertions, 6 deletions
diff --git a/gcc/tree-ssa-strlen.c b/gcc/tree-ssa-strlen.c index be0f37c..06fa99e 100644 --- a/gcc/tree-ssa-strlen.c +++ b/gcc/tree-ssa-strlen.c @@ -1946,8 +1946,6 @@ handle_builtin_strlen (gimple_stmt_iterator *gsi) static void handle_builtin_strchr (gimple_stmt_iterator *gsi) { - int idx; - tree src; gimple *stmt = gsi_stmt (*gsi); tree lhs = gimple_call_lhs (stmt); @@ -1957,8 +1955,14 @@ handle_builtin_strchr (gimple_stmt_iterator *gsi) if (!integer_zerop (gimple_call_arg (stmt, 1))) return; - src = gimple_call_arg (stmt, 0); - idx = get_stridx (src); + tree src = gimple_call_arg (stmt, 0); + + /* Avoid folding if the first argument is not a nul-terminated array. + Defer warning until later. */ + if (!check_nul_terminated_array (NULL_TREE, src)) + return; + + int idx = get_stridx (src); if (idx) { strinfo *si = NULL; @@ -3794,11 +3798,11 @@ handle_builtin_string_cmp (gimple_stmt_iterator *gsi) /* For strncmp set to the the value of the third argument if known. */ HOST_WIDE_INT bound = -1; - + tree len = NULL_TREE; /* Extract the strncmp bound. */ if (gimple_call_num_args (stmt) == 3) { - tree len = gimple_call_arg (stmt, 2); + len = gimple_call_arg (stmt, 2); if (tree_fits_shwi_p (len)) bound = tree_to_shwi (len); @@ -3807,6 +3811,12 @@ handle_builtin_string_cmp (gimple_stmt_iterator *gsi) return false; } + /* Avoid folding if either argument is not a nul-terminated array. + Defer warning until later. */ + if (!check_nul_terminated_array (NULL_TREE, arg1, len) + || !check_nul_terminated_array (NULL_TREE, arg2, len)) + return false; + { /* Set to the length of one argument (or its complement if it's the lower bound of a range) and the size of the array storing |