diff options
author | Martin Sebor <msebor@redhat.com> | 2018-09-14 18:23:58 +0000 |
---|---|---|
committer | Jeff Law <law@gcc.gnu.org> | 2018-09-14 12:23:58 -0600 |
commit | e08341bb0e026ef019a3b201ea26ecdc4a9233ea (patch) | |
tree | 85a654a129f0c74bd85567d343cb3ba067c3c9c4 /gcc/builtins.c | |
parent | dd98382b8b016c9af092f77b47dfb7eaf35db426 (diff) | |
download | gcc-e08341bb0e026ef019a3b201ea26ecdc4a9233ea.zip gcc-e08341bb0e026ef019a3b201ea26ecdc4a9233ea.tar.gz gcc-e08341bb0e026ef019a3b201ea26ecdc4a9233ea.tar.bz2 |
builtins.c (unterminated_array): New.
* builtins.c (unterminated_array): New.
(expand_builtin_strcpy): Adjust.
(expand_builtin_strcpy_args): Detect unterminated arrays.
* gimple-fold.c (get_maxval_strlen): Add argument. Detect
unterminated arrays.
* gimple-fold.h (get_maxval_strlen): Add argument.
(gimple_fold_builtin_strcpy): Detec unterminated arrays.
* gimple-fold.c (get_range_strlen): Add argument.
(get_maxval_strlen): Adjust.
* gimple-fold.h (get_range_strlen): Add argument.
* gcc.dg/warn-strcpy-no-nul.c: New test.
Co-Authored-By: Jeff Law <law@redhat.com>
From-SVN: r264327
Diffstat (limited to 'gcc/builtins.c')
-rw-r--r-- | gcc/builtins.c | 45 |
1 files changed, 41 insertions, 4 deletions
diff --git a/gcc/builtins.c b/gcc/builtins.c index a345704..be813db 100644 --- a/gcc/builtins.c +++ b/gcc/builtins.c @@ -132,7 +132,7 @@ static rtx expand_builtin_mempcpy (tree, rtx); static rtx expand_builtin_mempcpy_args (tree, tree, tree, rtx, tree, int); static rtx expand_builtin_strcat (tree, rtx); static rtx expand_builtin_strcpy (tree, rtx); -static rtx expand_builtin_strcpy_args (tree, tree, rtx); +static rtx expand_builtin_strcpy_args (tree, tree, tree, rtx); static rtx expand_builtin_stpcpy (tree, rtx, machine_mode); static rtx expand_builtin_stpncpy (tree, rtx); static rtx expand_builtin_strncat (tree, rtx); @@ -563,6 +563,34 @@ warn_string_no_nul (location_t loc, const char *fn, tree arg, tree decl) } } +/* If EXP refers to an unterminated constant character array return + the declaration of the object of which the array is a member or + element. Otherwise return null. */ + +static tree +unterminated_array (tree exp) +{ + if (TREE_CODE (exp) == SSA_NAME) + { + gimple *stmt = SSA_NAME_DEF_STMT (exp); + if (!is_gimple_assign (stmt)) + return NULL_TREE; + + tree rhs1 = gimple_assign_rhs1 (stmt); + tree_code code = gimple_assign_rhs_code (stmt); + if (code != POINTER_PLUS_EXPR) + return NULL_TREE; + + exp = rhs1; + } + + tree nonstr = NULL; + if (c_strlen (exp, 1, &nonstr, 1) == NULL && nonstr) + return nonstr; + + return NULL_TREE; +} + /* Compute the length of a null-terminated character string or wide character string handling character sizes of 1, 2, and 4 bytes. TREE_STRING_LENGTH is not the right way because it evaluates to @@ -3879,7 +3907,7 @@ expand_builtin_strcpy (tree exp, rtx target) src, destsize); } - if (rtx ret = expand_builtin_strcpy_args (dest, src, target)) + if (rtx ret = expand_builtin_strcpy_args (exp, dest, src, target)) { /* Check to see if the argument was declared attribute nonstring and if so, issue a warning since at this point it's not known @@ -3899,8 +3927,17 @@ expand_builtin_strcpy (tree exp, rtx target) expand_builtin_strcpy. */ static rtx -expand_builtin_strcpy_args (tree dest, tree src, rtx target) +expand_builtin_strcpy_args (tree exp, tree dest, tree src, rtx target) { + /* Detect strcpy calls with unterminated arrays.. */ + if (tree nonstr = unterminated_array (src)) + { + /* NONSTR refers to the non-nul terminated constant array. */ + if (!TREE_NO_WARNING (exp)) + warn_string_no_nul (EXPR_LOCATION (exp), "strcpy", src, nonstr); + return NULL_RTX; + } + return expand_movstr (dest, src, target, /*endp=*/0); } @@ -3960,7 +3997,7 @@ expand_builtin_stpcpy_1 (tree exp, rtx target, machine_mode mode) if (CONST_INT_P (len_rtx)) { - ret = expand_builtin_strcpy_args (dst, src, target); + ret = expand_builtin_strcpy_args (exp, dst, src, target); if (ret) { |