diff mbox

[google,gcc-4.9,committed] Add inexpensive bounds checks to std::array

Message ID CALoOobM=nx=yw9p39+QRZ6inQUrF0V6NLK=y3m3LK3C9_Szr+A@mail.gmail.com
State New
Headers show

Commit Message

Paul Pluzhnikov Aug. 1, 2015, 8:30 p.m. UTC
Greetings,

For google b/9650176, attached patch adds bounds checks to std::array
and updates expected line numbers in tests.

This is similar to the checks that we do for std::vector.
See also https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56109

Committed as r226465.

Thanks,
diff mbox

Patch

Index: libstdc++-v3/include/std/array
===================================================================
--- libstdc++-v3/include/std/array	(revision 226462)
+++ libstdc++-v3/include/std/array	(working copy)
@@ -50,7 +50,18 @@ 
 
       static constexpr _Tp*
       _S_ptr(const _Type& __t, std::size_t __n) noexcept
+#if __google_stl_debug_array
+      {
+	return __n < _Nm
+	  ? const_cast<_Tp*>(std::__addressof(__t[__n]))
+          : (std::__throw_out_of_range_fmt(__N("array::_S_ptr: __n "
+                                           "(which is %zu) >= size() "
+                                           "(which is %zu)"),
+                                           __n, _Nm), nullptr);
+      }
+#else
       { return const_cast<_Tp*>(std::__addressof(__t[__n])); }
+#endif
     };
 
  template<typename _Tp>
Index: libstdc++-v3/testsuite/23_containers/array/tuple_interface/get_neg.cc
===================================================================
--- libstdc++-v3/testsuite/23_containers/array/tuple_interface/get_neg.cc	(revision 226462)
+++ libstdc++-v3/testsuite/23_containers/array/tuple_interface/get_neg.cc	(working copy)
@@ -28,6 +28,6 @@ 
 int n2 = std::get<1>(std::move(a));
 int n3 = std::get<1>(ca);
 
-// { dg-error "static assertion failed" "" { target *-*-* } 274 }
-// { dg-error "static assertion failed" "" { target *-*-* } 283 }
-// { dg-error "static assertion failed" "" { target *-*-* } 291 }
+// { dg-error "static assertion failed" "" { target *-*-* } 285 }
+// { dg-error "static assertion failed" "" { target *-*-* } 294 }
+// { dg-error "static assertion failed" "" { target *-*-* } 302 }
Index: libstdc++-v3/testsuite/23_containers/array/tuple_interface/tuple_element_neg.cc
===================================================================
--- libstdc++-v3/testsuite/23_containers/array/tuple_interface/tuple_element_neg.cc	(revision 226462)
+++ libstdc++-v3/testsuite/23_containers/array/tuple_interface/tuple_element_neg.cc	(working copy)
@@ -23,4 +23,4 @@ 
 
 typedef std::tuple_element<1, std::array<int, 1>>::type type;
 
-// { dg-error "static assertion failed" "" { target *-*-* } 320 }
+// { dg-error "static assertion failed" "" { target *-*-* } 331 }