libcpp, c, middle-end: Optimize initializers using #embed in C
[official-gcc.git] / gcc / testsuite / gcc.dg / Wstringop-truncation-5.c
blobf66c0ce1e3f19124e38b098a2912133ab6dfa3e8
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" } */
6 char *strncpy (char *, const char *, __SIZE_TYPE__);
8 #define STR "1234567890"
10 struct S
12 char a[5], b[5];
15 const char arr[] = STR;
16 const char* const ptr = STR;
18 const char arr2[][10] = { "123", STR };
20 void test_literal (struct S *s)
22 strncpy (s->a, STR, sizeof s->a - 1); /* { dg-bogus "\\\[-Wstringop-truncation]" } */
23 s->a[sizeof s->a - 1] = '\0';
26 void test_global_arr (struct S *s)
28 strncpy (s->a, arr, sizeof s->a - 1); /* { dg-bogus "\\\[-Wstringop-truncation]" } */
29 s->a [sizeof s->a - 1] = '\0';
32 void test_global_arr2 (struct S *s)
34 strncpy (s->a, arr2[1], sizeof s->a - 1); /* { dg-bogus "\\\[-Wstringop-truncation]" } */
35 s->a [sizeof s->a - 1] = '\0';
37 strncpy (s->b, arr2[0], sizeof s->a - 1);
40 void test_global_ptr (struct S *s)
42 strncpy (s->a, ptr, sizeof s->a - 1); /* { dg-bogus "\\\[-Wstringop-truncation]" } */
43 s->a [sizeof s->a - 1] = '\0';
46 void test_local_arr (struct S *s)
48 const char arr[] = STR;
49 strncpy (s->a, arr, sizeof s->a - 1);
50 s->a [sizeof s->a - 1] = '\0';
53 void test_local_ptr (struct S *s)
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';
60 void test_compound_literal (struct S *s)
62 strncpy (s->a, (char[]){ STR }, sizeof s->a - 1);
63 s->a [sizeof s->a - 1] = '\0';