| 1 |
#include <string.h> |
| 2 |
#include <stdlib.h> |
| 3 |
|
| 4 |
#include "analyzer-decls.h" |
| 5 |
|
| 6 |
extern void requires_nonnull (void *ptr) |
| 7 |
__attribute__((nonnull)); |
| 8 |
|
| 9 |
void test_1 (const char *s) |
| 10 |
{ |
| 11 |
char *p = strdup (s); /* { dg-message "allocated here" } */ |
| 12 |
} /* { dg-warning "leak of 'p'" } */ |
| 13 |
|
| 14 |
void test_2 (const char *s) |
| 15 |
{ |
| 16 |
char *p = strdup (s); |
| 17 |
free (p); |
| 18 |
} |
| 19 |
|
| 20 |
void test_3 (const char *s) |
| 21 |
{ |
| 22 |
char *p = strdup (s); /* { dg-message "this call could return NULL" } */ |
| 23 |
requires_nonnull (p); /* { dg-warning "use of possibly-NULL 'p'" } */ |
| 24 |
} |
| 25 |
|
| 26 |
/* Repeat tests for __builtin_strdup. */ |
| 27 |
void test_4 (const char *s) |
| 28 |
{ |
| 29 |
char *p = __builtin_strdup (s); /* { dg-message "allocated here" } */ |
| 30 |
} /* { dg-warning "leak of 'p'" } */ |
| 31 |
|
| 32 |
void test_5 (const char *s) |
| 33 |
{ |
| 34 |
char *p = __builtin_strdup (s); |
| 35 |
free (p); |
| 36 |
} |
| 37 |
|
| 38 |
void test_6 (const char *s) |
| 39 |
{ |
| 40 |
char *p = __builtin_strdup (s); /* { dg-message "this call could return NULL" } */ |
| 41 |
requires_nonnull (p); /* { dg-warning "use of possibly-NULL 'p'" } */ |
| 42 |
} |
| 43 |
|
| 44 |
char *test_unterminated (void) |
| 45 |
{ |
| 46 |
char buf[3] = "abc"; |
| 47 |
return strdup (buf); /* { dg-warning "stack-based buffer over-read" } */ |
| 48 |
/* { dg-message "while looking for null terminator for argument 1 \\('&buf'\\) of 'strdup'..." "event" { target *-*-* } .-1 } */ |
| 49 |
} |
| 50 |
|
| 51 |
char *test_uninitialized (void) |
| 52 |
{ |
| 53 |
char buf[16]; |
| 54 |
return strdup (buf); /* { dg-warning "use of uninitialized value 'buf\\\[0\\\]'" } */ |
| 55 |
/* { dg-message "while looking for null terminator for argument 1 \\('&buf'\\) of 'strdup'..." "event" { target *-*-* } .-1 } */ |
| 56 |
} |
| 57 |
|
| 58 |
char *test_concrete_strlen (void) |
| 59 |
{ |
| 60 |
char *p = strdup ("abc"); |
| 61 |
if (!p) |
| 62 |
return p; |
| 63 |
__analyzer_eval (__analyzer_get_strlen (p) == 3); /* { dg-warning "TRUE" } */ |
| 64 |
__analyzer_eval (p[0] == 'a'); /* { dg-warning "TRUE" } */ |
| 65 |
__analyzer_eval (p[1] == 'b'); /* { dg-warning "TRUE" } */ |
| 66 |
__analyzer_eval (p[2] == 'c'); /* { dg-warning "TRUE" } */ |
| 67 |
__analyzer_eval (p[3] == '\0'); /* { dg-warning "TRUE" } */ |
| 68 |
return p; |
| 69 |
} |
| 70 |
|
| 71 |
char *test_symbolic_strlen (const char *p) |
| 72 |
{ |
| 73 |
char *q = strdup (p); |
| 74 |
if (!q) |
| 75 |
return q; |
| 76 |
__analyzer_eval (__analyzer_get_strlen (p) == __analyzer_get_strlen (q)); /* { dg-warning "UNKNOWN" } */ |
| 77 |
// TODO: should be TRUE |
| 78 |
__analyzer_eval (p[0] == q[0]); /* { dg-warning "UNKNOWN" } */ |
| 79 |
// TODO: should be TRUE |
| 80 |
return q; |
| 81 |
} |