diff options
author | Michael Matz <matz@suse.de> | 2018-04-04 14:49:06 +0000 |
---|---|---|
committer | Michael Matz <matz@gcc.gnu.org> | 2018-04-04 14:49:06 +0000 |
commit | f1acdcd066f3ba2a4565a9e8a002af897161483d (patch) | |
tree | 7e5d7bc4e8ce12d0b5d26478dd6693af9c618432 | |
parent | eefecf6546957c92ab0073c0e6072c9554f19566 (diff) | |
download | gcc-f1acdcd066f3ba2a4565a9e8a002af897161483d.zip gcc-f1acdcd066f3ba2a4565a9e8a002af897161483d.tar.gz gcc-f1acdcd066f3ba2a4565a9e8a002af897161483d.tar.bz2 |
Fix -Wstringop-overflow regression
we shouldn't claim string overflows for character arrays at
end of structures; the code that tries to avoid these
accidentally passed the address of the accessed member to
array_at_struct_end_p(), but that one wants the component_ref
or array_ref itself. Needs updating of one testcase that
incorrectly expected warning to occur in this situation.
From-SVN: r259083
-rw-r--r-- | gcc/ChangeLog | 5 | ||||
-rw-r--r-- | gcc/builtins.c | 2 | ||||
-rw-r--r-- | gcc/testsuite/ChangeLog | 5 | ||||
-rw-r--r-- | gcc/testsuite/c-c++-common/Wstringop-truncation-4.c | 8 | ||||
-rw-r--r-- | gcc/testsuite/gcc.dg/Wstringop-overflow-4.c | 28 |
5 files changed, 43 insertions, 5 deletions
diff --git a/gcc/ChangeLog b/gcc/ChangeLog index c24c3dc..f284fe8 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,8 @@ +2018-04-04 Michael Matz <matz@suse.de> + + * builtins.c (compute_objsize): Pass correct operand + to array_at_struct_end_p. + 2018-04-04 Richard Biener <rguenther@suse.de> PR lto/85176 diff --git a/gcc/builtins.c b/gcc/builtins.c index e0f6c2a..ababee5 100644 --- a/gcc/builtins.c +++ b/gcc/builtins.c @@ -3377,7 +3377,7 @@ compute_objsize (tree dest, int ostype) type = TYPE_MAIN_VARIANT (type); if (TREE_CODE (type) == ARRAY_TYPE - && !array_at_struct_end_p (dest)) + && !array_at_struct_end_p (TREE_OPERAND (dest, 0))) { /* Return the constant size unless it's zero (that's a zero-length array likely at the end of a struct). */ diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 9b03492..0e7cd5a 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2018-04-04 Michael Matz <matz@suse.de> + + * gcc.dg/Wstringop-overflow-4.c: New test. + * c-c++-common/Wstringop-truncation-4.c: Adjust. + 2018-04-04 Jakub Jelinek <jakub@redhat.com> PR testsuite/85189 diff --git a/gcc/testsuite/c-c++-common/Wstringop-truncation-4.c b/gcc/testsuite/c-c++-common/Wstringop-truncation-4.c index c4ad4d6..c76f282 100644 --- a/gcc/testsuite/c-c++-common/Wstringop-truncation-4.c +++ b/gcc/testsuite/c-c++-common/Wstringop-truncation-4.c @@ -23,7 +23,7 @@ void test_arrays (struct Arrays *p, const char *s) { strncpy (p->a, s, sizeof p->a); /* { dg-warning "\\\[-Wstringop-truncation" } */ strncpy ((char*)p->b, s, sizeof p->b); /* { dg-warning "\\\[-Wstringop-truncation" } */ - strncpy ((char*)p->c, s, sizeof p->c); /* { dg-warning "\\\[-Wstringop-truncation" } */ + strncpy ((char*)p->c, s, sizeof p->c); /* { dg-bogus "\\\[-Wstringop-truncation" } */ } struct Pointers @@ -51,7 +51,7 @@ void test_const_arrays (struct ConstArrays *p, const char *s) { strncpy ((char*)p->a, s, sizeof p->a); /* { dg-warning "\\\[-Wstringop-truncation" } */ strncpy ((char*)p->b, s, sizeof p->b); /* { dg-warning "\\\[-Wstringop-truncation" } */ - strncpy ((char*)p->c, s, sizeof p->c); /* { dg-warning "\\\[-Wstringop-truncation" } */ + strncpy ((char*)p->c, s, sizeof p->c); /* { dg-bogus "\\\[-Wstringop-truncation" } */ } struct ConstPointers @@ -79,7 +79,7 @@ void test_volatile_arrays (struct VolatileArrays *p, const char *s) { strncpy ((char*)p->a, s, sizeof p->a); /* { dg-warning "\\\[-Wstringop-truncation" } */ strncpy ((char*)p->b, s, sizeof p->b); /* { dg-warning "\\\[-Wstringop-truncation" } */ - strncpy ((char*)p->c, s, sizeof p->c); /* { dg-warning "\\\[-Wstringop-truncation" } */ + strncpy ((char*)p->c, s, sizeof p->c); /* { dg-bogus "\\\[-Wstringop-truncation" } */ } struct VolatilePointers @@ -107,7 +107,7 @@ void test_const_volatile_arrays (struct ConstVolatileArrays *p, const char *s) { strncpy ((char*)p->a, s, sizeof p->a); /* { dg-warning "\\\[-Wstringop-truncation" } */ strncpy ((char*)p->b, s, sizeof p->b); /* { dg-warning "\\\[-Wstringop-truncation" } */ - strncpy ((char*)p->c, s, sizeof p->c); /* { dg-warning "\\\[-Wstringop-truncation" } */ + strncpy ((char*)p->c, s, sizeof p->c); /* { dg-bogus "\\\[-Wstringop-truncation" } */ } struct ConstVolatilePointers diff --git a/gcc/testsuite/gcc.dg/Wstringop-overflow-4.c b/gcc/testsuite/gcc.dg/Wstringop-overflow-4.c new file mode 100644 index 0000000..74596c4 --- /dev/null +++ b/gcc/testsuite/gcc.dg/Wstringop-overflow-4.c @@ -0,0 +1,28 @@ +/* { dg-do compile } + { dg-options "-O2 -Wstringop-overflow" } */ + +extern char* strchr (const char*, int); +extern char* strcpy (char*, const char*); +extern void* malloc (__SIZE_TYPE__); +extern __SIZE_TYPE__ strlen (const char *); +struct define_item { + int len; + char value[1]; +}; + +struct define_item * foo(char *name) +{ + char * p; + char * value; + struct define_item * ptr; + + p = strchr (name, '='); + if (1 && p) { + value = p+1; + } else + value = "1"; + + ptr = malloc(sizeof(struct define_item) + strlen(value)); + strcpy(ptr->value, value); /* { dg-bogus "bytes into a region" } */ + return ptr; +} |