From 581654e4087ca3c4e15d1f0e0b521fbd4424d0d1 Mon Sep 17 00:00:00 2001 From: Andrey Semashev Date: Tue, 3 May 2022 01:05:56 +0300 Subject: [PATCH] Use assert in string_view::remove_prefix/suffix to enforce preconditions. This is in line with std::string_view::remove_prefix/suffix definition, where calling the method with n > size() is UB. We're keeping the check to clamp n to size() for now for backward compatibility so that it can be eventually removed. Closes https://github.com/boostorg/utility/issues/92. --- include/boost/utility/string_view.hpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/include/boost/utility/string_view.hpp b/include/boost/utility/string_view.hpp index cf47c10..3d195b6 100644 --- a/include/boost/utility/string_view.hpp +++ b/include/boost/utility/string_view.hpp @@ -23,6 +23,7 @@ #include #include #include +#include #include #include @@ -140,6 +141,8 @@ namespace boost { void clear() BOOST_NOEXCEPT { len_ = 0; } // Boost extension BOOST_CXX14_CONSTEXPR void remove_prefix(size_type n) { + BOOST_ASSERT(n <= size()); + // This check is deprecated and is left for backward compatibility. It will be removed in the future. if ( n > len_ ) n = len_; ptr_ += n; @@ -147,6 +150,8 @@ namespace boost { } BOOST_CXX14_CONSTEXPR void remove_suffix(size_type n) { + BOOST_ASSERT(n <= size()); + // This check is deprecated and is left for backward compatibility. It will be removed in the future. if ( n > len_ ) n = len_; len_ -= n;