aboutsummaryrefslogtreecommitdiff
path: root/gcc/c-family/c-warn.c
diff options
context:
space:
mode:
authorMartin Sebor <msebor@redhat.com>2018-06-12 17:14:31 +0000
committerMartin Sebor <msebor@gcc.gnu.org>2018-06-12 11:14:31 -0600
commitbb0f14ae684a38a44aaf42b312c213aedaa74103 (patch)
tree7e041b94bbe264ea9855d679e7299cd8a1f4281d /gcc/c-family/c-warn.c
parent132afeecc3f5adc88a029f9d1e11ca6d64ab3e8b (diff)
downloadgcc-bb0f14ae684a38a44aaf42b312c213aedaa74103.zip
gcc-bb0f14ae684a38a44aaf42b312c213aedaa74103.tar.gz
gcc-bb0f14ae684a38a44aaf42b312c213aedaa74103.tar.bz2
PR c/85931 - -Wsizeof-pointer-memaccess for strncpy with size of source
gcc/c-family/ChangeLog: PR c/85931 * c-warn.c (sizeof_pointer_memaccess_warning): Avoid warning when sizeof source and destination yields the same value. gcc/ChangeLog: PR c/85931 * fold-const.c (operand_equal_p): Handle SAVE_EXPR. gcc/testsuite/ChangeLog: PR c/85931 * gcc.dg/Wstringop-truncation-3.c: New test. From-SVN: r261515
Diffstat (limited to 'gcc/c-family/c-warn.c')
-rw-r--r--gcc/c-family/c-warn.c17
1 files changed, 15 insertions, 2 deletions
diff --git a/gcc/c-family/c-warn.c b/gcc/c-family/c-warn.c
index 859d72b..95ac09d 100644
--- a/gcc/c-family/c-warn.c
+++ b/gcc/c-family/c-warn.c
@@ -792,13 +792,26 @@ sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee,
{
/* The argument type may be an array. Diagnose bounded string
copy functions that specify the bound in terms of the source
- argument rather than the destination. */
+ argument rather than the destination unless they are equal
+ to one another. Handle constant sizes and also try to handle
+ sizeof expressions involving VLAs. */
if (strop && !cmp && fncode != BUILT_IN_STRNDUP && src)
{
tem = tree_strip_nop_conversions (src);
if (TREE_CODE (tem) == ADDR_EXPR)
tem = TREE_OPERAND (tem, 0);
- if (operand_equal_p (tem, sizeof_arg[idx], OEP_ADDRESS_OF))
+
+ tree d = tree_strip_nop_conversions (dest);
+ if (TREE_CODE (d) == ADDR_EXPR)
+ d = TREE_OPERAND (d, 0);
+
+ tree dstsz = TYPE_SIZE_UNIT (TREE_TYPE (d));
+ tree srcsz = TYPE_SIZE_UNIT (TREE_TYPE (tem));
+
+ if ((!dstsz
+ || !srcsz
+ || !operand_equal_p (dstsz, srcsz, OEP_LEXICOGRAPHIC))
+ && operand_equal_p (tem, sizeof_arg[idx], OEP_ADDRESS_OF))
warning_at (sizeof_arg_loc[idx], OPT_Wsizeof_pointer_memaccess,
"argument to %<sizeof%> in %qD call is the same "
"expression as the source; did you mean to use "