diff options
author | Martin Sebor <msebor@redhat.com> | 2018-06-12 17:14:31 +0000 |
---|---|---|
committer | Martin Sebor <msebor@gcc.gnu.org> | 2018-06-12 11:14:31 -0600 |
commit | bb0f14ae684a38a44aaf42b312c213aedaa74103 (patch) | |
tree | 7e041b94bbe264ea9855d679e7299cd8a1f4281d /gcc/testsuite | |
parent | 132afeecc3f5adc88a029f9d1e11ca6d64ab3e8b (diff) | |
download | gcc-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/testsuite')
-rw-r--r-- | gcc/testsuite/ChangeLog | 5 | ||||
-rw-r--r-- | gcc/testsuite/gcc.dg/Wstringop-truncation-3.c | 59 |
2 files changed, 64 insertions, 0 deletions
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 088f5c2..f9c8d93 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2018-06-12 Martin Sebor <msebor@redhat.com> + + PR c/85931 + * gcc.dg/Wstringop-truncation-3.c: New test. + 2018-06-12 Will Schmidt <will_schmidt@vnet.ibm.com> * gcc.target/powerpc/p8-vec-xl-xst-v2.c: New. diff --git a/gcc/testsuite/gcc.dg/Wstringop-truncation-3.c b/gcc/testsuite/gcc.dg/Wstringop-truncation-3.c new file mode 100644 index 0000000..57f4d64 --- /dev/null +++ b/gcc/testsuite/gcc.dg/Wstringop-truncation-3.c @@ -0,0 +1,59 @@ +/* PR c/85931 - -Wsizeof-pointer-memaccess for strncpy with size of source + { dg-do compile } + { dg-options "-O2 -Wall -Wstringop-truncation -ftrack-macro-expansion=0" } */ + +typedef __SIZE_TYPE__ size_t; + +extern char* strncpy (char*, const char*, size_t); + +extern char a3[3], b3[3]; +extern char a5[5], b5[5]; +extern char ax[], bx[]; + +struct SA +{ + char a3[3], b3[3]; + char a5[5], b5[5]; + char ax[]; +}; + +void sink (void*, ...); + +#define T(d, s, n) sink (strncpy (d, s, n)) + +void test_array (unsigned n) +{ + T (a3, b3, 3); + /* For the following statemenmt, GCC 8.1 issues warning: + + argument to ‘sizeof’ in ‘strncpy’ call is the same expression + as the source; did you mean to use the size of the destination? + + Since the size of both the source and destination the warning + isn't helpful. Verify that it isn't issued. */ + T (a3, b3, sizeof b3); /* { dg-bogus "\\\[-Wsizeof-pointer-memaccess" } */ + + T (a3, ax, sizeof a3); /* { dg-warning "\\\[-Wstringop-truncation" } */ + T (ax, a3, sizeof a3); /* { dg-warning "argument to .sizeof. in .strncpy. call is the same expression as the source" } */ + + char an[n], bn[n]; + sink (an, bn); + + T (an, bn, sizeof bn); /* { dg-bogus "\\\[-Wsizeof-pointer-memaccess" } */ +} + +void test_member_array (struct SA *sa, unsigned n) +{ + T (sa->a3, sa->b3, 3); + T (sa->a3, sa->b3, sizeof sa->b3); /* { dg-bogus "\\\[-Wsizeof-pointer-memaccess" } */ + + T (sa->a3, sa->ax, sizeof sa->a3); /* { dg-warning "\\\[-Wstringop-truncation" } */ + T (sa->ax, sa->a3, sizeof sa->a3); /* { dg-warning "argument to .sizeof. in .strncpy. call is the same expression as the source" } */ + + struct VarLenStruct { + char an[n], bn[n]; + } x; + + sink (&x); + T (x.an, x.bn, sizeof x.bn); /* { dg-bogus "\\\[-Wsizeof-pointer-memaccess" } */ +} |