[committed] analyzer: avoid using <string.h> in malloc-1.c

Message ID 20200131001558.16913-1-dmalcolm@redhat.com
State New
Headers show
Series
  • [committed] analyzer: avoid using <string.h> in malloc-1.c
Related show

Commit Message

David Malcolm Jan. 31, 2020, 12:15 a.m.
This test assumes that memset and strlen have been marked with
__attribute__((nonnull)), which isn't necessarily the case for an
arbitrary <string.h>.  This likely explains these failures:
  FAIL: gcc.dg/analyzer/malloc-1.c  (test for warnings, line 417)
  FAIL: gcc.dg/analyzer/malloc-1.c  (test for warnings, line 418)
  FAIL: gcc.dg/analyzer/malloc-1.c  (test for warnings, line 425)
  FAIL: gcc.dg/analyzer/malloc-1.c  (test for warnings, line 429)
seen in https://gcc.gnu.org/ml/gcc-testresults/2020-01/msg01608.html
on x86_64-apple-darwin18.

Fix it by using the __builtin_ forms.

Successfully regrtested on x86_64-pc-linux-gnu.
Committed to master as 3e990d795405b370dc5315da59ce809750173312.

gcc/testsuite/ChangeLog:
	* gcc.dg/analyzer/malloc-1.c: Remove include of <string.h>.
	Use __builtin_ forms of memset and strlen throughout.
---
 gcc/testsuite/gcc.dg/analyzer/malloc-1.c | 17 ++++++++---------
 1 file changed, 8 insertions(+), 9 deletions(-)

-- 
2.21.0

Comments

Martin Sebor Jan. 31, 2020, 2:03 a.m. | #1
On 1/30/20 5:15 PM, David Malcolm wrote:
> This test assumes that memset and strlen have been marked with

> __attribute__((nonnull)), which isn't necessarily the case for an

> arbitrary <string.h>.


I sometimes find it useful to run tests with a cross-compiler.  Those
that include standard library headers that GCC itself doesn't provide
fail to build, so I've been also avoiding including headers for this
reason (in addition to it causing problems due to the system headers
sometimes containing things that makes the tests unreliable).

Martin

> This likely explains these failures:

>    FAIL: gcc.dg/analyzer/malloc-1.c  (test for warnings, line 417)

>    FAIL: gcc.dg/analyzer/malloc-1.c  (test for warnings, line 418)

>    FAIL: gcc.dg/analyzer/malloc-1.c  (test for warnings, line 425)

>    FAIL: gcc.dg/analyzer/malloc-1.c  (test for warnings, line 429)

> seen in https://gcc.gnu.org/ml/gcc-testresults/2020-01/msg01608.html

> on x86_64-apple-darwin18.

> 

> Fix it by using the __builtin_ forms.

> 

> Successfully regrtested on x86_64-pc-linux-gnu.

> Committed to master as 3e990d795405b370dc5315da59ce809750173312.

> 

> gcc/testsuite/ChangeLog:

> 	* gcc.dg/analyzer/malloc-1.c: Remove include of <string.h>.

> 	Use __builtin_ forms of memset and strlen throughout.

> ---

>   gcc/testsuite/gcc.dg/analyzer/malloc-1.c | 17 ++++++++---------

>   1 file changed, 8 insertions(+), 9 deletions(-)

> 

> diff --git a/gcc/testsuite/gcc.dg/analyzer/malloc-1.c b/gcc/testsuite/gcc.dg/analyzer/malloc-1.c

> index e2e279bd7fd..c13170560af 100644

> --- a/gcc/testsuite/gcc.dg/analyzer/malloc-1.c

> +++ b/gcc/testsuite/gcc.dg/analyzer/malloc-1.c

> @@ -1,6 +1,5 @@

>   #include <alloca.h>

>   #include <stdlib.h>

> -#include <string.h>

>   

>   extern int foo (void);

>   extern int bar (void);

> @@ -71,7 +70,7 @@ void test_7 (void)

>     void *ptr = malloc(4096);

>     if (!ptr)

>       return;

> -  memset(ptr, 0, 4096);

> +  __builtin_memset(ptr, 0, 4096);

>     free(ptr);

>   }

>   

> @@ -80,7 +79,7 @@ void *test_8 (void)

>     void *ptr = malloc(4096);

>     if (!ptr)

>       return NULL;

> -  memset(ptr, 0, 4096);

> +  __builtin_memset(ptr, 0, 4096);

>     return ptr;

>     /* This needs phi nodes to affect equivalence classes, or we get a false report

>        of a leak.  */

> @@ -398,7 +397,7 @@ int test_35 (void)

>     void *ptr = malloc(4096);

>     if (!ptr)

>       return -1;

> -  memset(ptr, 0, 4096);

> +  __builtin_memset(ptr, 0, 4096);

>     free(ptr);

>     return 0;

>   }

> @@ -408,14 +407,14 @@ void test_36 (void)

>     void *ptr = malloc(4096);

>     if (!ptr)

>       return;

> -  memset(ptr, 0, 4096);

> +  __builtin_memset(ptr, 0, 4096);

>     free(ptr);

>   }

>   

>   void *test_37a (void)

>   {

>     void *ptr = malloc(4096); /* { dg-message "this call could return NULL" } */

> -  memset(ptr, 0, 4096); /* { dg-warning "use of possibly-NULL 'ptr' where non-null expected" } */

> +  __builtin_memset(ptr, 0, 4096); /* { dg-warning "use of possibly-NULL 'ptr' where non-null expected" } */

>     return ptr;

>   }

>   

> @@ -424,9 +423,9 @@ int test_37b (void)

>     void *p = malloc(4096);

>     void *q = malloc(4096); /* { dg-message "this call could return NULL" } */

>     if (p) {

> -    memset(p, 0, 4096); /* Not a bug: checked */

> +    __builtin_memset(p, 0, 4096); /* Not a bug: checked */

>     } else {

> -    memset(q, 0, 4096); /* { dg-warning "use of possibly-NULL 'q' where non-null expected" } */

> +    __builtin_memset(q, 0, 4096); /* { dg-warning "use of possibly-NULL 'q' where non-null expected" } */

>     }

>     free(p);

>     free(q);

> @@ -579,7 +578,7 @@ int test_47 (void)

>       int retval = maybe_alloc (&p); /* this might write to "p".  */

>       if (retval)

>         return (retval);

> -    p_size = strlen(p); /* { dg-bogus "non-null expected" } */

> +    p_size = __builtin_strlen(p); /* { dg-bogus "non-null expected" } */

>       free (p);

>     }

>     return p_size;

>

Patch

diff --git a/gcc/testsuite/gcc.dg/analyzer/malloc-1.c b/gcc/testsuite/gcc.dg/analyzer/malloc-1.c
index e2e279bd7fd..c13170560af 100644
--- a/gcc/testsuite/gcc.dg/analyzer/malloc-1.c
+++ b/gcc/testsuite/gcc.dg/analyzer/malloc-1.c
@@ -1,6 +1,5 @@ 
 #include <alloca.h>
 #include <stdlib.h>
-#include <string.h>
 
 extern int foo (void);
 extern int bar (void);
@@ -71,7 +70,7 @@  void test_7 (void)
   void *ptr = malloc(4096);
   if (!ptr)
     return;
-  memset(ptr, 0, 4096);
+  __builtin_memset(ptr, 0, 4096);
   free(ptr);
 }
 
@@ -80,7 +79,7 @@  void *test_8 (void)
   void *ptr = malloc(4096);
   if (!ptr)
     return NULL;
-  memset(ptr, 0, 4096);
+  __builtin_memset(ptr, 0, 4096);
   return ptr;
   /* This needs phi nodes to affect equivalence classes, or we get a false report
      of a leak.  */
@@ -398,7 +397,7 @@  int test_35 (void)
   void *ptr = malloc(4096);
   if (!ptr)
     return -1;
-  memset(ptr, 0, 4096);
+  __builtin_memset(ptr, 0, 4096);
   free(ptr);
   return 0;
 }
@@ -408,14 +407,14 @@  void test_36 (void)
   void *ptr = malloc(4096);
   if (!ptr)
     return;
-  memset(ptr, 0, 4096);
+  __builtin_memset(ptr, 0, 4096);
   free(ptr);
 }
 
 void *test_37a (void)
 {
   void *ptr = malloc(4096); /* { dg-message "this call could return NULL" } */
-  memset(ptr, 0, 4096); /* { dg-warning "use of possibly-NULL 'ptr' where non-null expected" } */
+  __builtin_memset(ptr, 0, 4096); /* { dg-warning "use of possibly-NULL 'ptr' where non-null expected" } */
   return ptr;
 }
 
@@ -424,9 +423,9 @@  int test_37b (void)
   void *p = malloc(4096);
   void *q = malloc(4096); /* { dg-message "this call could return NULL" } */
   if (p) {
-    memset(p, 0, 4096); /* Not a bug: checked */
+    __builtin_memset(p, 0, 4096); /* Not a bug: checked */
   } else {
-    memset(q, 0, 4096); /* { dg-warning "use of possibly-NULL 'q' where non-null expected" } */
+    __builtin_memset(q, 0, 4096); /* { dg-warning "use of possibly-NULL 'q' where non-null expected" } */
   }
   free(p);
   free(q);
@@ -579,7 +578,7 @@  int test_47 (void)
     int retval = maybe_alloc (&p); /* this might write to "p".  */
     if (retval)
       return (retval);
-    p_size = strlen(p); /* { dg-bogus "non-null expected" } */
+    p_size = __builtin_strlen(p); /* { dg-bogus "non-null expected" } */
     free (p);
   }
   return p_size;