[clang] [-Wunsafe-buffer-usage] Ignore constant safe indices in array subscripts (PR #80504)

via cfe-commits cfe-commits at lists.llvm.org
Wed Feb 14 18:00:55 PST 2024


================
@@ -598,16 +623,16 @@ class ArraySubscriptGadget : public WarningGadget {
   }
 
   static Matcher matcher() {
-    // FIXME: What if the index is integer literal 0? Should this be
-    // a safe gadget in this case?
-      // clang-format off
+    // clang-format off
       return stmt(arraySubscriptExpr(
             hasBase(ignoringParenImpCasts(
               anyOf(hasPointerType(), hasArrayType()))),
-            unless(hasIndex(
-                anyOf(integerLiteral(equals(0)), arrayInitIndexExpr())
-             )))
-            .bind(ArraySubscrTag));
+            unless(anyOf(
+              isSafeArraySubscript(),
+              hasIndex(
+                  anyOf(integerLiteral(equals(0)), arrayInitIndexExpr())
----------------
jkorous-apple wrote:

Actually, it is still necessary - we need to somehow cover:
```
int arr[0];
arr[0] = 5;
```
And that has been defined as out of scope for the warning. I don't feel like calling it "safe" and I'd rather have it visible in the top-level matcher.

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


More information about the cfe-commits mailing list