From 22c27854529b1e5c5da2b4845ffac764f208326d Mon Sep 17 00:00:00 2001 From: Krzysztof Wrzalik Date: Sun, 18 Sep 2016 17:44:45 +0200 Subject: [PATCH 1/2] Added tests for negative multi-span access. --- tests/multi_span_tests.cpp | 6 ++++++ 1 file changed, 6 insertions(+) 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 exp, int expected_value) From 6cb0e3082a2b475d2ef5e9b591c60ae98bb3c483 Mon Sep 17 00:00:00 2001 From: Krzysztof Wrzalik Date: Sun, 18 Sep 2016 17:56:18 +0200 Subject: [PATCH 2/2] Added a fix for not flagging negative indices to multi_span. --- gsl/multi_span | 4 ++-- 1 file changed, 2 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 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(arr); } @@ -1510,7 +1510,7 @@ public: template 1), typename Ret = std::enable_if_t> 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