You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[UnsafeBufferUsage] Merge commit '0abf4975bbf1' from llvm.org/main into next
Conflicts:
clang/lib/Analysis/UnsafeBufferUsage.cpp
The upstream commit no longer matches libc functions in namespaces,
unless the namespace is `::std`. This splits part of the downstream
test case `warn-unsafe-buffer-usage-libc-functions-interop.cpp` to
`warn-unsafe-buffer-usage-libc-functions-interop-annotated.cpp`, to
avoid having to use namespaces to disambiguate the overlapping function
definitions. Also changes some of the code surrounding the merge
conflict to align closer to upstream, (with no change in functionality).
rdar://157725434
// `-Wunsafe-buffer-usage-in-libc-call` yields to the interoperation
36
+
// warnings.
37
+
38
+
// expected-note@+2{{consider using a safe container and passing '.data()' to the parameter 'dst' and '.size()' to its dependent parameter 'size' or 'std::span' and passing '.first(...).data()' to the parameter 'dst'}}
39
+
// expected-note@+1{{consider using a safe container and passing '.data()' to the parameter 'src' and '.size()' to its dependent parameter 'size' or 'std::span' and passing '.first(...).data()' to the parameter 'src'}}
// expected-note@+1{{consider using a safe container and passing '.data()' to the parameter 'buffer' and '.size()' to its dependent parameter 'buf_size' or 'std::span' and passing '.first(...).data()' to the parameter 'buffer'}}
// expected-note@+1 2{{consider using a safe container and passing '.data()' to the parameter 'buffer' and '.size()' to its dependent parameter 'buf_size' or 'std::span' and passing '.first(...).data()' to the parameter 'buffer'}}
// The '__counted_by(10)' is not a correct bounds annotation for
49
+
// 'sprintf'. It is used to test that even if 'sprintf' has bounds
50
+
// annotations, the function will still be warned against as 'sprintf'
51
+
// can't be safe.
52
+
intsprintf( char* __counted_by(10) buffer, const char* format, ... );
53
+
54
+
voidtest(char * p, char * q, constchar * str,
55
+
constchar * __null_terminated safe_str,
56
+
char * __counted_by(n) safe_p,
57
+
size_t n,
58
+
char * __counted_by(10) safe_ten) {
59
+
memcpy(p, q, 10); // expected-warning2{{unsafe assignment to function parameter of count-attributed type}}
60
+
snprintf(p, 10, "%s", "hlo"); // expected-warning{{unsafe assignment to function parameter of count-attributed type}}
61
+
62
+
// We still warn about unsafe string pointer arguments to printfs:
63
+
snprintf(safe_p, n, "%s", str); // expected-warning{{function 'snprintf' is unsafe}} expected-note{{string argument is not guaranteed to be null-terminated}}
64
+
65
+
memcpy(safe_p, safe_p, n); // no warn
66
+
strlen(str); // expected-warning{{passing 'const char *' to parameter of incompatible type 'const char * __terminated_by(0)' (aka 'const char *') is an unsafe operation}}
67
+
snprintf(safe_p, n, "%s", "hlo"); // no warn
68
+
snprintf(safe_p, n, "%s", safe_str); // no warn
69
+
70
+
// v-printf functions and sprintf are still warned about because
71
+
// they cannot be fully safe:
72
+
va_list vlist;
73
+
vsnprintf(safe_p, n, "%s", vlist); // expected-warning{{function 'vsnprintf' is unsafe}} expected-note{{'va_list' is unsafe}}
74
+
sprintf(safe_ten, "%s", safe_str); // expected-warning{{function 'sprintf' is unsafe}} expected-note{{change to 'snprintf' for explicit bounds checking}}
75
+
76
+
}
77
+
78
+
voidtest_wchar(wchar_t * p, wchar_t * q, constwchar_t * wstr,
snwprintf(cxx_wspan.data(), cxx_wspan.size(), cxx_wspan.data()); // expected-warning{{function 'snwprintf' is unsafe}} expected-note{{string argument is not guaranteed to be null-terminated}}
90
+
snwprintf(p, n, L"%ls", safe_wstr); // expected-warning{{unsafe assignment to function parameter of count-attributed type}}
91
+
snwprintf(sizedby_p, n, L"%ls", safe_wstr); // expected-warning{{unsafe assignment to function parameter of count-attributed type}}
92
+
snwprintf(safe_p, n, L"%ls", wstr); // expected-warning{{function 'snwprintf' is unsafe}} expected-note{{string argument is not guaranteed to be null-terminated}}
Copy file name to clipboardExpand all lines: clang/test/SemaCXX/warn-unsafe-buffer-usage-libc-functions-interop.cpp
+6-71Lines changed: 6 additions & 71 deletions
Original file line number
Diff line number
Diff line change
@@ -31,80 +31,16 @@ namespace std {
31
31
typedef basic_string<wchar_t> wstring;
32
32
}
33
33
34
-
namespaceannotated_libc {
35
-
// For libc functions that have annotations,
36
-
// `-Wunsafe-buffer-usage-in-libc-call` yields to the interoperation
37
-
// warnings.
38
-
39
-
// expected-note@+2{{consider using a safe container and passing '.data()' to the parameter 'dst' and '.size()' to its dependent parameter 'size' or 'std::span' and passing '.first(...).data()' to the parameter 'dst'}}
40
-
// expected-note@+1{{consider using a safe container and passing '.data()' to the parameter 'src' and '.size()' to its dependent parameter 'size' or 'std::span' and passing '.first(...).data()' to the parameter 'src'}}
// expected-note@+1{{consider using a safe container and passing '.data()' to the parameter 'buffer' and '.size()' to its dependent parameter 'buf_size' or 'std::span' and passing '.first(...).data()' to the parameter 'buffer'}}
// expected-note@+1 2{{consider using a safe container and passing '.data()' to the parameter 'buffer' and '.size()' to its dependent parameter 'buf_size' or 'std::span' and passing '.first(...).data()' to the parameter 'buffer'}}
// The '__counted_by(10)' is not a correct bounds annotation for
50
-
// 'sprintf'. It is used to test that even if 'sprintf' has bounds
51
-
// annotations, the function will still be warned against as 'sprintf'
52
-
// can't be safe.
53
-
intsprintf( char* __counted_by(10) buffer, const char* format, ... );
54
-
55
-
voidtest(char * p, char * q, constchar * str,
56
-
constchar * __null_terminated safe_str,
57
-
char * __counted_by(n) safe_p,
58
-
size_t n,
59
-
char * __counted_by(10) safe_ten) {
60
-
memcpy(p, q, 10); // expected-warning2{{unsafe assignment to function parameter of count-attributed type}}
61
-
snprintf(p, 10, "%s", "hlo"); // expected-warning{{unsafe assignment to function parameter of count-attributed type}}
62
-
63
-
// We still warn about unsafe string pointer arguments to printfs:
64
-
snprintf(safe_p, n, "%s", str); // expected-warning{{function 'snprintf' is unsafe}} expected-note{{string argument is not guaranteed to be null-terminated}}
65
-
66
-
memcpy(safe_p, safe_p, n); // no warn
67
-
strlen(str); // expected-warning{{passing 'const char *' to parameter of incompatible type 'const char * __terminated_by(0)' (aka 'const char *') is an unsafe operation}}
68
-
snprintf(safe_p, n, "%s", "hlo"); // no warn
69
-
snprintf(safe_p, n, "%s", safe_str); // no warn
70
-
71
-
// v-printf functions and sprintf are still warned about because
72
-
// they cannot be fully safe:
73
-
va_list vlist;
74
-
vsnprintf(safe_p, n, "%s", vlist); // expected-warning{{function 'vsnprintf' is unsafe}} expected-note{{'va_list' is unsafe}}
75
-
sprintf(safe_ten, "%s", safe_str); // expected-warning{{function 'sprintf' is unsafe}} expected-note{{change to 'snprintf' for explicit bounds checking}}
76
-
77
-
}
78
-
79
-
voidtest_wchar(wchar_t * p, wchar_t * q, constwchar_t * wstr,
snwprintf(cxx_wspan.data(), cxx_wspan.size(), cxx_wspan.data()); // expected-warning{{function 'snwprintf' is unsafe}} expected-note{{string argument is not guaranteed to be null-terminated}}
91
-
snwprintf(p, n, L"%ls", safe_wstr); // expected-warning{{unsafe assignment to function parameter of count-attributed type}}
92
-
snwprintf(sizedby_p, n, L"%ls", safe_wstr); // expected-warning{{unsafe assignment to function parameter of count-attributed type}}
93
-
snwprintf(safe_p, n, L"%ls", wstr); // expected-warning{{function 'snwprintf' is unsafe}} expected-note{{string argument is not guaranteed to be null-terminated}}
94
-
}
95
-
} // namespace annotated_libc
96
-
97
-
namespaceunannotated_libc {
98
-
// The -Wunsafe-buffer-usage analysis considers some printf
99
-
// functions safe, arguments are correctly annotated. Because these
100
-
// functions are harder to be changed to C++ equivalents.
34
+
// The -Wunsafe-buffer-usage analysis considers some printf
35
+
// functions safe, arguments are correctly annotated. Because these
36
+
// functions are harder to be changed to C++ equivalents.
snwprintf(cxx_wspan.data(), cxx_wspan.size(), cxx_wspan.data()); // expected-warning{{function 'snwprintf' is unsafe}} expected-note{{string argument is not guaranteed to be null-terminated}}
141
77
snwprintf(sizedby_wp, n, safe_wstr); // expected-warning{{function 'snwprintf' is unsafe}} expected-note{{buffer pointer and size may not match}}
142
78
snwprintf(safe_wp, n, unsafe_wstr); // expected-warning{{function 'snwprintf' is unsafe}} expected-note{{string argument is not guaranteed to be null-terminated}}
memcpy(); // expected-warning{{function 'memcpy' is unsafe}}
80
-
std::memcpy(); // expected-warning{{function 'memcpy' is unsafe}}
81
89
__builtin_memcpy(p, q, 64); // expected-warning{{function '__builtin_memcpy' is unsafe}}
82
90
__builtin___memcpy_chk(p, q, 8, 64); // expected-warning{{function '__builtin___memcpy_chk' is unsafe}}
83
91
__asan_memcpy(); // expected-warning{{function '__asan_memcpy' is unsafe}}
84
92
strcpy(); // expected-warning{{function 'strcpy' is unsafe}}
85
-
std::strcpy(); // expected-warning{{function 'strcpy' is unsafe}}
86
93
strcpy_s(); // expected-warning{{function 'strcpy_s' is unsafe}}
87
94
wcscpy_s(); // expected-warning{{function 'wcscpy_s' is unsafe}}
95
+
#ifdef TEST_STD_NS
96
+
std::strcpy(); // expected-warning{{function 'strcpy' is unsafe}}
97
+
std::memcpy(); // expected-warning{{function 'memcpy' is unsafe}}
98
+
#endif
88
99
89
100
/* Test printfs */
90
101
fprintf((FILE*)p, "%s%d", p, *p); // expected-warning{{function 'fprintf' is unsafe}} expected-note{{string argument is not guaranteed to be null-terminated}}
0 commit comments