-
Notifications
You must be signed in to change notification settings - Fork 13.3k
[-Wunsafe-buffer-usage] Add absl::{Span,string_view} to UnsafeBufferUsage analysis #127698
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
base: main
Are you sure you want to change the base?
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-clang Author: Max (mxms0) ChangesAdd support for absl Span and string_view types to warn on their unsafe usage. Full diff: https://github.com/llvm/llvm-project/pull/127698.diff 1 Files Affected:
diff --git a/clang/lib/Analysis/UnsafeBufferUsage.cpp b/clang/lib/Analysis/UnsafeBufferUsage.cpp
index c51398698922b..e0f9a683bdb6c 100644
--- a/clang/lib/Analysis/UnsafeBufferUsage.cpp
+++ b/clang/lib/Analysis/UnsafeBufferUsage.cpp
@@ -891,7 +891,9 @@ AST_MATCHER(CallExpr, hasUnsafeSnprintfBuffer) {
// Pattern 1:
static StringRef SizedObjs[] = {"span", "array", "vector",
- "basic_string_view", "basic_string"};
+ "basic_string_view", "basic_string",
+ // Support absl::Span and absl::string_view
+ "Span", "string_view" };
Buf = Buf->IgnoreParenImpCasts();
Size = Size->IgnoreParenImpCasts();
if (auto *MCEPtr = dyn_cast<CXXMemberCallExpr>(Buf))
|
@llvm/pr-subscribers-clang-analysis Author: Max (mxms0) ChangesAdd support for absl Span and string_view types to warn on their unsafe usage. Full diff: https://github.com/llvm/llvm-project/pull/127698.diff 1 Files Affected:
diff --git a/clang/lib/Analysis/UnsafeBufferUsage.cpp b/clang/lib/Analysis/UnsafeBufferUsage.cpp
index c51398698922b..e0f9a683bdb6c 100644
--- a/clang/lib/Analysis/UnsafeBufferUsage.cpp
+++ b/clang/lib/Analysis/UnsafeBufferUsage.cpp
@@ -891,7 +891,9 @@ AST_MATCHER(CallExpr, hasUnsafeSnprintfBuffer) {
// Pattern 1:
static StringRef SizedObjs[] = {"span", "array", "vector",
- "basic_string_view", "basic_string"};
+ "basic_string_view", "basic_string",
+ // Support absl::Span and absl::string_view
+ "Span", "string_view" };
Buf = Buf->IgnoreParenImpCasts();
Size = Size->IgnoreParenImpCasts();
if (auto *MCEPtr = dyn_cast<CXXMemberCallExpr>(Buf))
|
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.
This makes sense, although I suspect it doesn't work because the code checks for std
namespace.
It would be nice to get a signal from the code owners that they're happy with adding Abseil as a special case in addition to STL. Hopefully it's a no-brainer as it's coming from Googlers, who are owners of Abseil.
Could you also add a test? (search for Wunsafe-buffer-usage
inside clang/test
if you need existing examples)
"basic_string_view", "basic_string"}; | ||
"basic_string_view", "basic_string", | ||
// Support absl::Span and absl::string_view | ||
"Span", "string_view" }; |
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.
I am afraid this will not work because of the following check on line 920:
if (MCEPtr->getRecordDecl()->isInStdNamespace() &&
MCEPtr->getRecordDecl()->getCanonicalDecl()->getName() ==
SizedObj)
You want to change this to take namespace into account somehow. I suggest creating a method IsInNamedNamespace
that accepts the namespace name and passes it all the way down to this function that current passes a string constant "std"
.
llvm-project/clang/lib/AST/DeclBase.cpp
Line 1344 in 3dc1594
return II && II->isStr("std"); |
That way the IsInStdNamespace
will be implemented as return IsInNamedNamespace("std")
PS a more sophisticated version would allow to follow multiple nested namespaces is probably also useful, but I'd not bother with it for now.
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.
yes, please match namespaces. It also helps preventing false negatives. You probably do not want to match any type named Span
.
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.
I agree. We need name space checks here.
+1 for tests |
Add support for absl Span and string_view types to warn on their unsafe usage.