Skip to content

Commit 50ea604

Browse files
committed
[-Wunsafe-buffer-usage] Do not warn about class methods with libc function names
This commit fixes the false positive that C++ class methods with libc function names would be false warned about. For example, ``` struct T {void strcpy() const;}; void test(const T& t) { str.strcpy(); // no warn } ``` rdar://156264388
1 parent ee1ecf3 commit 50ea604

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

clang/lib/Analysis/UnsafeBufferUsage.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1925,7 +1925,7 @@ class UnsafeLibcFunctionCallGadget : public WarningGadget {
19251925
if (!CE || !CE->getDirectCallee())
19261926
return false;
19271927
const auto *FD = dyn_cast<FunctionDecl>(CE->getDirectCallee());
1928-
if (!FD)
1928+
if (!FD || isa<CXXMethodDecl>(FD))
19291929
return false;
19301930
auto isSingleStringLiteralArg = false;
19311931
if (CE->getNumArgs() == 1) {

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,21 @@ void ff(char * p, char * q, std::span<char> s, std::span<char> s2) {
155155
wcscpy_s();
156156
#pragma clang diagnostic pop
157157
}
158+
159+
160+
namespace CXXMethodNoWarn {
161+
struct StrBuff
162+
{
163+
void strcpy() const;
164+
void strcpy(char* dst) const;
165+
void Strcpy(char* dst) const;
166+
};
167+
168+
void test(const StrBuff& str)
169+
{
170+
char buff[64];
171+
str.strcpy();
172+
str.strcpy(buff);
173+
str.Strcpy(buff);
174+
}
175+
} // namespace CXXMethodNoWarn

0 commit comments

Comments
 (0)