On 11/14/19 10:38 AM, Martin Sebor wrote:
> GCC 9 added checks for usafe uses of unterminated constant char
> arrays to a few string functions but the checking is far from
> comprehensive.  It's been on my list of things to do to do
> a more thorough review and add the checks where they're missing.
> 
> The attached patch does this for the majority of common built-ins.
> There still are a few where it could be added but this should cover
> most of the commonly used ones where the misuses are likely to come
> up.
> 
> This patch depends on the one I posted earlier today for PR 92501:
>   https://gcc.gnu.org/ml/gcc-patches/2019-11/msg01233.html
> 
> I tested both patches together on x86_64-linux.
> 
> Martin
> 
> PS I considered introducing a new attribute, say string, first
> to reduce the extent of the changes in GCC, and second to provide
> a mechanism to let GCC check even user-defined functions for these
> bugs.  I stopped short of doing this because most of the changes
> to the built-ins are necessary either way, and also because it
> seems late in the cycle to introduce such an extension.  Unless
> there's a strong preference for adding it now I will revisit
> the decision for GCC 11.
> 
> gcc-88226.diff
> 
> 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.
OK
jeff

Reply via email to