aboutsummaryrefslogtreecommitdiff
path: root/clang/test/SemaCXX/warn-unsafe-buffer-usage-libc-functions.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'clang/test/SemaCXX/warn-unsafe-buffer-usage-libc-functions.cpp')
-rw-r--r--clang/test/SemaCXX/warn-unsafe-buffer-usage-libc-functions.cpp71
1 files changed, 64 insertions, 7 deletions
diff --git a/clang/test/SemaCXX/warn-unsafe-buffer-usage-libc-functions.cpp b/clang/test/SemaCXX/warn-unsafe-buffer-usage-libc-functions.cpp
index 6ae329a..765dcbc 100644
--- a/clang/test/SemaCXX/warn-unsafe-buffer-usage-libc-functions.cpp
+++ b/clang/test/SemaCXX/warn-unsafe-buffer-usage-libc-functions.cpp
@@ -4,8 +4,16 @@
// RUN: -verify %s -x objective-c++
// RUN: %clang_cc1 -std=c++20 -Wno-all -Wunsafe-buffer-usage-in-libc-call \
// RUN: -verify %s
+// RUN: %clang_cc1 -std=c++20 -Wno-all -Wunsafe-buffer-usage-in-libc-call \
+// RUN: -verify %s -DTEST_STD_NS
typedef struct {} FILE;
+typedef unsigned int size_t;
+
+#ifdef TEST_STD_NS
+namespace std {
+#endif
+
void memcpy();
void __asan_memcpy();
void strcpy();
@@ -25,6 +33,11 @@ int sscanf(const char * buffer, const char * format, ... );
int wprintf(const wchar_t* format, ... );
int __asan_printf();
+#ifdef TEST_STD_NS
+} //namespace std
+using namespace std;
+#endif
+
namespace std {
template< class InputIt, class OutputIt >
OutputIt copy( InputIt first, InputIt last,
@@ -64,10 +77,6 @@ namespace std {
typedef basic_string_view<char> string_view;
typedef basic_string_view<wchar_t> wstring_view;
-
- // C function under std:
- void memcpy();
- void strcpy();
}
void f(char * p, char * q, std::span<char> s, std::span<char> s2) {
@@ -77,14 +86,16 @@ void f(char * p, char * q, std::span<char> s, std::span<char> s2) {
aligned_char_ptr_t cp;
memcpy(); // expected-warning{{function 'memcpy' is unsafe}}
- std::memcpy(); // expected-warning{{function 'memcpy' is unsafe}}
__builtin_memcpy(p, q, 64); // expected-warning{{function '__builtin_memcpy' is unsafe}}
__builtin___memcpy_chk(p, q, 8, 64); // expected-warning{{function '__builtin___memcpy_chk' is unsafe}}
__asan_memcpy(); // expected-warning{{function '__asan_memcpy' is unsafe}}
strcpy(); // expected-warning{{function 'strcpy' is unsafe}}
- std::strcpy(); // expected-warning{{function 'strcpy' is unsafe}}
strcpy_s(); // expected-warning{{function 'strcpy_s' is unsafe}}
wcscpy_s(); // expected-warning{{function 'wcscpy_s' is unsafe}}
+#ifdef TEST_STD_NS
+ std::strcpy(); // expected-warning{{function 'strcpy' is unsafe}}
+ std::memcpy(); // expected-warning{{function 'memcpy' is unsafe}}
+#endif
/* Test printfs */
fprintf((FILE*)p, "%s%d", p, *p); // expected-warning{{function 'fprintf' is unsafe}} expected-note{{string argument is not guaranteed to be null-terminated}}
@@ -181,13 +192,59 @@ void ff(char * p, char * q, std::span<char> s, std::span<char> s2) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunsafe-buffer-usage-in-libc-call"
memcpy();
- std::memcpy();
__builtin_memcpy(p, q, 64);
__builtin___memcpy_chk(p, q, 8, 64);
__asan_memcpy();
strcpy();
+#ifdef TEST_STD_NS
std::strcpy();
+ std::memcpy();
+#endif
strcpy_s();
wcscpy_s();
#pragma clang diagnostic pop
}
+
+
+
+// functions not in global scope or std:: namespace are not libc
+// functions regardless of their names:
+struct StrBuff
+{
+ void strcpy();
+ void strcpy(char* dst);
+ void memcpy(void *dst, const void *src, size_t size);
+};
+
+namespace NS {
+ void strcpy();
+ void strcpy(char* dst);
+ void memcpy(void *dst, const void *src, size_t size);
+}
+
+namespace std {
+ // class methods even in std namespace cannot be libc functions:
+ struct LibC
+ {
+ void strcpy();
+ void strcpy(char* dst);
+ void memcpy(void *dst, const void *src, size_t size);
+ };
+}
+
+void test(StrBuff& str)
+{
+ char buff[64];
+ str.strcpy();
+ str.strcpy(buff);
+ str.memcpy(buff, buff, 64);
+ NS::strcpy();
+ NS::strcpy(buff);
+ NS::memcpy(buff, buff, 64);
+
+ std::LibC LibC;
+
+ LibC.strcpy();
+ LibC.strcpy(buff);
+ LibC.memcpy(buff, buff, 64);
+}