[PATCH] D138321: [-Wunsafe-buffer-usage] Ignore array subscript on literal zero

Ziqing Luo via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Nov 18 13:12:32 PST 2022


ziqingluo-90 updated this revision to Diff 476581.

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D138321/new/

https://reviews.llvm.org/D138321

Files:
  clang/lib/Analysis/UnsafeBufferUsage.cpp
  clang/test/SemaCXX/warn-unsafe-buffer-usage.cpp


Index: clang/test/SemaCXX/warn-unsafe-buffer-usage.cpp
===================================================================
--- clang/test/SemaCXX/warn-unsafe-buffer-usage.cpp
+++ clang/test/SemaCXX/warn-unsafe-buffer-usage.cpp
@@ -29,10 +29,10 @@
 char * baz(void);
 
 void testArraySubscripts(int *p, int **pp) {
-  foo(p[0],             // expected-warning{{unchecked operation on raw buffer in expression}}
-      pp[0][0],         // expected-warning2{{unchecked operation on raw buffer in expression}}
-      0[0[pp]],         // expected-warning2{{unchecked operation on raw buffer in expression}}
-      0[pp][0]          // expected-warning2{{unchecked operation on raw buffer in expression}}
+  foo(p[1],             // expected-warning{{unchecked operation on raw buffer in expression}}
+      pp[1][1],         // expected-warning2{{unchecked operation on raw buffer in expression}}
+      1[1[pp]],         // expected-warning2{{unchecked operation on raw buffer in expression}}
+      1[pp][1]          // expected-warning2{{unchecked operation on raw buffer in expression}}
       );
 
   if (p[3]) {           // expected-warning{{unchecked operation on raw buffer in expression}}
@@ -50,11 +50,18 @@
   int a[10], b[10][10];
 
   // Not to warn subscripts on arrays
-  foo(a[0], a[1],
-      0[a], 1[a],
+  foo(a[1], 1[a],
       b[3][4],
       4[b][3],
       4[3[b]]);
+
+  // Not to warn when index is zero
+  foo(p[0], pp[0][0], 0[0[pp]], 0[pp][0],
+      ((int*)bar())[0],
+      0[(int*)bar()],
+      baz()[0],
+      0[baz()]
+      );
 }
 
 void testArraySubscriptsWithAuto(int *p, int **pp) {
@@ -62,19 +69,19 @@
 
   auto ap1 = a;
 
-  foo(ap1[0]);  // expected-warning{{unchecked operation on raw buffer in expression}}
+  foo(ap1[1]);  // expected-warning{{unchecked operation on raw buffer in expression}}
 
   auto ap2 = p;
 
-  foo(ap2[0]);  // expected-warning{{unchecked operation on raw buffer in expression}}
+  foo(ap2[1]);   // expected-warning{{unchecked operation on raw buffer in expression}}
 
   auto ap3 = pp;
 
-  foo(ap3[0][0]); // expected-warning2{{unchecked operation on raw buffer in expression}}
+  foo(ap3[1][1]); // expected-warning2{{unchecked operation on raw buffer in expression}}
 
   auto ap4 = *pp;
 
-  foo(ap4[0]);  // expected-warning{{unchecked operation on raw buffer in expression}}
+  foo(ap4[1]);   // expected-warning{{unchecked operation on raw buffer in expression}}
 }
 
 void testQualifiedParameters(const int * p, const int * const q,
Index: clang/lib/Analysis/UnsafeBufferUsage.cpp
===================================================================
--- clang/lib/Analysis/UnsafeBufferUsage.cpp
+++ clang/lib/Analysis/UnsafeBufferUsage.cpp
@@ -214,10 +214,9 @@
   }
 
   static Matcher matcher() {
-    // FIXME: What if the index is integer literal 0? Should this be
-    // a safe gadget in this case?
     return stmt(
-        arraySubscriptExpr(hasBase(ignoringParenImpCasts(hasPointerType())))
+        arraySubscriptExpr(hasBase(ignoringParenImpCasts(hasPointerType())),
+                           unless(hasIndex(integerLiteral(equals(0)))))
             .bind("arraySubscr"));
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D138321.476581.patch
Type: text/x-patch
Size: 3170 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20221118/2837567f/attachment-0001.bin>


More information about the cfe-commits mailing list