From 07ed076499c820738b95601a832aeea5e22f3949 Mon Sep 17 00:00:00 2001 From: Manny Date: Fri, 12 Apr 2019 12:16:32 +0200 Subject: [PATCH 1/2] BUGFIX: crash when "content-length" > max_int --- httplib.h | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 8967012..aae6e1c 100644 --- a/httplib.h +++ b/httplib.h @@ -788,6 +788,15 @@ inline int get_header_value_int(const Headers &headers, const char *key, return def; } +inline unsigned long long get_header_value_uint64(const Headers &headers, const char *key, + int def = 0) { + auto it = headers.find(key); + if (it != headers.end()) { + return std::strtoull(it->second.data(), nullptr, 10); + } + return def; +} + inline bool read_headers(Stream &strm, Headers &headers) { static std::regex re(R"((.+?):\s*(.+?)\s*\r\n)"); @@ -881,7 +890,7 @@ inline bool read_content_chunked(Stream &strm, std::string &out) { template bool read_content(Stream &strm, T &x, Progress progress = Progress()) { if (has_header(x.headers, "Content-Length")) { - auto len = get_header_value_int(x.headers, "Content-Length", 0); + auto len = get_header_value_uint64(x.headers, "Content-Length", 0); if (len == 0) { const auto &encoding = get_header_value(x.headers, "Transfer-Encoding", 0, ""); From 8af85019dc7d326f85e855e8c31416b1c0dd79c5 Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 12 Apr 2019 17:22:39 -0400 Subject: [PATCH 2/2] Removed get_header_value_int and use uint64_t for the return value of get_header_value_uint64 --- httplib.h | 9 +-------- test/test.cc | 10 +++++----- 2 files changed, 6 insertions(+), 13 deletions(-) diff --git a/httplib.h b/httplib.h index aae6e1c..99ca369 100644 --- a/httplib.h +++ b/httplib.h @@ -781,14 +781,7 @@ inline const char *get_header_value(const Headers &headers, const char *key, return def; } -inline int get_header_value_int(const Headers &headers, const char *key, - int def = 0) { - auto it = headers.find(key); - if (it != headers.end()) { return std::stoi(it->second); } - return def; -} - -inline unsigned long long get_header_value_uint64(const Headers &headers, const char *key, +inline uint64_t get_header_value_uint64(const Headers &headers, const char *key, int def = 0) { auto it = headers.find(key); if (it != headers.end()) { diff --git a/test/test.cc b/test/test.cc index 0143e62..deb6f6d 100644 --- a/test/test.cc +++ b/test/test.cc @@ -72,8 +72,8 @@ TEST(GetHeaderValueTest, DefaultValue) { TEST(GetHeaderValueTest, DefaultValueInt) { Headers headers = {{"Dummy", "Dummy"}}; - auto val = detail::get_header_value_int(headers, "Content-Length", 100); - EXPECT_EQ(100, val); + auto val = detail::get_header_value_uint64(headers, "Content-Length", 100); + EXPECT_EQ(100ull, val); } TEST(GetHeaderValueTest, RegularValue) { @@ -84,8 +84,8 @@ TEST(GetHeaderValueTest, RegularValue) { TEST(GetHeaderValueTest, RegularValueInt) { Headers headers = {{"Content-Length", "100"}, {"Dummy", "Dummy"}}; - auto val = detail::get_header_value_int(headers, "Content-Length", 0); - EXPECT_EQ(100, val); + auto val = detail::get_header_value_uint64(headers, "Content-Length", 0); + EXPECT_EQ(100ull, val); } TEST(GetHeaderValueTest, Range) { @@ -474,7 +474,7 @@ protected: EXPECT_EQ("value3", req.get_param_value("array", 2)); }) .Post("/validate-no-multiple-headers", - [&](const Request &req, Response &res) { + [&](const Request &req, Response & /*res*/) { EXPECT_EQ(1u, req.get_header_value_count("Content-Length")); EXPECT_EQ("5", req.get_header_value("Content-Length")); })