| 1 |
// RUN: %clang_cc1 -std=c++20 -Wno-all -Wunsafe-buffer-usage \ |
| 2 |
// RUN: -verify %s |
| 3 |
|
| 4 |
namespace std { |
| 5 |
inline namespace __1 { |
| 6 |
template< class InputIt, class OutputIt > |
| 7 |
OutputIt copy( InputIt first, InputIt last, |
| 8 |
OutputIt d_first ); |
| 9 |
|
| 10 |
struct iterator{}; |
| 11 |
template<typename T> |
| 12 |
struct span { |
| 13 |
T * ptr; |
| 14 |
T * data(); |
| 15 |
unsigned size_bytes(); |
| 16 |
unsigned size(); |
| 17 |
iterator begin() const noexcept; |
| 18 |
iterator end() const noexcept; |
| 19 |
}; |
| 20 |
|
| 21 |
template<typename T> |
| 22 |
struct basic_string { |
| 23 |
T* p; |
| 24 |
T *c_str(); |
| 25 |
T *data(); |
| 26 |
unsigned size_bytes(); |
| 27 |
}; |
| 28 |
|
| 29 |
typedef basic_string<char> string; |
| 30 |
typedef basic_string<wchar_t> wstring; |
| 31 |
|
| 32 |
// C function under std: |
| 33 |
void memcpy(); |
| 34 |
void strcpy(); |
| 35 |
int snprintf( char* buffer, unsigned buf_size, const char* format, ... ); |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
void f(char * p, char * q, std::span<char> s) { |
| 40 |
std::memcpy(); // expected-warning{{function 'memcpy' is unsafe}} |
| 41 |
std::strcpy(); // expected-warning{{function 'strcpy' is unsafe}} |
| 42 |
std::__1::memcpy(); // expected-warning{{function 'memcpy' is unsafe}} |
| 43 |
std::__1::strcpy(); // expected-warning{{function 'strcpy' is unsafe}} |
| 44 |
|
| 45 |
/* Test printfs */ |
| 46 |
std::snprintf(s.data(), 10, "%s%d", "hello", *p); // expected-warning{{function 'snprintf' is unsafe}} expected-note{{buffer pointer and size may not match}} |
| 47 |
std::__1::snprintf(s.data(), 10, "%s%d", "hello", *p); // expected-warning{{function 'snprintf' is unsafe}} expected-note{{buffer pointer and size may not match}} |
| 48 |
std::snprintf(s.data(), s.size_bytes(), "%s%d", "hello", *p); // no warn |
| 49 |
std::__1::snprintf(s.data(), s.size_bytes(), "%s%d", "hello", *p); // no warn |
| 50 |
} |
| 51 |
|
| 52 |
void v(std::string s1) { |
| 53 |
std::snprintf(s1.data(), s1.size_bytes(), "%s%d", s1.c_str(), 0); // no warn |
| 54 |
std::__1::snprintf(s1.data(), s1.size_bytes(), "%s%d", s1.c_str(), 0); // no warn |
| 55 |
} |
| 56 |
|
| 57 |
void g(char *begin, char *end, char *p, std::span<char> s) { |
| 58 |
std::copy(begin, end, p); // no warn |
| 59 |
std::copy(s.begin(), s.end(), s.begin()); // no warn |
| 60 |
} |