diff --git a/CHANGELOG.md b/CHANGELOG.md index eff5dcd5..d9d425c4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +Version 264: + +* Handle overflow in max size calculation in `basic_dynamic_body` + +-------------------------------------------------------------------------------- + Version 263: * Update documentation @@ -135,7 +141,7 @@ Version 250: * Use SaxonHE in reference generation * Cleanup endianness conversions -* Set parser status and flags even if body_limit_ has been reached +* Set parser status and flags even if body_limit_ has been reached -------------------------------------------------------------------------------- diff --git a/include/boost/beast/http/basic_dynamic_body.hpp b/include/boost/beast/http/basic_dynamic_body.hpp index 3fea3737..f49c50da 100644 --- a/include/boost/beast/http/basic_dynamic_body.hpp +++ b/include/boost/beast/http/basic_dynamic_body.hpp @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -89,7 +90,7 @@ struct basic_dynamic_body error_code& ec) { auto const n = buffer_bytes(buffers); - if(body_.size() > body_.max_size() - n) + if(beast::detail::sum_exceeds(body_.size(), n, body_.max_size())) { ec = error::buffer_overflow; return 0; diff --git a/test/beast/http/dynamic_body.cpp b/test/beast/http/dynamic_body.cpp index c5b35b19..179731ee 100644 --- a/test/beast/http/dynamic_body.cpp +++ b/test/beast/http/dynamic_body.cpp @@ -39,7 +39,7 @@ public: } void - run() override + test_success() { std::string const s = "HTTP/1.1 200 OK\r\n" @@ -55,6 +55,34 @@ public: BEAST_EXPECT(buffers_to_string(m.body().data()) == "xyz"); BEAST_EXPECT(to_string(m) == s); } + + void + test_issue1581() + { + std::string const s = + "HTTP/1.1 200 OK\r\n" + "Server: test\r\n" + "Content-Length: 132\r\n" + "\r\n" + "xyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyz" + "xyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyz" + "xyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyz" + "xyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyz"; + test::stream ts(ioc_, s); + response_parser p; + multi_buffer b; + p.get().body().max_size(64); + error_code ec; + read(ts, b, p, ec); + BEAST_EXPECT(ec == http::error::buffer_overflow); + } + + void + run() override + { + test_success(); + test_issue1581(); + } }; BEAST_DEFINE_TESTSUITE(beast,http,dynamic_body);