]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Fix -Wstringop-overflow regression
authorMichael Matz <matz@suse.de>
Wed, 4 Apr 2018 14:49:06 +0000 (14:49 +0000)
committerMichael Matz <matz@gcc.gnu.org>
Wed, 4 Apr 2018 14:49:06 +0000 (14:49 +0000)
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
gcc/builtins.c
gcc/testsuite/ChangeLog
gcc/testsuite/c-c++-common/Wstringop-truncation-4.c
gcc/testsuite/gcc.dg/Wstringop-overflow-4.c [new file with mode: 0644]

index c24c3dc090f1a74d3f38c2ce95231374b680f157..f284fe8f350459f0c6ba2ccec36fbf947ee21f38 100644 (file)
@@ -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
index e0f6c2a1c0fd0fffcd5f3959f4af87dd0a77a70b..ababee5db16fd4e6110363be825969d992df3601 100644 (file)
@@ -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).  */
index 9b03492a04eb479ef1daaf1eb417bce500115448..0e7cd5a8a50f4ec89f2b18ab988bfe692c6c5b48 100644 (file)
@@ -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
index c4ad4d6dafc6f922c734b3713684ae9392a72ba8..c76f2823daf3ab4ce78baee6dc2631b53d65dd47 100644 (file)
@@ -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 (file)
index 0000000..74596c4
--- /dev/null
@@ -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;
+}