Commit cf87c78c authored by Mathieu STEFANI's avatar Mathieu STEFANI

Fixed the MAX_SIZE macro that was incorrectly calculating the maximum remaining size

Also used the macro when checking if the subtype belongs to the vendor
tree.
parent 326dcc29
...@@ -66,10 +66,17 @@ MediaType::parseRaw(const char* str, size_t len) { ...@@ -66,10 +66,17 @@ MediaType::parseRaw(const char* str, size_t len) {
}; };
auto raise = [&](const char* str) { auto raise = [&](const char* str) {
// TODO: eventually, we should throw a more generic exception
// that could then be catched in lower stack frames to rethrow
// an HttpError
throw HttpError(Http::Code::Unsupported_Media_Type, str); throw HttpError(Http::Code::Unsupported_Media_Type, str);
}; };
#define MAX_SIZE(s) std::min(sizeof(s) - 1, offset(p)) // Macro to ensure that we do not overflow when comparing strings
// The trick here is to use sizeof on a raw string literal of type
// const char[N] instead of strlen to avoid additional
// runtime computation
#define MAX_SIZE(s) std::min(sizeof(s) - 1, len - offset(p))
// Parse type // Parse type
const char *p = strchr(str, '/'); const char *p = strchr(str, '/');
...@@ -97,7 +104,7 @@ MediaType::parseRaw(const char* str, size_t len) { ...@@ -97,7 +104,7 @@ MediaType::parseRaw(const char* str, size_t len) {
} }
MIME_TYPES MIME_TYPES
#undef TYPE #undef TYPE
throw HttpError(Http::Code::Unsupported_Media_Type, "Unknown Media Type"); raise("Unknown Media Type");
} while (0); } while (0);
top_ = top; top_ = top;
...@@ -108,7 +115,7 @@ MediaType::parseRaw(const char* str, size_t len) { ...@@ -108,7 +115,7 @@ MediaType::parseRaw(const char* str, size_t len) {
if (eof(p)) raise("Malformed Media Type"); if (eof(p)) raise("Malformed Media Type");
if (memcmp(p, "vnd.", 4) == 0) { if (memcmp(p, "vnd.", MAX_SIZE("vnd.")) == 0) {
sub = Subtype::Vendor; sub = Subtype::Vendor;
} else { } else {
do { do {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment