1 /* PR tree-optimization/104119 - unexpected -Wformat-overflow after strlen
2 in ILP32 since Ranger integration
4 { dg-options "-O2 -Wall -ftrack-macro-expansion=0" } */
6 typedef __SIZE_TYPE__
size_t;
9 int sprintf (char*, const char*, ...);
10 size_t strlen (const char*);
12 void sink (void*, ...);
14 struct __attribute__ ((packed
)) S
16 char a3
[3], a4
[4], a5
[5], a6
[6], a7
[7], a8
[8], a9
[9], ax
[];
20 extern char a4
[4], a7
[7], a8
[8];
28 size_t n
= strlen (p
->a3
);
29 sprintf (a4
, "%s", p
->a3
); // { dg-bogus "-Wformat-overflow" }
34 size_t n
= strlen (p
->a4
);
35 sprintf (a4
, "%s", p
->a4
); // { dg-bogus "-Wformat-overflow" }
40 size_t n
= strlen (p
->a5
);
41 sprintf (a4
, "%s", p
->a5
); // { dg-warning "may write a terminating nul past the end" }
46 size_t n
= strlen (p
->a7
);
47 sprintf (a8
, "%s", p
->a7
); // { dg-bogus "-Wformat-overflow" }
52 size_t n
= strlen (p
->a8
);
53 sprintf (a8
, "%s", p
->a8
); // { dg-bogus "-Wformat-overflow" }
58 size_t n
= strlen (p
->a9
);
59 sprintf (a8
, "%s", p
->a9
); // { dg-warning "may write a terminating nul past the end " }
64 size_t n
= strlen (p
->ax
);
65 sprintf (a7
, "%s", p
->ax
); // { dg-bogus "-Wformat-overflow" "pr??????" { xfail ilp32 } }
71 /* Verify the warning with a pointer to an allocated object with nonstant
72 size in known range. */
74 void test_alloc_5_8 (int n
)
79 struct S
*p
= (struct S
*)malloc (sizeof *p
+ n
);
80 sink (p
); // initialize *p
83 size_t n
= strlen (p
->a3
);
84 sprintf (a4
, "%s", p
->a3
); // { dg-bogus "-Wformat-overflow" }
89 size_t n
= strlen (p
->a4
);
90 sprintf (a4
, "%s", p
->a4
); // { dg-bogus "-Wformat-overflow" }
95 size_t n
= strlen (p
->a5
);
96 sprintf (a4
, "%s", p
->a5
); // { dg-warning "may write a terminating nul past the end" }
101 size_t n
= strlen (p
->a7
);
102 sprintf (a8
, "%s", p
->a7
); // { dg-bogus "-Wformat-overflow" }
107 size_t n
= strlen (p
->a8
);
108 sprintf (a8
, "%s", p
->a8
); // { dg-bogus "-Wformat-overflow" }
113 size_t n
= strlen (p
->a9
);
114 sprintf (a8
, "%s", p
->a9
); // { dg-warning "may write a terminating nul past the end " }
119 /* The size of the flexible array member p->ax is between 5 and 8
120 bytes so the length of the string stored in it is at most 7.
121 Verify the warning triggers based on its size and also gets
123 size_t n
= strlen (p
->ax
);
124 sprintf (a4
, "%s", p
->ax
); // { dg-warning "writing up to 7 bytes " }
129 size_t n
= strlen (p
->ax
);
130 sprintf (a8
, "%s", p
->ax
);
136 void test_ptr (struct S
*p
)
139 size_t n
= strlen (p
->a3
);
140 sprintf (a4
, "%s", p
->a3
); // { dg-bogus "-Wformat-overflow" }
145 size_t n
= strlen (p
->a4
);
146 sprintf (a4
, "%s", p
->a4
); // { dg-bogus "-Wformat-overflow" }
151 size_t n
= strlen (p
->a5
);
152 sprintf (a4
, "%s", p
->a5
); // { dg-warning "may write a terminating nul past the end" }
157 size_t n
= strlen (p
->a7
);
158 sprintf (a8
, "%s", p
->a7
); // { dg-bogus "-Wformat-overflow" }
163 size_t n
= strlen (p
->a8
);
164 sprintf (a8
, "%s", p
->a8
); // { dg-bogus "-Wformat-overflow" }
169 size_t n
= strlen (p
->a9
);
170 sprintf (a8
, "%s", p
->a9
); // { dg-warning "may write a terminating nul past the end " }
175 size_t n
= strlen (p
->ax
);
176 sprintf (a8
, "%s", p
->ax
); // { dg-bogus "-Wformat-overflow" "pr??????" { xfail ilp32 } }