[clang] [-Wunsafe-buffer-usage] Add absl::{Span,string_view} to UnsafeBufferUsage analysis (PR #127698)

Ilya Biryukov via cfe-commits cfe-commits at lists.llvm.org
Mon Feb 24 02:40:25 PST 2025


================
@@ -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" };
----------------
ilya-biryukov wrote:

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

https://github.com/llvm/llvm-project/blob/3dc159431be7a8c5f1a26a8bd57794f1c7008969/clang/lib/AST/DeclBase.cpp#L1344


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.


https://github.com/llvm/llvm-project/pull/127698


More information about the cfe-commits mailing list