diff --git a/include/boost/utility/string_ref.hpp b/include/boost/utility/string_ref.hpp index 706967d..f4697eb 100644 --- a/include/boost/utility/string_ref.hpp +++ b/include/boost/utility/string_ref.hpp @@ -147,15 +147,15 @@ namespace boost { // modifiers void clear() { len_ = 0; } void remove_prefix(size_type n) { - if ( n > len_ ) - n = len_; + if ( n > size()) + BOOST_THROW_EXCEPTION( std::out_of_range ( "string_ref::substr" ) ); ptr_ += n; len_ -= n; } void remove_suffix(size_type n) { - if ( n > len_ ) - n = len_; + if ( n > size()) + BOOST_THROW_EXCEPTION( std::out_of_range ( "string_ref::substr" ) ); len_ -= n; } diff --git a/include/boost/utility/string_view.hpp b/include/boost/utility/string_view.hpp index 35471fb..51b2bd6 100644 --- a/include/boost/utility/string_view.hpp +++ b/include/boost/utility/string_view.hpp @@ -141,15 +141,15 @@ namespace boost { void clear() BOOST_NOEXCEPT { len_ = 0; } // Boost extension BOOST_CXX14_CONSTEXPR void remove_prefix(size_type n) { - if ( n > len_ ) - n = len_; + if ( n > size()) + BOOST_THROW_EXCEPTION( std::out_of_range ( "string_ref::substr" ) ); ptr_ += n; len_ -= n; } BOOST_CXX14_CONSTEXPR void remove_suffix(size_type n) { - if ( n > len_ ) - n = len_; + if ( n > size()) + BOOST_THROW_EXCEPTION( std::out_of_range ( "string_ref::substr" ) ); len_ -= n; } diff --git a/test/string_ref_test1.cpp b/test/string_ref_test1.cpp index fc88905..01584f7 100644 --- a/test/string_ref_test1.cpp +++ b/test/string_ref_test1.cpp @@ -74,6 +74,13 @@ void test_remove ( const std::string &str ) { } } + ref = str; + try { + ref.remove_prefix(ref.size() + 1); + BOOST_TEST(false); + } + catch ( const std::out_of_range &) {} + for ( size_t i = 1; i < sz; ++ i ) { work = str; ref = str; @@ -83,6 +90,13 @@ void test_remove ( const std::string &str ) { ref.remove_suffix (i); } } + + ref = str; + try { + ref.remove_suffix(ref.size() + 1); + BOOST_TEST(false); + } + catch ( const std::out_of_range &) {} } const char *test_strings [] = { diff --git a/test/string_view_test1.cpp b/test/string_view_test1.cpp index c35bbfa..e3fdb24 100644 --- a/test/string_view_test1.cpp +++ b/test/string_view_test1.cpp @@ -75,6 +75,13 @@ void test_remove ( const std::string &str ) { } } + ref = str; + try { + ref.remove_prefix(ref.size() + 1); + BOOST_TEST(false); + } + catch ( const std::out_of_range &) {} + for ( size_t i = 1; i < sz; ++ i ) { work = str; ref = str; @@ -84,6 +91,13 @@ void test_remove ( const std::string &str ) { ref.remove_suffix (i); } } + + ref = str; + try { + ref.remove_suffix(ref.size() + 1); + BOOST_TEST(false); + } + catch ( const std::out_of_range &) {} } void test_hash(const std::string& str) {