1  #include <string.h>
       2  #include <stdlib.h>
       3  
       4  extern void requires_nonnull (void *ptr)
       5    __attribute__((nonnull));
       6  
       7  void test_1 (const char *s)
       8  {
       9    char *p = strdup (s); /* { dg-message "allocated here" } */
      10  } /* { dg-warning "leak of 'p'" } */
      11  
      12  void test_2 (const char *s)
      13  {
      14    char *p = strdup (s);
      15    free (p);
      16  }
      17  
      18  void test_3 (const char *s)
      19  {
      20    char *p = strdup (s); /* { dg-message "this call could return NULL" } */
      21    requires_nonnull (p); /* { dg-warning "use of possibly-NULL 'p'" } */
      22  }
      23  
      24  /* Repeat tests for __builtin_strdup.  */
      25  void test_4 (const char *s)
      26  {
      27    char *p = __builtin_strdup (s); /* { dg-message "allocated here" } */
      28  } /* { dg-warning "leak of 'p'" } */
      29  
      30  void test_5 (const char *s)
      31  {
      32    char *p = __builtin_strdup (s);
      33    free (p);
      34  }
      35  
      36  void test_6 (const char *s)
      37  {
      38    char *p = __builtin_strdup (s); /* { dg-message "this call could return NULL" } */
      39    requires_nonnull (p); /* { dg-warning "use of possibly-NULL 'p'" } */
      40  }