mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2025-05-18 02:38:27 +00:00
Do not throw exceptions when parsing request chunks (#441)
detail::read_content_chunked was using std::stoul to parse the hexadecimal chunk lengths for "Transfer-Encoding: chunked" requests. This throws an exception if the string does not begin with any valid digits. read_content_chunked is not called in the context of a try block so this caused the process to terminate. Rather than use exceptions, I opted for std::stroul, which is similar to std::stoul but does not throw exceptions. Since malformed user input is not particularly exceptional, and some projects are compiled without exception support, this approach seems both more portable and more correct.
This commit is contained in:
parent
e1506fa186
commit
c49441ae64
2 changed files with 46 additions and 8 deletions
13
httplib.h
13
httplib.h
|
@ -1893,9 +1893,16 @@ inline bool read_content_chunked(Stream &strm, ContentReceiver out) {
|
|||
|
||||
if (!line_reader.getline()) { return false; }
|
||||
|
||||
auto chunk_len = std::stoul(line_reader.ptr(), 0, 16);
|
||||
unsigned long chunk_len;
|
||||
while (true) {
|
||||
char *end_ptr;
|
||||
|
||||
chunk_len = std::strtoul(line_reader.ptr(), &end_ptr, 16);
|
||||
|
||||
if (end_ptr == line_reader.ptr()) { return false; }
|
||||
|
||||
if (chunk_len == 0) { break; }
|
||||
|
||||
while (chunk_len > 0) {
|
||||
if (!read_content_with_length(strm, chunk_len, nullptr, out)) {
|
||||
return false;
|
||||
}
|
||||
|
@ -1905,8 +1912,6 @@ inline bool read_content_chunked(Stream &strm, ContentReceiver out) {
|
|||
if (strcmp(line_reader.ptr(), "\r\n")) { break; }
|
||||
|
||||
if (!line_reader.getline()) { return false; }
|
||||
|
||||
chunk_len = std::stoul(line_reader.ptr(), 0, 16);
|
||||
}
|
||||
|
||||
if (chunk_len == 0) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue