-
Notifications
You must be signed in to change notification settings - Fork 14.7k
[-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
Conversation
…ction 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
@llvm/pr-subscribers-clang Author: Ziqing Luo (ziqingluo-90) ChangesThis commit fixes the false positive that C++ class methods with libc function names would be false warned about. For example,
rdar://156264388 Full diff: https://github.com/llvm/llvm-project/pull/151270.diff 2 Files Affected:
diff --git a/clang/lib/Analysis/UnsafeBufferUsage.cpp b/clang/lib/Analysis/UnsafeBufferUsage.cpp
index ac47b12cc8d72..6cd71c7674a92 100644
--- a/clang/lib/Analysis/UnsafeBufferUsage.cpp
+++ b/clang/lib/Analysis/UnsafeBufferUsage.cpp
@@ -1925,7 +1925,7 @@ class UnsafeLibcFunctionCallGadget : public WarningGadget {
if (!CE || !CE->getDirectCallee())
return false;
const auto *FD = dyn_cast<FunctionDecl>(CE->getDirectCallee());
- if (!FD)
+ if (!FD || isa<CXXMethodDecl>(FD))
return false;
auto isSingleStringLiteralArg = false;
if (CE->getNumArgs() == 1) {
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 d6c32ca7baa5d..e6902c5a95dc0 100644
--- a/clang/test/SemaCXX/warn-unsafe-buffer-usage-libc-functions.cpp
+++ b/clang/test/SemaCXX/warn-unsafe-buffer-usage-libc-functions.cpp
@@ -155,3 +155,21 @@ void ff(char * p, char * q, std::span<char> s, std::span<char> s2) {
wcscpy_s();
#pragma clang diagnostic pop
}
+
+
+namespace CXXMethodNoWarn {
+ struct StrBuff
+ {
+ void strcpy() const;
+ void strcpy(char* dst) const;
+ void Strcpy(char* dst) const;
+ };
+
+ void test(const StrBuff& str)
+ {
+ char buff[64];
+ str.strcpy();
+ str.strcpy(buff);
+ str.Strcpy(buff);
+ }
+} // namespace CXXMethodNoWarn
|
@llvm/pr-subscribers-clang-analysis Author: Ziqing Luo (ziqingluo-90) ChangesThis commit fixes the false positive that C++ class methods with libc function names would be false warned about. For example,
rdar://156264388 Full diff: https://github.com/llvm/llvm-project/pull/151270.diff 2 Files Affected:
diff --git a/clang/lib/Analysis/UnsafeBufferUsage.cpp b/clang/lib/Analysis/UnsafeBufferUsage.cpp
index ac47b12cc8d72..6cd71c7674a92 100644
--- a/clang/lib/Analysis/UnsafeBufferUsage.cpp
+++ b/clang/lib/Analysis/UnsafeBufferUsage.cpp
@@ -1925,7 +1925,7 @@ class UnsafeLibcFunctionCallGadget : public WarningGadget {
if (!CE || !CE->getDirectCallee())
return false;
const auto *FD = dyn_cast<FunctionDecl>(CE->getDirectCallee());
- if (!FD)
+ if (!FD || isa<CXXMethodDecl>(FD))
return false;
auto isSingleStringLiteralArg = false;
if (CE->getNumArgs() == 1) {
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 d6c32ca7baa5d..e6902c5a95dc0 100644
--- a/clang/test/SemaCXX/warn-unsafe-buffer-usage-libc-functions.cpp
+++ b/clang/test/SemaCXX/warn-unsafe-buffer-usage-libc-functions.cpp
@@ -155,3 +155,21 @@ void ff(char * p, char * q, std::span<char> s, std::span<char> s2) {
wcscpy_s();
#pragma clang diagnostic pop
}
+
+
+namespace CXXMethodNoWarn {
+ struct StrBuff
+ {
+ void strcpy() const;
+ void strcpy(char* dst) const;
+ void Strcpy(char* dst) const;
+ };
+
+ void test(const StrBuff& str)
+ {
+ char buff[64];
+ str.strcpy();
+ str.strcpy(buff);
+ str.Strcpy(buff);
+ }
+} // namespace CXXMethodNoWarn
|
CC @dtarditi |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about functions with the same name but in different namespaces? Maybe we should check that the function is either in the global scope or in the std
namespace instead?
I agree that we should probably check for the function being in the global scope or the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM - thanks!
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/169/builds/13795 Here is the relevant piece of the build log for the reference
|
This commit fixes the false positive that C++ class methods with libc function names would be false warned about. For example,
rdar://156264388