diff options
author | Neil MacIntosh <neilmac@microsoft.com> | 2016-09-20 08:44:41 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-09-20 08:44:41 -0700 |
commit | 75fb2236ed53a2243d77cba299b922ae4ef64474 (patch) | |
tree | ecbb70b39e0d03e325100c6cb2f87c6eba58af8d | |
parent | 8361aae39eba1bd3b41bd188baa4019aaa8317b1 (diff) | |
parent | 6cb0e3082a2b475d2ef5e9b591c60ae98bb3c483 (diff) | |
download | platform_external_Microsoft-GSL-75fb2236ed53a2243d77cba299b922ae4ef64474.tar.gz platform_external_Microsoft-GSL-75fb2236ed53a2243d77cba299b922ae4ef64474.tar.bz2 platform_external_Microsoft-GSL-75fb2236ed53a2243d77cba299b922ae4ef64474.zip |
Fix negative indices to multi_span not being caught.
Negative indices to multi_span are not flagged with neither exception nor abort.
-rw-r--r-- | gsl/multi_span | 4 | ||||
-rw-r--r-- | tests/multi_span_tests.cpp | 6 |
2 files changed, 8 insertions, 2 deletions
diff --git a/gsl/multi_span b/gsl/multi_span index 0abc7c1..40752aa 100644 --- a/gsl/multi_span +++ b/gsl/multi_span @@ -443,7 +443,7 @@ namespace details template <typename T, size_t Dim = 0> size_type linearize(const T& arr) const { - Expects(arr[Dim] < CurrentRange); // Index is out of range + Expects(arr[Dim] >= 0 && arr[Dim] < CurrentRange); // Index is out of range return this->Base::totalSize() * arr[Dim] + this->Base::template linearize<T, Dim + 1>(arr); } @@ -1510,7 +1510,7 @@ public: template <bool Enabled = (Rank > 1), typename Ret = std::enable_if_t<Enabled, sliced_type>> constexpr Ret operator[](size_type idx) const noexcept { - Expects(idx < bounds_.size()); // index is out of bounds of the array + Expects(idx >= 0 && idx < bounds_.size()); // index is out of bounds of the array const size_type ridx = idx * bounds_.stride(); // index is out of bounds of the underlying data diff --git a/tests/multi_span_tests.cpp b/tests/multi_span_tests.cpp index f04ba84..69cb34b 100644 --- a/tests/multi_span_tests.cpp +++ b/tests/multi_span_tests.cpp @@ -1084,6 +1084,12 @@ SUITE(multi_span_tests) CHECK_THROW(av[10][2], fail_fast); CHECK_THROW((av[{10, 2}]), fail_fast); + + CHECK_THROW(av[-1][0], fail_fast); + CHECK_THROW((av[{-1, 0}]), fail_fast); + + CHECK_THROW(av[0][-1], fail_fast); + CHECK_THROW((av[{0, -1}]), fail_fast); } void overloaded_func(multi_span<const int, dynamic_range, 3, 5> exp, int expected_value) |