From b279a9005bfda9f5837aadf22a61c9843eaa862b Mon Sep 17 00:00:00 2001 From: Marshall Clow Date: Thu, 7 Feb 2019 07:52:11 -0800 Subject: [PATCH] Fix off-by-one error in range-checking for 'at()'. thanks to DHilbrich for the bug report. --- include/boost/array.hpp | 2 +- test/array6.cpp | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/include/boost/array.hpp b/include/boost/array.hpp index 210c072..99dc2c6 100644 --- a/include/boost/array.hpp +++ b/include/boost/array.hpp @@ -183,7 +183,7 @@ namespace boost { // check range (may be private because it is static) static BOOST_CONSTEXPR bool rangecheck (size_type i) { - return i > size() ? boost::throw_exception(std::out_of_range ("array<>: index out of range")), true : true; + return i >= size() ? boost::throw_exception(std::out_of_range ("array<>: index out of range")), true : true; } }; diff --git a/test/array6.cpp b/test/array6.cpp index 3d737fd..b8bac55 100644 --- a/test/array6.cpp +++ b/test/array6.cpp @@ -27,6 +27,25 @@ namespace { const arr &caRef = get_c_array ( test_case ); typename test_type::const_iterator iter = test_case.begin (); BOOST_CHECK ( &*iter == &caRef[0] ); + + // Confirm at() throws the std lib defined exception + try { + test_case.at( test_case.size()); + BOOST_CHECK(false); + } + catch ( const std::out_of_range & ) {} + + try { + test_case.at( test_case.size() + 1); + BOOST_CHECK(false); + } + catch ( const std::out_of_range & ) {} + + try { + test_case.at( test_case.size() + 100); + BOOST_CHECK(false); + } + catch ( const std::out_of_range & ) {} } }