| 1 |
/* PR tree-optimization/87028 - false positive -Wstringop-truncation |
| 2 |
strncpy with global variable source string |
| 3 |
{ dg-do compile } |
| 4 |
{ dg-options "-O2 -Wstringop-truncation" } */ |
| 5 |
|
| 6 |
char *strncpy (char *, const char *, __SIZE_TYPE__); |
| 7 |
|
| 8 |
#define STR "1234567890" |
| 9 |
|
| 10 |
struct S |
| 11 |
{ |
| 12 |
char a[5], b[5]; |
| 13 |
}; |
| 14 |
|
| 15 |
const char arr[] = STR; |
| 16 |
const char* const ptr = STR; |
| 17 |
|
| 18 |
const char arr2[][10] = { "123", STR }; |
| 19 |
|
| 20 |
void test_literal (struct S *s) |
| 21 |
{ |
| 22 |
strncpy (s->a, STR, sizeof s->a - 1); /* { dg-bogus "\\\[-Wstringop-truncation]" } */ |
| 23 |
s->a[sizeof s->a - 1] = '\0'; |
| 24 |
} |
| 25 |
|
| 26 |
void test_global_arr (struct S *s) |
| 27 |
{ |
| 28 |
strncpy (s->a, arr, sizeof s->a - 1); /* { dg-bogus "\\\[-Wstringop-truncation]" } */ |
| 29 |
s->a [sizeof s->a - 1] = '\0'; |
| 30 |
} |
| 31 |
|
| 32 |
void test_global_arr2 (struct S *s) |
| 33 |
{ |
| 34 |
strncpy (s->a, arr2[1], sizeof s->a - 1); /* { dg-bogus "\\\[-Wstringop-truncation]" } */ |
| 35 |
s->a [sizeof s->a - 1] = '\0'; |
| 36 |
|
| 37 |
strncpy (s->b, arr2[0], sizeof s->a - 1); |
| 38 |
} |
| 39 |
|
| 40 |
void test_global_ptr (struct S *s) |
| 41 |
{ |
| 42 |
strncpy (s->a, ptr, sizeof s->a - 1); /* { dg-bogus "\\\[-Wstringop-truncation]" } */ |
| 43 |
s->a [sizeof s->a - 1] = '\0'; |
| 44 |
} |
| 45 |
|
| 46 |
void test_local_arr (struct S *s) |
| 47 |
{ |
| 48 |
const char arr[] = STR; |
| 49 |
strncpy (s->a, arr, sizeof s->a - 1); |
| 50 |
s->a [sizeof s->a - 1] = '\0'; |
| 51 |
} |
| 52 |
|
| 53 |
void test_local_ptr (struct S *s) |
| 54 |
{ |
| 55 |
const char* const ptr = STR; |
| 56 |
strncpy (s->a, ptr, sizeof s->a - 1); /* { dg-bogus "\\\[-Wstringop-truncation]" } */ |
| 57 |
s->a [sizeof s->a - 1] = '\0'; |
| 58 |
} |
| 59 |
|
| 60 |
void test_compound_literal (struct S *s) |
| 61 |
{ |
| 62 |
strncpy (s->a, (char[]){ STR }, sizeof s->a - 1); |
| 63 |
s->a [sizeof s->a - 1] = '\0'; |
| 64 |
} |