| 1 |
#include <string.h> |
| 2 |
|
| 3 |
#ifdef __cplusplus |
| 4 |
#define CONST_CAST(type) const_cast<type> |
| 5 |
#else |
| 6 |
#define CONST_CAST(type) |
| 7 |
#endif |
| 8 |
|
| 9 |
/* PR analyzer/95007. */ |
| 10 |
|
| 11 |
void test_1 (void) |
| 12 |
{ |
| 13 |
char *s = CONST_CAST(char *)("foo"); |
| 14 |
s[0] = 'g'; /* { dg-warning "write to string literal" } */ |
| 15 |
} |
| 16 |
|
| 17 |
/* PR c/83347. */ |
| 18 |
|
| 19 |
void test_2 (void) |
| 20 |
{ |
| 21 |
// Technically irrelevant for C++ as fpermissive will warn about invalid conversion. |
| 22 |
memcpy (CONST_CAST(char *)("abc"), "def", 3); /* { dg-warning "write to string literal" } */ |
| 23 |
} |
| 24 |
|
| 25 |
/* PR c/83347. */ |
| 26 |
|
| 27 |
static char * __attribute__((noinline)) |
| 28 |
called_by_test_3 (void) |
| 29 |
{ |
| 30 |
return (char *)"foo"; |
| 31 |
} |
| 32 |
|
| 33 |
void test_3 (void) |
| 34 |
{ |
| 35 |
char *s = called_by_test_3 (); |
| 36 |
s[1] = 'a'; /* { dg-warning "write to string literal" } */ |
| 37 |
} |
| 38 |
|
| 39 |
static char * __attribute__((noinline)) |
| 40 |
called_by_test_4 (int flag) |
| 41 |
{ |
| 42 |
if (flag) |
| 43 |
return (char *)"foo"; |
| 44 |
else |
| 45 |
return (char *)"bar"; |
| 46 |
} |
| 47 |
|
| 48 |
void test_4 (void) |
| 49 |
{ |
| 50 |
char *s = called_by_test_4 (0); |
| 51 |
s[1] = 'z'; /* { dg-warning "write to string literal" } */ |
| 52 |
} |
| 53 |
|
| 54 |
static char * __attribute__((noinline)) |
| 55 |
called_by_test_5 (int flag) |
| 56 |
{ |
| 57 |
if (flag) |
| 58 |
return (char *)"foo"; |
| 59 |
else |
| 60 |
return (char *)"bar"; |
| 61 |
} |
| 62 |
|
| 63 |
void test_5 (int flag) |
| 64 |
{ |
| 65 |
char *s = called_by_test_5 (flag); |
| 66 |
s[1] = 'z'; /* We miss this one, unless we disable state merging. */ |
| 67 |
} |