diff --git a/include/boost/beast/http/string_body.hpp b/include/boost/beast/http/string_body.hpp index 6c813dd9..121a718a 100644 --- a/include/boost/beast/http/string_body.hpp +++ b/include/boost/beast/http/string_body.hpp @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -94,21 +95,12 @@ public: { if(length) { - if(static_cast(*length) != *length) - { - ec = error::buffer_overflow; - return; - } - try - { - body_.reserve( - static_cast(*length)); - } - catch(std::exception const&) + if(*length > body_.max_size()) { ec = error::buffer_overflow; return; } + body_.reserve(beast::detail::clamp(*length)); } ec = {}; } @@ -120,15 +112,13 @@ public: { auto const extra = buffer_size(buffers); auto const size = body_.size(); - try - { - body_.resize(size + extra); - } - catch(std::exception const&) + if (extra > body_.max_size() - size) { ec = error::buffer_overflow; return 0; } + + body_.resize(size + extra); ec = {}; CharT* dest = &body_[size]; for(auto b : beast::buffers_range_ref(buffers)) diff --git a/include/boost/beast/http/vector_body.hpp b/include/boost/beast/http/vector_body.hpp index 76195c29..78cf637f 100644 --- a/include/boost/beast/http/vector_body.hpp +++ b/include/boost/beast/http/vector_body.hpp @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -88,21 +89,12 @@ public: { if(length) { - if(static_cast(*length) != *length) - { - ec = error::buffer_overflow; - return; - } - try - { - body_.reserve( - static_cast(*length)); - } - catch(std::exception const&) + if(*length > body_.max_size()) { ec = error::buffer_overflow; return; } + body_.reserve(beast::detail::clamp(*length)); } ec = {}; } @@ -114,15 +106,13 @@ public: { auto const n = buffer_size(buffers); auto const len = body_.size(); - try - { - body_.resize(len + n); - } - catch(std::exception const&) + if (n > body_.max_size() - len) { ec = error::buffer_overflow; return 0; } + + body_.resize(len + n); ec = {}; return net::buffer_copy(net::buffer( &body_[0] + len, n), buffers);