PR c/81117 - Improve buffer overflow checking in strncpy
authorMartin Sebor <msebor@redhat.com>
Fri, 10 Nov 2017 16:35:26 +0000 (16:35 +0000)
committerMartin Sebor <msebor@gcc.gnu.org>
Fri, 10 Nov 2017 16:35:26 +0000 (09:35 -0700)
commit025d57f037ad13eb479818b677ef4be4d97b639c
tree28c279950fb42d8f67b86b9a2193b3acc6a669f2
parente89ce41dbab07a9acafd900a3ab57eeb5d499276
PR c/81117 - Improve buffer overflow checking in strncpy

gcc/ChangeLog:

PR c/81117
* builtins.c (compute_objsize): Handle arrays that
compute_builtin_object_size likes to fail for.  Make extern.
* builtins.h (compute_objsize): Declare.
(check_strncpy_sizes): New function.
(expand_builtin_strncpy): Call check_strncpy_sizes.
* gimple-fold.c (gimple_fold_builtin_strncpy): Implement
-Wstringop-truncation.
(gimple_fold_builtin_strncat): Same.
* gimple.c (gimple_build_call_from_tree): Set call location.
* tree-ssa-strlen.c (strlen_to_stridx): New global variable.
(maybe_diag_bound_equal_length, is_strlen_related_p): New functions.
(handle_builtin_stxncpy, handle_builtin_strncat): Same.
(handle_builtin_strlen): Use strlen_to_stridx.
(strlen_optimize_stmt): Handle flavors of strncat, strncpy, and
stpncpy.
Use strlen_to_stridx.
(pass_strlen::execute): Release strlen_to_stridx.
* doc/invoke.texi (-Wsizeof-pointer-memaccess): Document enhancement.
(-Wstringop-truncation): Document new option.

gcc/ada/ChangeLog:

PR c/81117
* ada/adadecode.c (__gnat_decode): Use memcpy instead of strncpy.
* ada/argv.c (__gnat_fill_arg, __gnat_fill_env): Same.

gcc/c-family/ChangeLog:

PR c/81117
* c-common.c (catenate_strings): Use memcpy instead of strncpy.
* c-warn.c (sizeof_pointer_memaccess_warning): Handle arrays.
* c.opt (-Wstringop-truncation): New option.

gcc/fortran/ChangeLog:

PR c/81117
* gcc/fortran/decl.c (build_sym): Use strcpy instead of strncpy.

gcc/objc/ChangeLog:

PR c/81117
* objc-encoding.c (encode_type): Use memcpy instead of strncpy.

gcc/testsuite/ChangeLog:

PR c/81117
* c-c++-common/Wsizeof-pointer-memaccess3.c: New test.
* c-c++-common/Wstringop-overflow.c: Same.
* c-c++-common/Wstringop-truncation.c: Same.
* c-c++-common/Wsizeof-pointer-memaccess2.c: Adjust.
* c-c++-common/attr-nonstring-2.c: New test.
* g++.dg/torture/Wsizeof-pointer-memaccess1.C: Adjust.
* g++.dg/torture/Wsizeof-pointer-memaccess2.C: Same.
* gcc.dg/torture/pr63554.c: Same.
* gcc.dg/Walloca-1.c: Disable macro tracking.

From-SVN: r254630
31 files changed:
gcc/ChangeLog
gcc/ada/ChangeLog
gcc/ada/adadecode.c
gcc/ada/argv.c
gcc/builtins.c
gcc/builtins.h
gcc/c-family/ChangeLog
gcc/c-family/c-common.c
gcc/c-family/c-warn.c
gcc/c-family/c.opt
gcc/doc/invoke.texi
gcc/fortran/ChangeLog
gcc/fortran/decl.c
gcc/gimple-fold.c
gcc/gimple.c
gcc/objc/ChangeLog
gcc/objc/objc-encoding.c
gcc/testsuite/ChangeLog
gcc/testsuite/c-c++-common/Wsizeof-pointer-memaccess2.c
gcc/testsuite/c-c++-common/Wsizeof-pointer-memaccess3.c [new file with mode: 0644]
gcc/testsuite/c-c++-common/Wstringop-overflow.c [new file with mode: 0644]
gcc/testsuite/c-c++-common/Wstringop-truncation.c [new file with mode: 0644]
gcc/testsuite/c-c++-common/attr-nonstring-1.c [new file with mode: 0644]
gcc/testsuite/c-c++-common/attr-nonstring-2.c [new file with mode: 0644]
gcc/testsuite/g++.dg/torture/Wsizeof-pointer-memaccess1.C
gcc/testsuite/g++.dg/torture/Wsizeof-pointer-memaccess2.C
gcc/testsuite/gcc.dg/Walloca-1.c
gcc/testsuite/gcc.dg/builtin-stpncpy.c
gcc/testsuite/gcc.dg/torture/Wsizeof-pointer-memaccess1.c
gcc/testsuite/gcc.dg/torture/pr63554.c
gcc/tree-ssa-strlen.c