From f1acdcd066f3ba2a4565a9e8a002af897161483d Mon Sep 17 00:00:00 2001 From: Michael Matz Date: Wed, 4 Apr 2018 14:49:06 +0000 Subject: [PATCH] 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 --- gcc/ChangeLog | 5 ++++ gcc/builtins.c | 2 +- gcc/testsuite/ChangeLog | 5 ++++ .../c-c++-common/Wstringop-truncation-4.c | 8 +++--- gcc/testsuite/gcc.dg/Wstringop-overflow-4.c | 28 +++++++++++++++++++ 5 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/Wstringop-overflow-4.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index c24c3dc090f..f284fe8f350 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,8 @@ +2018-04-04 Michael Matz + + * builtins.c (compute_objsize): Pass correct operand + to array_at_struct_end_p. + 2018-04-04 Richard Biener PR lto/85176 diff --git a/gcc/builtins.c b/gcc/builtins.c index e0f6c2a1c0f..ababee5db16 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 9b03492a04e..0e7cd5a8a50 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2018-04-04 Michael Matz + + * gcc.dg/Wstringop-overflow-4.c: New test. + * c-c++-common/Wstringop-truncation-4.c: Adjust. + 2018-04-04 Jakub Jelinek 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 c4ad4d6dafc..c76f2823daf 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 00000000000..74596c4c994 --- /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; +} -- 2.30.2