mirror of
https://github.com/boostorg/utility.git
synced 2025-05-09 15:04:00 +00:00
Fix potential overflow in substr; Trac #11536. Also change string_view::copy to use the traits::copy
This commit is contained in:
parent
00f02167e3
commit
0876da45db
@ -162,9 +162,7 @@ namespace boost {
|
|||||||
basic_string_ref substr(size_type pos, size_type n=npos) const {
|
basic_string_ref substr(size_type pos, size_type n=npos) const {
|
||||||
if ( pos > size())
|
if ( pos > size())
|
||||||
BOOST_THROW_EXCEPTION( std::out_of_range ( "string_ref::substr" ) );
|
BOOST_THROW_EXCEPTION( std::out_of_range ( "string_ref::substr" ) );
|
||||||
if ( n == npos || pos + n > size())
|
return basic_string_ref(data() + pos, (std::min)(size() - pos, n));
|
||||||
n = size () - pos;
|
|
||||||
return basic_string_ref ( data() + pos, n );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int compare(basic_string_ref x) const {
|
int compare(basic_string_ref x) const {
|
||||||
|
@ -183,18 +183,14 @@ namespace boost {
|
|||||||
if (pos > size())
|
if (pos > size())
|
||||||
BOOST_THROW_EXCEPTION(std::out_of_range("string_view::copy" ));
|
BOOST_THROW_EXCEPTION(std::out_of_range("string_view::copy" ));
|
||||||
size_type rlen = (std::min)(n, len_ - pos);
|
size_type rlen = (std::min)(n, len_ - pos);
|
||||||
// use std::copy(begin() + pos, begin() + pos + rlen, s) rather than
|
traits_type::copy(s, data() + pos, rlen);
|
||||||
// std::copy_n(begin() + pos, rlen, s) to support pre-C++11 standard libraries
|
|
||||||
std::copy(begin() + pos, begin() + pos + rlen, s);
|
|
||||||
return rlen;
|
return rlen;
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOST_CXX14_CONSTEXPR basic_string_view substr(size_type pos, size_type n=npos) const {
|
BOOST_CXX14_CONSTEXPR basic_string_view substr(size_type pos, size_type n=npos) const {
|
||||||
if ( pos > size())
|
if ( pos > size())
|
||||||
BOOST_THROW_EXCEPTION( std::out_of_range ( "string_view::substr" ) );
|
BOOST_THROW_EXCEPTION( std::out_of_range ( "string_view::substr" ) );
|
||||||
if (n == npos || pos + n > size())
|
return basic_string_view(data() + pos, (std::min)(size() - pos, n));
|
||||||
n = size () - pos;
|
|
||||||
return basic_string_view(data() + pos, n);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOST_CXX14_CONSTEXPR int compare(basic_string_view x) const BOOST_NOEXCEPT {
|
BOOST_CXX14_CONSTEXPR int compare(basic_string_view x) const BOOST_NOEXCEPT {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user