From c7a7d16992e30d60e50634b55ca8b524ba2d971c Mon Sep 17 00:00:00 2001 From: Damian Jarek Date: Fri, 1 Mar 2019 22:55:58 +0100 Subject: [PATCH] Allow the use of string_body and vector_body with -fno-exceptions `string_body` and `vector_body` will no longer translate all exceptions to "buffer_overflow" error code. `buffer_overflow` error can now only occur if the Body's max_size() is exceeded. Changes required: Code that relies on exceptions thrown from value_type's reserve/resize being translated into an error code must implement a mechanism to catch the exception. Signed-off-by: Damian Jarek --- include/boost/beast/http/string_body.hpp | 22 ++++++---------------- include/boost/beast/http/vector_body.hpp | 22 ++++++---------------- 2 files changed, 12 insertions(+), 32 deletions(-) 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);