From 872f516edec86cf1c60d411203ac30b1c180f52e Mon Sep 17 00:00:00 2001 From: arenevier Date: Fri, 13 Oct 2023 19:55:26 -0700 Subject: [PATCH] allow building when cpp exceptions are disabled (#489) IXWebSocket needs exceptions support because of the use of stoi. In order to build when cpp exceptions are disabled, we can use strtol. --- ixwebsocket/IXHttp.cpp | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/ixwebsocket/IXHttp.cpp b/ixwebsocket/IXHttp.cpp index 46504026..19f17fbc 100644 --- a/ixwebsocket/IXHttp.cpp +++ b/ixwebsocket/IXHttp.cpp @@ -133,16 +133,20 @@ namespace ix if (headers.find("Content-Length") != headers.end()) { int contentLength = 0; - try { - contentLength = std::stoi(headers["Content-Length"]); + const char* p = headers["Content-Length"].c_str(); + char* p_end{}; + errno = 0; + long val = std::strtol(p, &p_end, 10); + if (p_end == p // invalid argument + || errno == ERANGE // out of range + || val < std::numeric_limits::min() + || val > std::numeric_limits::max()) { + return std::make_tuple( + false, "Error parsing HTTP Header 'Content-Length'", httpRequest); + } + contentLength = val; } - catch (const std::exception&) - { - return std::make_tuple( - false, "Error parsing HTTP Header 'Content-Length'", httpRequest); - } - if (contentLength < 0) { return std::make_tuple(