[libcxx-commits] [libcxx] 23776a1 - [libc++] Add assertions on OOB accesses in std::array when the debug mode is enabled

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Mon Jun 1 13:38:00 PDT 2020


Author: Louis Dionne
Date: 2020-06-01T16:37:39-04:00
New Revision: 23776a178f8379e1d9b4d79952bac916c1fa70fe

URL: https://github.com/llvm/llvm-project/commit/23776a178f8379e1d9b4d79952bac916c1fa70fe
DIFF: https://github.com/llvm/llvm-project/commit/23776a178f8379e1d9b4d79952bac916c1fa70fe.diff

LOG: [libc++] Add assertions on OOB accesses in std::array when the debug mode is enabled

Like we do for empty std::array, make sure we have assertions in place
for obvious out-of-bounds issues in std::array when the debug mode is
enabled (which isn't by default).

Added: 
    

Modified: 
    libcxx/include/array

Removed: 
    


################################################################################
diff  --git a/libcxx/include/array b/libcxx/include/array
index 6cd4d199372a..e73bbe7fea5f 100644
--- a/libcxx/include/array
+++ b/libcxx/include/array
@@ -196,9 +196,15 @@ struct _LIBCPP_TEMPLATE_VIS array
 
     // element access:
     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
-    reference operator[](size_type __n)             _NOEXCEPT {return __elems_[__n];}
+    reference operator[](size_type __n) _NOEXCEPT {
+        _LIBCPP_ASSERT(__n < _Size, "out-of-bounds access in std::array<T, N>");
+        return __elems_[__n];
+    }
     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
-    const_reference operator[](size_type __n) const _NOEXCEPT {return __elems_[__n];}
+    const_reference operator[](size_type __n) const _NOEXCEPT {
+        _LIBCPP_ASSERT(__n < _Size, "out-of-bounds access in std::array<T, N>");
+        return __elems_[__n];
+    }
 
     _LIBCPP_CONSTEXPR_AFTER_CXX14 reference at(size_type __n)
     {
@@ -214,10 +220,10 @@ struct _LIBCPP_TEMPLATE_VIS array
         return __elems_[__n];
     }
 
-    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 reference front()             _NOEXCEPT {return __elems_[0];}
-    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 const_reference front() const _NOEXCEPT {return __elems_[0];}
-    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 reference back()              _NOEXCEPT {return __elems_[_Size - 1];}
-    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 const_reference back() const  _NOEXCEPT {return __elems_[_Size - 1];}
+    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 reference front()             _NOEXCEPT {return (*this)[0];}
+    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 const_reference front() const _NOEXCEPT {return (*this)[0];}
+    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 reference back()              _NOEXCEPT {return (*this)[_Size - 1];}
+    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 const_reference back() const  _NOEXCEPT {return (*this)[_Size - 1];}
 
     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
     value_type* data() _NOEXCEPT {return __elems_;}


        


More information about the libcxx-commits mailing list