| 1 |
/* { dg-skip-if "no strndup in libc" { *-*-darwin[789]* *-*-darwin10* hppa*-*-hpux* *-*-mingw* *-*-vxworks* } } */ |
| 2 |
/* { dg-additional-options "-D_POSIX_C_SOURCE=200809L" } */ |
| 3 |
/* { dg-skip-if "requires hosted libstdc++ for stdlib free" { ! hostedlib } } */ |
| 4 |
|
| 5 |
#include <string.h> |
| 6 |
#include <stdlib.h> |
| 7 |
|
| 8 |
extern void requires_nonnull (void *ptr) |
| 9 |
__attribute__((nonnull)); |
| 10 |
|
| 11 |
void test_1 (const char *s) |
| 12 |
{ |
| 13 |
char *p = strndup (s, 42); /* { dg-message "allocated here" } */ |
| 14 |
} /* { dg-warning "leak of 'p'" } */ |
| 15 |
|
| 16 |
void test_2 (const char *s) |
| 17 |
{ |
| 18 |
char *p = strndup (s, 42); |
| 19 |
free (p); |
| 20 |
} |
| 21 |
void test_3 (const char *s) |
| 22 |
{ |
| 23 |
char *p = strndup (s, 42); /* { dg-message "this call could return NULL" } */ |
| 24 |
requires_nonnull (p); /* { dg-warning "use of possibly-NULL 'p'" } */ |
| 25 |
} |