Debian Codegraph @

clang/test/SemaCXX/warn-unsafe-buffer-usage-libc-functions.cpp

raw
1 // RUN: %clang_cc1 -std=c++20 -Wno-all -Wunsafe-buffer-usage -Wno-gcc-compat\
2 // RUN: -verify %s
3 // RUN: %clang_cc1 -std=c++20 -Wno-all -Wunsafe-buffer-usage -Wno-gcc-compat\
4 // RUN: -verify %s -x objective-c++
5 // RUN: %clang_cc1 -std=c++20 -Wno-all -Wunsafe-buffer-usage-in-libc-call \
6 // RUN: -Wunsafe-buffer-usage-in-format-attr-call -Wno-gcc-compat \
7 // RUN: -verify %s
8 // RUN: %clang_cc1 -std=c++20 -Wno-all -Wunsafe-buffer-usage-in-libc-call \
9 // RUN: -Wunsafe-buffer-usage-in-format-attr-call -Wno-gcc-compat \
10 // RUN: -verify %s -DTEST_STD_NS
11
12 typedef struct {} FILE;
13 typedef unsigned int size_t;
14
15 #ifdef TEST_STD_NS
16 namespace std {
17 #endif
18
19 void memcpy();
20 void __asan_memcpy();
21 void strcpy();
22 void strcpy_s();
23 void wcscpy_s();
24 unsigned strlen( const char* str );
25 int fprintf( FILE* stream, const char* format, ... );
26 int printf( const char* format, ... );
27 int sprintf( char* buffer, const char* format, ... );
28 int swprintf( char* buffer, const char* format, ... );
29 int snprintf( char* buffer, unsigned buf_size, const char* format, ... );
30 int snwprintf( char* buffer, unsigned buf_size, const char* format, ... );
31 int snwprintf_s( char* buffer, unsigned buf_size, const char* format, ... );
32 int vsnprintf( char* buffer, unsigned buf_size, const char* format, ... );
33 int sscanf_s(const char * buffer, const char * format, ...);
34 int sscanf(const char * buffer, const char * format, ... );
35 int wprintf(const wchar_t* format, ... );
36 int __asan_printf();
37
38 #ifdef TEST_STD_NS
39 } //namespace std
40 using namespace std;
41 #endif
42
43 namespace std {
44 template< class InputIt, class OutputIt >
45 OutputIt copy( InputIt first, InputIt last,
46 OutputIt d_first );
47
48 struct iterator{};
49 template<typename T>
50 struct span {
51 T * ptr;
52 T * data();
53 unsigned size_bytes();
54 unsigned size();
55 iterator begin() const noexcept;
56 iterator end() const noexcept;
57 };
58
59 template<typename T>
60 struct basic_string {
61 T* p;
62 T *c_str();
63 T *data();
64 unsigned size_bytes();
65 unsigned size();
66 };
67
68 typedef basic_string<char> string;
69 typedef basic_string<wchar_t> wstring;
70
71 template<typename T>
72 struct basic_string_view {
73 T *c_str() const noexcept;
74 T *data() const noexcept;
75 unsigned size();
76 const T* begin() const noexcept;
77 const T* end() const noexcept;
78 };
79
80 typedef basic_string_view<char> string_view;
81 typedef basic_string_view<wchar_t> wstring_view;
82 }
83
84 void f(char * p, char * q, std::span<char> s, std::span<char> s2) {
85 typedef FILE * _Nullable aligned_file_ptr_t __attribute__((align_value(64)));
86 typedef char * _Nullable aligned_char_ptr_t __attribute__((align_value(64)));
87 aligned_file_ptr_t fp;
88 aligned_char_ptr_t cp;
89
90 memcpy(); // expected-warning{{function 'memcpy' is unsafe}}
91 __builtin_memcpy(p, q, 64); // expected-warning{{function '__builtin_memcpy' is unsafe}}
92 __builtin___memcpy_chk(p, q, 8, 64); // expected-warning{{function '__builtin___memcpy_chk' is unsafe}}
93 __asan_memcpy(); // expected-warning{{function '__asan_memcpy' is unsafe}}
94 strcpy(); // expected-warning{{function 'strcpy' is unsafe}}
95 strcpy_s(); // expected-warning{{function 'strcpy_s' is unsafe}}
96 wcscpy_s(); // expected-warning{{function 'wcscpy_s' is unsafe}}
97 #ifdef TEST_STD_NS
98 std::strcpy(); // expected-warning{{function 'strcpy' is unsafe}}
99 std::memcpy(); // expected-warning{{function 'memcpy' is unsafe}}
100 #endif
101
102 /* Test printfs */
103 fprintf((FILE*)p, "%s%d", p, *p); // expected-warning{{function 'fprintf' is unsafe}} expected-note{{string argument is not guaranteed to be null-terminated}}
104 printf("%s%d", // expected-warning{{function 'printf' is unsafe}}
105 p, // expected-note{{string argument is not guaranteed to be null-terminated}} note attached to the unsafe argument
106 *p);
107 printf(cp, p, *p); // expected-warning{{function 'printf' is unsafe}} // expected-note{{string argument is not guaranteed to be null-terminated}}
108 sprintf(q, "%s%d", "hello", *p); // expected-warning{{function 'sprintf' is unsafe}} expected-note{{change to 'snprintf' for explicit bounds checking}}
109 swprintf(q, "%s%d", "hello", *p); // expected-warning{{function 'swprintf' is unsafe}} expected-note{{change to 'snprintf' for explicit bounds checking}}
110 snprintf(q, 10, "%s%d", "hello", *p); // expected-warning{{function 'snprintf' is unsafe}} expected-note{{buffer pointer and size may not match}}
111 snprintf(cp, 10, "%s%d", "hello", *p); // expected-warning{{function 'snprintf' is unsafe}} expected-note{{buffer pointer and size may not match}}
112 snprintf(s.data(), s2.size(), "%s%d", "hello", *p); // expected-warning{{function 'snprintf' is unsafe}} expected-note{{buffer pointer and size may not match}}
113 printf(nullptr); // expected-warning{{function 'printf' is unsafe}} expected-note{{string argument is not guaranteed to be null-terminated}}
114 printf("%s", nullptr); // expected-warning{{function 'printf' is unsafe}} expected-note{{string argument is not guaranteed to be null-terminated}}
115 snwprintf(s.data(), s2.size(), "%s%d", "hello", *p); // expected-warning{{function 'snwprintf' is unsafe}} expected-note{{buffer pointer and size may not match}}
116 snwprintf_s( // expected-warning{{function 'snwprintf_s' is unsafe}}
117 s.data(), // expected-note{{buffer pointer and size may not match}} // note attached to the buffer
118 s2.size(),
119 "%s%d", "hello", *p);
120 vsnprintf(s.data(), s.size_bytes(), "%s%d", "hello", *p); // expected-warning{{function 'vsnprintf' is unsafe}} expected-note{{'va_list' is unsafe}}
121 sscanf(p, "%s%d", "hello", *p); // expected-warning{{function 'sscanf' is unsafe}}
122 sscanf_s(p, "%s%d", "hello", *p); // expected-warning{{function 'sscanf_s' is unsafe}}
123 fprintf((FILE*)p, "%P%d%p%i hello world %32s", *p, *p, p, *p, p); // expected-warning{{function 'fprintf' is unsafe}} expected-note{{string argument is not guaranteed to be null-terminated}}
124 fprintf(fp, "%P%d%p%i hello world %32s", *p, *p, p, *p, p); // expected-warning{{function 'fprintf' is unsafe}} expected-note{{string argument is not guaranteed to be null-terminated}}
125 wprintf(L"hello %s", p); // expected-warning{{function 'wprintf' is unsafe}} expected-note{{string argument is not guaranteed to be null-terminated}}
126
127
128 char a[10], b[11];
129 int c[10];
130 std::wstring WS;
131
132 snprintf(a, sizeof(b), "%s", __PRETTY_FUNCTION__); // expected-warning{{function 'snprintf' is unsafe}} expected-note{{buffer pointer and size may not match}}
133 snprintf((char*)c, sizeof(c), "%s", __PRETTY_FUNCTION__); // expected-warning{{function 'snprintf' is unsafe}} expected-note{{buffer pointer and size may not match}}
134 fprintf((FILE*)p, "%P%d%p%i hello world %32s", *p, *p, p, *p, "hello"); // no warn
135 fprintf(fp, "%P%d%p%i hello world %32s", *p, *p, p, *p, "hello"); // no warn
136 printf("%s%d", "hello", *p); // no warn
137 snprintf(s.data(), s.size_bytes(), "%s%d", "hello", *p); // no warn
138 snprintf(s.data(), s.size_bytes(), "%s%d", __PRETTY_FUNCTION__, *p); // no warn
139 snwprintf(s.data(), s.size_bytes(), "%s%d", __PRETTY_FUNCTION__, *p); // no warn
140 snwprintf_s(s.data(), s.size_bytes(), "%s%d", __PRETTY_FUNCTION__, *p); // no warn
141 snprintf(s.data(), s.size_bytes(), "%s%d"); // no warn
142 snprintf(s.data(), s.size_bytes(), "%s%d"); // no warn
143 snwprintf(s.data(), s.size_bytes(), "%s%d"); // no warn
144 snwprintf_s(s.data(), s.size_bytes(), "%s%d"); // no warn
145 wprintf(L"hello %ls", L"world"); // no warn
146 wprintf(L"hello %ls", WS.c_str()); // no warn
147 strlen("hello");// no warn
148 __asan_printf();// a printf but no argument, so no warn
149 }
150
151 void safe_examples(std::string s1, int *p) {
152 snprintf(s1.data(), s1.size_bytes(), "%s%d%s%p%s", __PRETTY_FUNCTION__, *p, "hello", p, s1.c_str()); // no warn
153 snprintf(s1.data(), s1.size_bytes(), s1.c_str(), __PRETTY_FUNCTION__, *p, "hello", s1.c_str()); // no warn
154 printf("%s%d%s%p%s", __PRETTY_FUNCTION__, *p, "hello", p, s1.c_str()); // no warn
155 printf(s1.c_str(), __PRETTY_FUNCTION__, *p, "hello", s1.c_str()); // no warn
156 fprintf((FILE*)0, "%s%d%s%p%s", __PRETTY_FUNCTION__, *p, "hello", p, s1.c_str()); // no warn
157 fprintf((FILE*)0, s1.c_str(), __PRETTY_FUNCTION__, *p, "hello", s1.c_str()); // no warn
158
159 char a[10];
160 char c = 'c';
161
162 snprintf(a, sizeof a, "%s%d%s%p%s", __PRETTY_FUNCTION__, *p, "hello", s1.c_str()); // no warn
163 snprintf(a, sizeof(decltype(a)), "%s%d%s%p%s", __PRETTY_FUNCTION__, *p, "hello", s1.c_str()); // no warn
164 snprintf(a, 10, "%s%d%s%p%s", __PRETTY_FUNCTION__, *p, "hello", s1.c_str()); // no warn
165 snprintf(&c, 1, "%s%d%s%p%s", __PRETTY_FUNCTION__, *p, "hello", s1.c_str()); // no warn
166 snprintf(nullptr, 0, "%s%d%s%p%s", __PRETTY_FUNCTION__, *p, "hello", s1.c_str()); // no warn
167
168 strlen(s1.c_str());
169 }
170
171 void test_sarg_precision(std::string Str, std::string_view Sv, std::wstring_view WSv,
172 std::span<char> SpC, std::span<int> SpI) {
173 printf("%.*s");
174 printf("%.*s", (int)Str.size(), Str.data());
175 printf("%.*s", (int)Str.size_bytes(), Str.data());
176 printf("%.*s", (int)Sv.size(), Sv.data());
177 printf("%.*s", (int)SpC.size(), SpC.data());
178 printf("%.*s", SpC.size(), SpC.data());
179 printf("%.*ls", WSv.size(), WSv.data());
180 printf("%.*s", SpC.data()); // no warn because `SpC.data()` is passed to the precision while the actually string pointer is not given
181
182 printf("%.*s", SpI.size(), SpI.data()); // expected-warning {{function 'printf' is unsafe}} expected-note{{string argument is not guaranteed to be null-terminated}}
183 printf("%.*s", SpI.size(), SpC.data()); // expected-warning {{function 'printf' is unsafe}} expected-note{{string argument is not guaranteed to be null-terminated}}
184 printf("%.*s", WSv.size(), WSv.data()); // expected-warning {{function 'printf' is unsafe}} expected-note{{string argument is not guaranteed to be null-terminated}}
185
186 char a[10];
187 int b[10];
188
189 printf("%.10s", a);
190 printf("%.11s", a); // expected-warning {{function 'printf' is unsafe}} expected-note{{string argument is not guaranteed to be null-terminated}}
191 printf("%.10s", b); // expected-warning {{function 'printf' is unsafe}} expected-note{{string argument is not guaranteed to be null-terminated}}
192 }
193
194
195 void g(char *begin, char *end, char *p, std::span<char> s) {
196 std::copy(begin, end, p); // no warn
197 std::copy(s.begin(), s.end(), s.begin()); // no warn
198 }
199
200 // warning gets turned off
201 void ff(char * p, char * q, std::span<char> s, std::span<char> s2) {
202 #pragma clang diagnostic push
203 #pragma clang diagnostic ignored "-Wunsafe-buffer-usage-in-libc-call"
204 memcpy();
205 __builtin_memcpy(p, q, 64);
206 __builtin___memcpy_chk(p, q, 8, 64);
207 __asan_memcpy();
208 strcpy();
209 #ifdef TEST_STD_NS
210 std::strcpy();
211 std::memcpy();
212 #endif
213 strcpy_s();
214 wcscpy_s();
215 #pragma clang diagnostic pop
216 }
217
218
219
220 // functions not in global scope or std:: namespace are not libc
221 // functions regardless of their names:
222 struct StrBuff
223 {
224 void strcpy();
225 void strcpy(char* dst);
226 void memcpy(void *dst, const void *src, size_t size);
227 };
228
229 namespace NS {
230 void strcpy();
231 void strcpy(char* dst);
232 void memcpy(void *dst, const void *src, size_t size);
233 }
234
235 namespace std {
236 // class methods even in std namespace cannot be libc functions:
237 struct LibC
238 {
239 void strcpy();
240 void strcpy(char* dst);
241 void memcpy(void *dst, const void *src, size_t size);
242 };
243 }
244
245 void test(StrBuff& str)
246 {
247 char buff[64];
248 str.strcpy();
249 str.strcpy(buff);
250 str.memcpy(buff, buff, 64);
251 NS::strcpy();
252 NS::strcpy(buff);
253 NS::memcpy(buff, buff, 64);
254
255 std::LibC LibC;
256
257 LibC.strcpy();
258 LibC.strcpy(buff);
259 LibC.memcpy(buff, buff, 64);
260 }
261
262 void dontCrashForInvalidFormatString() {
263 snprintf((char*)0, 0, "%");
264 snprintf((char*)0, 0, "\0");
265 }
266
267
268 // Also warn about unsafe printf/scanf-like functions:
269 void myprintf(const char *, ...) __attribute__((__format__ (__printf__, 1, 2)));
270 void myprintf_2(const char *, int, const char *) __attribute__((__format__ (__printf__, 1, 3)));
271 void myprintf_3(const char *, const char *, int, const char *) __attribute__((__format__ (__printf__, 2, 4)));
272 void myscanf(const char *, ...) __attribute__((__format__ (__scanf__, 1, 2)));
273
274 void myprintf_default(const char *, const char * Fmt = "hello %s",
275 int X = 0, const char * Str = "world") __attribute__((__format__ (__printf__, 2, 4)));
276
277 struct FormatAttrTestMember {
278 void myprintf(const char *, ...) __attribute__((__format__ (__printf__, 2, 3)));
279 void myprintf_2(const char *, int, const char *) __attribute__((__format__ (__printf__, 2, 4)));
280 void myprintf_3(const char *, const char *, int, const char *) __attribute__((__format__ (__printf__, 3, 5)));
281 void myscanf(const char *, ...) __attribute__((__format__ (__scanf__, 2, 3)));
282
283 void operator()(const char * Fmt, ...) __attribute__((__format__ (__printf__, 2, 3)));
284 void operator[](const char * Fmt) __attribute__((__format__ (__printf__, 2, 3)));
285
286
287 static const char * StrField;
288
289 void myprintf_default(const char *, const char * Fmt = "hello %s",
290 int X = 0, const char * Str = StrField) __attribute__((__format__ (__printf__, 3, 5)));
291 };
292
293 struct FormatAttrTestMember2 {
294 void operator()(const char * Fmt, char *) __attribute__((__format__ (__printf__, 2, 3)));
295 };
296
297 void test_format_attr(char * Str, std::string StdStr) {
298 myprintf("hello", Str);
299 myprintf("hello %s", StdStr.c_str());
300 myprintf("hello %s", Str); // expected-warning{{formatting function 'myprintf' is unsafe}} \
301 expected-note{{string argument is not guaranteed to be null-terminated}}
302
303 extern int errno;
304 extern char *strerror(int errnum);
305 myprintf("errno: %s", strerror(errno));
306
307 myprintf_2("hello", 0, Str);
308 myprintf_2("hello %s", 0, StdStr.c_str());
309 myprintf_2("hello %s", 0, Str); // expected-warning{{formatting function 'myprintf_2' is unsafe}} \
310 expected-note{{string argument is not guaranteed to be null-terminated}}
311
312 myprintf_3("irrelevant", "hello", 0, Str);
313 myprintf_3("irrelevant", "hello %s", 0, StdStr.c_str());
314 myprintf_3("irrelevant", "hello %s", 0, Str); // expected-warning{{formatting function 'myprintf_3' is unsafe}} \
315 expected-note{{string argument is not guaranteed to be null-terminated}}
316 myscanf("hello %s");
317 myscanf("hello %s", Str); // expected-warning{{function 'myscanf' is unsafe}}
318
319 myprintf_default("irrelevant");
320
321 int X;
322
323 myscanf("hello %d", &X); // expected-warning{{function 'myscanf' is unsafe}}
324
325 // Test member functions:
326 FormatAttrTestMember Obj;
327
328 Obj.myprintf("hello", Str);
329 Obj.myprintf("hello %s", StdStr.c_str());
330 Obj.myprintf("hello %s", Str); // expected-warning{{formatting function 'myprintf' is unsafe}} \
331 expected-note{{string argument is not guaranteed to be null-terminated}}
332
333 Obj.myprintf_2("hello", 0, Str);
334 Obj.myprintf_2("hello %s", 0, StdStr.c_str());
335 Obj.myprintf_2("hello %s", 0, Str); // expected-warning{{formatting function 'myprintf_2' is unsafe}} \
336 expected-note{{string argument is not guaranteed to be null-terminated}}
337
338 Obj.myprintf_3("irrelevant", "hello", 0, Str);
339 Obj.myprintf_3("irrelevant", "hello %s", 0, StdStr.c_str());
340 Obj.myprintf_3("irrelevant", "hello %s", 0, Str); // expected-warning{{formatting function 'myprintf_3' is unsafe}} \
341 expected-note{{string argument is not guaranteed to be null-terminated}}
342
343 Obj.myscanf("hello %s");
344 Obj.myscanf("hello %s", Str); // expected-warning{{formatting function 'myscanf' is unsafe}}
345
346 Obj.myscanf("hello %d", &X); // expected-warning{{formatting function 'myscanf' is unsafe}}
347
348 Obj.myprintf_default("irrelevant"); // expected-warning{{formatting function 'myprintf_default' is unsafe}}
349 // expected-note@*{{string argument is not guaranteed to be null-terminated}}
350
351 Obj("hello", Str);
352 Obj("hello %s", StdStr.c_str());
353 Obj("hello %s", Str); // expected-warning{{formatting function 'operator()' is unsafe}} \
354 expected-note{{string argument is not guaranteed to be null-terminated}}
355 Obj["hello"];
356 Obj["hello %s"];
357
358 FormatAttrTestMember2 Obj2;
359
360 Obj2("hello", Str);
361 Obj2("hello %s", StdStr.c_str());
362 Obj2("hello %s", Str); // expected-warning{{formatting function 'operator()' is unsafe}} \
363 expected-note{{string argument is not guaranteed to be null-terminated}}
364 }
365
366 // The second attribute argument, which points the starting index of
367 // format string arguments, may not be a valid argument index:
368 void myprintf_arg_idx_oob(const char *) __attribute__((__format__ (__printf__, 1, 2)));
369
370 void test_format_attr_invalid_arg_idx(char * Str, std::string StdStr) {
371 myprintf_arg_idx_oob("hello");
372 myprintf_arg_idx_oob(Str); // expected-warning{{formatting function 'myprintf_arg_idx_oob' is unsafe}} expected-note{{string argument is not guaranteed to be null-terminated}}
373 myprintf_arg_idx_oob(StdStr.c_str());
374 myprintf("hello");
375 myprintf(Str); // expected-warning{{formatting function 'myprintf' is unsafe}} expected-note{{string argument is not guaranteed to be null-terminated}}
376 myprintf(StdStr.c_str());
377 }