[PATCH] D30192: [Sema] Detecting more array index out of bounds

Daniel Marjamäki via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 21 03:12:36 PST 2017


danielmarjamaki created this revision.

Diagnose array index out of bounds when there is overloaded C++ operators also.


Repository:
  rL LLVM

https://reviews.llvm.org/D30192

Files:
  lib/Sema/SemaChecking.cpp
  test/SemaCXX/array-bounds.cpp


Index: test/SemaCXX/array-bounds.cpp
===================================================================
--- test/SemaCXX/array-bounds.cpp
+++ test/SemaCXX/array-bounds.cpp
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -verify %s
+// RUN: %clang_cc1 -verify -std=c++11 %s
 
 int foo() {
   int x[2]; // expected-note 4 {{array 'x' declared here}}
@@ -253,3 +253,19 @@
 	int a[128]; // expected-note {{array 'a' declared here}}
 	a[(unsigned char)'\xA1'] = 1; // expected-warning {{array index 161 is past the end of the array}}
 }
+
+struct P {
+  int a;
+  int b;
+};
+
+void test_struct_array_index() {
+  struct P p[10]; // expected-note {{array 'p' declared here}}
+  p[11] = {0, 1}; // expected-warning {{array index 11 is past the end of the array (which contains 10 elements)}}
+}
+
+int operator+(const struct P &s1, const struct P &s2);
+int test_operator_overload_struct_array_index() {
+  struct P x[10] = {0}; // expected-note {{array 'x' declared here}}
+  return x[1] + x[11]; // expected-warning {{array index 11 is past the end of the array (which contains 10 elements)}}
+}
Index: lib/Sema/SemaChecking.cpp
===================================================================
--- lib/Sema/SemaChecking.cpp
+++ lib/Sema/SemaChecking.cpp
@@ -10609,6 +10609,12 @@
           CheckArrayAccess(rhs);
         return;
       }
+      case Stmt::CXXOperatorCallExprClass: {
+        const CXXOperatorCallExpr *OCE = cast<CXXOperatorCallExpr>(expr);
+        for (auto Arg : OCE->arguments())
+          CheckArrayAccess(Arg);
+        return;
+      }
       default:
         return;
     }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D30192.89183.patch
Type: text/x-patch
Size: 1591 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170221/7126619f/attachment.bin>


More information about the cfe-commits mailing list