Skip to content

[-Wunsafe-buffer-usage] Do not warn about class methods with libc function names #151270

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions clang/lib/Analysis/UnsafeBufferUsage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1986,6 +1986,14 @@ class UnsafeLibcFunctionCallGadget : public WarningGadget {
const auto *FD = dyn_cast<FunctionDecl>(CE->getDirectCallee());
if (!FD)
return false;

bool IsGlobalAndNotInAnyNamespace =
FD->isGlobal() && !FD->getEnclosingNamespaceContext()->isNamespace();

// A libc function must either be in the std:: namespace or a global
// function that is not in any namespace:
if (!FD->isInStdNamespace() && !IsGlobalAndNotInAnyNamespace)
return false;
auto isSingleStringLiteralArg = false;
if (CE->getNumArgs() == 1) {
isSingleStringLiteralArg =
Expand Down
71 changes: 64 additions & 7 deletions clang/test/SemaCXX/warn-unsafe-buffer-usage-libc-functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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,
Expand Down Expand Up @@ -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) {
Expand All @@ -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}}
Expand Down Expand Up @@ -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);
}