PR middle-end/77357 - strlen of constant strings not folded
[official-gcc.git] / gcc / testsuite / gcc.dg / Wstringop-truncation-2.c
blob707a511d92e0ed6497360f5133b994d21364ad77
1 /* PR tree-optimization/84468 - bogus -Wstringop-truncation despite
2 assignment after conditional strncpy
3 { dg-do compile }
4 { dg-options "-O2 -Wstringop-truncation -g" } */
6 extern char* strncpy (char*, const char*, __SIZE_TYPE__);
8 char a[4];
10 void f1 (char *s)
12 int i = 0;
14 if (s[0] == '0')
16 i += 1;
17 strncpy (a, s, sizeof a); /* { dg-bogus "\\\[-Wstringop-truncation]" } */
19 else
20 i += 2;
22 a[sizeof a - 1] = 0;
25 void f2 (char *s)
27 int i = 0;
29 if (s[0] == '0')
31 i += 1;
32 if (s[1] == '1')
34 i += 2;
35 strncpy (a, s, sizeof a); /* { dg-bogus "\\\[-Wstringop-truncation]" } */
37 else
38 i += 3;
40 else
41 i += 4;
43 a[sizeof a - 1] = 0;
46 void f3 (char *s)
48 int i = 0;
50 if (s[0] == '0')
52 i += 1;
53 if (s[1] == '1')
55 i += 2;
56 if (s[2] == '2')
57 strncpy (a, s, sizeof a); /* { dg-bogus "\\\[-Wstringop-truncation]" } */
58 else
59 i += 3;
61 else
62 i += 4;
64 else
65 i += 5;
67 a[sizeof a - 1] = 0;
70 void f4 (char *s)
72 int i = 0;
74 if (s[0] == '0')
76 i += 1;
77 if (s[1] == '1')
79 i += 2;
80 if (s[2] == '2')
82 i += 3;
83 if (s[3] == '3')
84 strncpy (a, s, sizeof a); /* { dg-bogus "\\\[-Wstringop-truncation]" } */
85 else
86 i += 4;
88 else
89 i += 5;
91 else
92 i += 6;
94 else
95 i += 7;
97 a[sizeof a - 1] = 0;
100 void f4_warn (char *s)
102 int i = 0;
104 if (s[0] == '0')
106 i += 1;
107 if (s[1] == '1')
109 i += 2;
110 if (s[2] == '2')
112 i += 3;
113 if (s[3] == '3')
114 strncpy (a, s, sizeof a); /* { dg-warning "\\\[-Wstringop-truncation]" } */
115 else
116 i += 4;
118 else
119 i += 5;
121 else
122 i += 6;
124 else
125 i += 7;