Handle bad_alloc in parser

This commit is contained in:
Vinnie Falco
2017-06-23 02:22:15 -07:00
parent f2e8af23f4
commit dab5d3bc12
5 changed files with 41 additions and 10 deletions

View File

@@ -5,6 +5,7 @@ Version 66:
* Make consuming_buffers smaller * Make consuming_buffers smaller
* Fix costly potential value-init in parser * Fix costly potential value-init in parser
* Fix unused parameter warning * Fix unused parameter warning
* Handle bad_alloc in parser
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------

View File

@@ -80,6 +80,13 @@ enum class error
*/ */
buffer_overflow, buffer_overflow,
/** A memory allocation failed.
When basic_fields throws std::bad_alloc, it is
converted into this error by @ref parser.
*/
bad_alloc,
// //
// (parser errors) // (parser errors)
// //

View File

@@ -44,6 +44,7 @@ public:
case error::unexpected_body: return "unexpected body"; case error::unexpected_body: return "unexpected body";
case error::need_buffer: return "need buffer"; case error::need_buffer: return "need buffer";
case error::buffer_overflow: return "buffer overflow"; case error::buffer_overflow: return "buffer overflow";
case error::bad_alloc: return "bad alloc";
case error::bad_line_ending: return "bad line ending"; case error::bad_line_ending: return "bad line ending";
case error::bad_method: return "bad method"; case error::bad_method: return "bad method";
case error::bad_path: return "bad path"; case error::bad_path: return "bad path";

View File

@@ -198,12 +198,19 @@ private:
on_request(verb method, string_view method_str, on_request(verb method, string_view method_str,
string_view target, int version, error_code& ec) string_view target, int version, error_code& ec)
{ {
ec.assign(0, ec.category()); try
m_.target(target); {
if(method != verb::unknown) m_.target(target);
m_.method(method); if(method != verb::unknown)
else m_.method(method);
m_.method_string(method_str); else
m_.method_string(method_str);
ec.assign(0, ec.category());
}
catch(std::bad_alloc const&)
{
ec = error::bad_alloc;
}
m_.version = version; m_.version = version;
} }
@@ -212,18 +219,32 @@ private:
string_view reason, string_view reason,
int version, error_code& ec) int version, error_code& ec)
{ {
ec.assign(0, ec.category());
m_.result(code); m_.result(code);
m_.version = version; m_.version = version;
m_.reason(reason); try
{
m_.reason(reason);
ec.assign(0, ec.category());
}
catch(std::bad_alloc const&)
{
ec = error::bad_alloc;
}
} }
void void
on_field(field name, string_view name_string, on_field(field name, string_view name_string,
string_view value, error_code& ec) string_view value, error_code& ec)
{ {
ec.assign(0, ec.category()); try
m_.insert(name, name_string, value); {
m_.insert(name, name_string, value);
ec.assign(0, ec.category());
}
catch(std::bad_alloc const&)
{
ec = error::bad_alloc;
}
} }
void void

View File

@@ -42,6 +42,7 @@ public:
check("beast.http", error::unexpected_body); check("beast.http", error::unexpected_body);
check("beast.http", error::need_buffer); check("beast.http", error::need_buffer);
check("beast.http", error::buffer_overflow); check("beast.http", error::buffer_overflow);
check("beast.http", error::bad_alloc);
check("beast.http", error::bad_line_ending); check("beast.http", error::bad_line_ending);
check("beast.http", error::bad_method); check("beast.http", error::bad_method);