Skip to content

[-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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

mxms0
Copy link

@mxms0 mxms0 commented Feb 18, 2025

Add support for absl Span and string_view types to warn on their unsafe usage.

Copy link

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 @ followed by their GitHub username.

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.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:analysis labels Feb 18, 2025
@llvmbot
Copy link
Member

llvmbot commented Feb 18, 2025

@llvm/pr-subscribers-clang

Author: Max (mxms0)

Changes

Add 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:

  • (modified) clang/lib/Analysis/UnsafeBufferUsage.cpp (+3-1)
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))

@llvmbot
Copy link
Member

llvmbot commented Feb 18, 2025

@llvm/pr-subscribers-clang-analysis

Author: Max (mxms0)

Changes

Add 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:

  • (modified) clang/lib/Analysis/UnsafeBufferUsage.cpp (+3-1)
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))

@mxms0
Copy link
Author

mxms0 commented Feb 18, 2025

cc @jkorous-apple

@mxms0 mxms0 changed the title Add absl::{Span,string_view} to UnsafeBufferUsage analysis [-Wunsafe-buffer-usage] Add absl::{Span,string_view} to UnsafeBufferUsage analysis Feb 18, 2025
Copy link
Contributor

@ilya-biryukov ilya-biryukov left a 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" };
Copy link
Contributor

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".

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.

Copy link
Contributor

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.

Copy link
Contributor

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.

@jkorous-apple
Copy link
Contributor

+1 for tests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:analysis clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants