From 72cf7db931573f85771887a6896451df75513df1 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Mon, 22 Jan 2018 11:57:00 -0800 Subject: [PATCH] Fix use-after-move in example request handlers fix #992 --- CHANGELOG.md | 1 + example/advanced/server-flex/advanced_server_flex.cpp | 7 +++++-- example/advanced/server/advanced_server.cpp | 7 +++++-- example/http/server/async-ssl/http_server_async_ssl.cpp | 7 +++++-- example/http/server/async/http_server_async.cpp | 7 +++++-- example/http/server/coro-ssl/http_server_coro_ssl.cpp | 7 +++++-- example/http/server/coro/http_server_coro.cpp | 7 +++++-- example/http/server/flex/http_server_flex.cpp | 7 +++++-- .../server/stackless-ssl/http_server_stackless_ssl.cpp | 7 +++++-- example/http/server/stackless/http_server_stackless.cpp | 7 +++++-- example/http/server/sync-ssl/http_server_sync_ssl.cpp | 7 +++++-- example/http/server/sync/http_server_sync.cpp | 7 +++++-- 12 files changed, 56 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 85589e2b..59db8209 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ Version 153: * Update README.md for branches * Avoid string_view::clear * Fix iterator version of basic_fields::erase +* Fix use-after-move in example request handlers -------------------------------------------------------------------------------- diff --git a/example/advanced/server-flex/advanced_server_flex.cpp b/example/advanced/server-flex/advanced_server_flex.cpp index dcffdef8..8bca4ad9 100644 --- a/example/advanced/server-flex/advanced_server_flex.cpp +++ b/example/advanced/server-flex/advanced_server_flex.cpp @@ -185,13 +185,16 @@ handle_request( if(ec) return send(server_error(ec.message())); + // Cache the size since we need it after the move + auto const size = body.size(); + // Respond to HEAD request if(req.method() == http::verb::head) { http::response res{http::status::ok, req.version()}; res.set(http::field::server, BOOST_BEAST_VERSION_STRING); res.set(http::field::content_type, mime_type(path)); - res.content_length(body.size()); + res.content_length(size); res.keep_alive(req.keep_alive()); return send(std::move(res)); } @@ -203,7 +206,7 @@ handle_request( std::make_tuple(http::status::ok, req.version())}; res.set(http::field::server, BOOST_BEAST_VERSION_STRING); res.set(http::field::content_type, mime_type(path)); - res.content_length(body.size()); + res.content_length(size); res.keep_alive(req.keep_alive()); return send(std::move(res)); } diff --git a/example/advanced/server/advanced_server.cpp b/example/advanced/server/advanced_server.cpp index 523094b2..f8bda0d7 100644 --- a/example/advanced/server/advanced_server.cpp +++ b/example/advanced/server/advanced_server.cpp @@ -179,13 +179,16 @@ handle_request( if(ec) return send(server_error(ec.message())); + // Cache the size since we need it after the move + auto const size = body.size(); + // Respond to HEAD request if(req.method() == http::verb::head) { http::response res{http::status::ok, req.version()}; res.set(http::field::server, BOOST_BEAST_VERSION_STRING); res.set(http::field::content_type, mime_type(path)); - res.content_length(body.size()); + res.content_length(size); res.keep_alive(req.keep_alive()); return send(std::move(res)); } @@ -197,7 +200,7 @@ handle_request( std::make_tuple(http::status::ok, req.version())}; res.set(http::field::server, BOOST_BEAST_VERSION_STRING); res.set(http::field::content_type, mime_type(path)); - res.content_length(body.size()); + res.content_length(size); res.keep_alive(req.keep_alive()); return send(std::move(res)); } diff --git a/example/http/server/async-ssl/http_server_async_ssl.cpp b/example/http/server/async-ssl/http_server_async_ssl.cpp index 447b151c..810857f3 100644 --- a/example/http/server/async-ssl/http_server_async_ssl.cpp +++ b/example/http/server/async-ssl/http_server_async_ssl.cpp @@ -180,13 +180,16 @@ handle_request( if(ec) return send(server_error(ec.message())); + // Cache the size since we need it after the move + auto const size = body.size(); + // Respond to HEAD request if(req.method() == http::verb::head) { http::response res{http::status::ok, req.version()}; res.set(http::field::server, BOOST_BEAST_VERSION_STRING); res.set(http::field::content_type, mime_type(path)); - res.content_length(body.size()); + res.content_length(size); res.keep_alive(req.keep_alive()); return send(std::move(res)); } @@ -198,7 +201,7 @@ handle_request( std::make_tuple(http::status::ok, req.version())}; res.set(http::field::server, BOOST_BEAST_VERSION_STRING); res.set(http::field::content_type, mime_type(path)); - res.content_length(body.size()); + res.content_length(size); res.keep_alive(req.keep_alive()); return send(std::move(res)); } diff --git a/example/http/server/async/http_server_async.cpp b/example/http/server/async/http_server_async.cpp index 99f57e1c..33f5f411 100644 --- a/example/http/server/async/http_server_async.cpp +++ b/example/http/server/async/http_server_async.cpp @@ -176,13 +176,16 @@ handle_request( if(ec) return send(server_error(ec.message())); + // Cache the size since we need it after the move + auto const size = body.size(); + // Respond to HEAD request if(req.method() == http::verb::head) { http::response res{http::status::ok, req.version()}; res.set(http::field::server, BOOST_BEAST_VERSION_STRING); res.set(http::field::content_type, mime_type(path)); - res.content_length(body.size()); + res.content_length(size); res.keep_alive(req.keep_alive()); return send(std::move(res)); } @@ -194,7 +197,7 @@ handle_request( std::make_tuple(http::status::ok, req.version())}; res.set(http::field::server, BOOST_BEAST_VERSION_STRING); res.set(http::field::content_type, mime_type(path)); - res.content_length(body.size()); + res.content_length(size); res.keep_alive(req.keep_alive()); return send(std::move(res)); } diff --git a/example/http/server/coro-ssl/http_server_coro_ssl.cpp b/example/http/server/coro-ssl/http_server_coro_ssl.cpp index 61f427fd..1f56cd8a 100644 --- a/example/http/server/coro-ssl/http_server_coro_ssl.cpp +++ b/example/http/server/coro-ssl/http_server_coro_ssl.cpp @@ -178,13 +178,16 @@ handle_request( if(ec) return send(server_error(ec.message())); + // Cache the size since we need it after the move + auto const size = body.size(); + // Respond to HEAD request if(req.method() == http::verb::head) { http::response res{http::status::ok, req.version()}; res.set(http::field::server, BOOST_BEAST_VERSION_STRING); res.set(http::field::content_type, mime_type(path)); - res.content_length(body.size()); + res.content_length(size); res.keep_alive(req.keep_alive()); return send(std::move(res)); } @@ -196,7 +199,7 @@ handle_request( std::make_tuple(http::status::ok, req.version())}; res.set(http::field::server, BOOST_BEAST_VERSION_STRING); res.set(http::field::content_type, mime_type(path)); - res.content_length(body.size()); + res.content_length(size); res.keep_alive(req.keep_alive()); return send(std::move(res)); } diff --git a/example/http/server/coro/http_server_coro.cpp b/example/http/server/coro/http_server_coro.cpp index ecd8e03d..acb0edb5 100644 --- a/example/http/server/coro/http_server_coro.cpp +++ b/example/http/server/coro/http_server_coro.cpp @@ -174,13 +174,16 @@ handle_request( if(ec) return send(server_error(ec.message())); + // Cache the size since we need it after the move + auto const size = body.size(); + // Respond to HEAD request if(req.method() == http::verb::head) { http::response res{http::status::ok, req.version()}; res.set(http::field::server, BOOST_BEAST_VERSION_STRING); res.set(http::field::content_type, mime_type(path)); - res.content_length(body.size()); + res.content_length(size); res.keep_alive(req.keep_alive()); return send(std::move(res)); } @@ -192,7 +195,7 @@ handle_request( std::make_tuple(http::status::ok, req.version())}; res.set(http::field::server, BOOST_BEAST_VERSION_STRING); res.set(http::field::content_type, mime_type(path)); - res.content_length(body.size()); + res.content_length(size); res.keep_alive(req.keep_alive()); return send(std::move(res)); } diff --git a/example/http/server/flex/http_server_flex.cpp b/example/http/server/flex/http_server_flex.cpp index 273a5aa1..00c0cf37 100644 --- a/example/http/server/flex/http_server_flex.cpp +++ b/example/http/server/flex/http_server_flex.cpp @@ -180,13 +180,16 @@ handle_request( if(ec) return send(server_error(ec.message())); + // Cache the size since we need it after the move + auto const size = body.size(); + // Respond to HEAD request if(req.method() == http::verb::head) { http::response res{http::status::ok, req.version()}; res.set(http::field::server, BOOST_BEAST_VERSION_STRING); res.set(http::field::content_type, mime_type(path)); - res.content_length(body.size()); + res.content_length(size); res.keep_alive(req.keep_alive()); return send(std::move(res)); } @@ -198,7 +201,7 @@ handle_request( std::make_tuple(http::status::ok, req.version())}; res.set(http::field::server, BOOST_BEAST_VERSION_STRING); res.set(http::field::content_type, mime_type(path)); - res.content_length(body.size()); + res.content_length(size); res.keep_alive(req.keep_alive()); return send(std::move(res)); } diff --git a/example/http/server/stackless-ssl/http_server_stackless_ssl.cpp b/example/http/server/stackless-ssl/http_server_stackless_ssl.cpp index 5d715731..0355777f 100644 --- a/example/http/server/stackless-ssl/http_server_stackless_ssl.cpp +++ b/example/http/server/stackless-ssl/http_server_stackless_ssl.cpp @@ -181,13 +181,16 @@ handle_request( if(ec) return send(server_error(ec.message())); + // Cache the size since we need it after the move + auto const size = body.size(); + // Respond to HEAD request if(req.method() == http::verb::head) { http::response res{http::status::ok, req.version()}; res.set(http::field::server, BOOST_BEAST_VERSION_STRING); res.set(http::field::content_type, mime_type(path)); - res.content_length(body.size()); + res.content_length(size); res.keep_alive(req.keep_alive()); return send(std::move(res)); } @@ -199,7 +202,7 @@ handle_request( std::make_tuple(http::status::ok, req.version())}; res.set(http::field::server, BOOST_BEAST_VERSION_STRING); res.set(http::field::content_type, mime_type(path)); - res.content_length(body.size()); + res.content_length(size); res.keep_alive(req.keep_alive()); return send(std::move(res)); } diff --git a/example/http/server/stackless/http_server_stackless.cpp b/example/http/server/stackless/http_server_stackless.cpp index a1edad5b..5851ec9a 100644 --- a/example/http/server/stackless/http_server_stackless.cpp +++ b/example/http/server/stackless/http_server_stackless.cpp @@ -177,13 +177,16 @@ handle_request( if(ec) return send(server_error(ec.message())); + // Cache the size since we need it after the move + auto const size = body.size(); + // Respond to HEAD request if(req.method() == http::verb::head) { http::response res{http::status::ok, req.version()}; res.set(http::field::server, BOOST_BEAST_VERSION_STRING); res.set(http::field::content_type, mime_type(path)); - res.content_length(body.size()); + res.content_length(size); res.keep_alive(req.keep_alive()); return send(std::move(res)); } @@ -195,7 +198,7 @@ handle_request( std::make_tuple(http::status::ok, req.version())}; res.set(http::field::server, BOOST_BEAST_VERSION_STRING); res.set(http::field::content_type, mime_type(path)); - res.content_length(body.size()); + res.content_length(size); res.keep_alive(req.keep_alive()); return send(std::move(res)); } diff --git a/example/http/server/sync-ssl/http_server_sync_ssl.cpp b/example/http/server/sync-ssl/http_server_sync_ssl.cpp index 9381e65e..51498628 100644 --- a/example/http/server/sync-ssl/http_server_sync_ssl.cpp +++ b/example/http/server/sync-ssl/http_server_sync_ssl.cpp @@ -175,13 +175,16 @@ handle_request( if(ec) return send(server_error(ec.message())); + // Cache the size since we need it after the move + auto const size = body.size(); + // Respond to HEAD request if(req.method() == http::verb::head) { http::response res{http::status::ok, req.version()}; res.set(http::field::server, BOOST_BEAST_VERSION_STRING); res.set(http::field::content_type, mime_type(path)); - res.content_length(body.size()); + res.content_length(size); res.keep_alive(req.keep_alive()); return send(std::move(res)); } @@ -193,7 +196,7 @@ handle_request( std::make_tuple(http::status::ok, req.version())}; res.set(http::field::server, BOOST_BEAST_VERSION_STRING); res.set(http::field::content_type, mime_type(path)); - res.content_length(body.size()); + res.content_length(size); res.keep_alive(req.keep_alive()); return send(std::move(res)); } diff --git a/example/http/server/sync/http_server_sync.cpp b/example/http/server/sync/http_server_sync.cpp index 0a967ce6..327aa54e 100644 --- a/example/http/server/sync/http_server_sync.cpp +++ b/example/http/server/sync/http_server_sync.cpp @@ -173,13 +173,16 @@ handle_request( if(ec) return send(server_error(ec.message())); + // Cache the size since we need it after the move + auto const size = body.size(); + // Respond to HEAD request if(req.method() == http::verb::head) { http::response res{http::status::ok, req.version()}; res.set(http::field::server, BOOST_BEAST_VERSION_STRING); res.set(http::field::content_type, mime_type(path)); - res.content_length(body.size()); + res.content_length(size); res.keep_alive(req.keep_alive()); return send(std::move(res)); } @@ -191,7 +194,7 @@ handle_request( std::make_tuple(http::status::ok, req.version())}; res.set(http::field::server, BOOST_BEAST_VERSION_STRING); res.set(http::field::content_type, mime_type(path)); - res.content_length(body.size()); + res.content_length(size); res.keep_alive(req.keep_alive()); return send(std::move(res)); }