diff --git a/CHANGELOG.md b/CHANGELOG.md index 311ac3d1..88131743 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ Version 71: * Fix spurious on_chunk invocation * Call prepare_payload in HTTP example * Check trailers in test +* Fix buffer overflow handling for string_body and mutable_body WebSockets: diff --git a/example/common/mutable_body.hpp b/example/common/mutable_body.hpp index f3b755fe..3d441e53 100644 --- a/example/common/mutable_body.hpp +++ b/example/common/mutable_body.hpp @@ -109,14 +109,21 @@ struct mutable_body if(*content_length > (std::numeric_limits< std::size_t>::max)()) { - ec = boost::system::errc::make_error_code( - boost::system::errc::not_enough_memory); + ec = beast::http::error::buffer_overflow; + return; + } + try + { + body_.reserve(static_cast< + std::size_t>(*content_length)); + } + catch(std::exception const&) + { + ec = beast::http::error::buffer_overflow; return; } - ec.assign(0, ec.category()); - body_.reserve(static_cast< - std::size_t>(*content_length)); } + ec.assign(0, ec.category()); } template @@ -132,7 +139,7 @@ struct mutable_body { body_.resize(len + n); } - catch(std::length_error const&) + catch(std::exception const&) { ec = beast::http::error::buffer_overflow; return 0; diff --git a/include/beast/http/string_body.hpp b/include/beast/http/string_body.hpp index f0d79d59..963d48bd 100644 --- a/include/beast/http/string_body.hpp +++ b/include/beast/http/string_body.hpp @@ -90,18 +90,21 @@ struct string_body if(*content_length > (std::numeric_limits< std::size_t>::max)()) { - ec = make_error_code( - errc::not_enough_memory); + ec = error::buffer_overflow; + return; + } + try + { + body_.reserve(static_cast< + std::size_t>(*content_length)); + } + catch(std::exception const&) + { + ec = error::buffer_overflow; return; } - ec.assign(0, ec.category()); - body_.reserve(static_cast< - std::size_t>(*content_length)); - } - else - { - ec.assign(0, ec.category()); } + ec.assign(0, ec.category()); } template @@ -117,7 +120,7 @@ struct string_body { body_.resize(len + n); } - catch(std::length_error const&) + catch(std::exception const&) { ec = error::buffer_overflow; return 0;