From: Arvin Schnell Date: Wed, 24 Jun 2026 05:44:22 +0000 (+0200) Subject: - minor code rearrangement X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=d5f007df6d31619100233b83bced9d38fbd9b6f0;p=thirdparty%2Fsnapper.git - minor code rearrangement --- diff --git a/stomp/Stomp.cc b/stomp/Stomp.cc index cf9ef0e3..a10576e1 100644 --- a/stomp/Stomp.cc +++ b/stomp/Stomp.cc @@ -21,6 +21,7 @@ #include +#include #include "Stomp.h" @@ -31,6 +32,38 @@ namespace Stomp using namespace std; + ssize_t + parse_content_length(const string& str) + { + try + { + size_t pos = 0; + long long ret = stoll(str, &pos); + + // Check if there are trailing unparsed characters (e.g., "100abc") + if (pos < str.size()) + { + throw runtime_error("stomp error: invalid content-length value '" + str + "'"); + } + + if (ret < 0 || ret > std::numeric_limits::max()) + { + throw runtime_error("stomp error: content-length value out of range '" + str + "'"); + } + + return static_cast(ret); + } + catch (const invalid_argument&) + { + throw runtime_error("stomp error: invalid content-length syntax '" + str + "'"); + } + catch (const out_of_range&) + { + throw runtime_error("stomp error: content-length value out of range'" + str + "'"); + } + } + + Message read_message(istream& is) { @@ -106,30 +139,7 @@ namespace Stomp if (key == "content-length") { has_content_length = true; - - try - { - size_t parsed_chars = 0; - long long parsed_length = stoll(value, &parsed_chars); - - // 1. Check if there are trailing unparsed characters (e.g., "100abc") - // 2. Reject negative integer limits - // 3. Explicitly reject negative signs to enforce pure digits - if (parsed_chars < value.size() || parsed_length < 0 || value[0] == '-') - { - throw runtime_error("stomp error: invalid content-length value '" + value + "'"); - } - - content_length = static_cast(parsed_length); - } - catch (const invalid_argument&) - { - throw runtime_error("stomp error: invalid content-length syntax '" + value + "'"); - } - catch (const out_of_range&) - { - throw runtime_error("stomp error: content-length value out of range"); - } + content_length = parse_content_length(value); } msg.headers[key] = value; diff --git a/stomp/testsuite/read1.cc b/stomp/testsuite/read1.cc index 5124e17e..a6720a57 100644 --- a/stomp/testsuite/read1.cc +++ b/stomp/testsuite/read1.cc @@ -130,3 +130,16 @@ BOOST_AUTO_TEST_CASE(error3) return strcmp(e.what(), "stomp error: invalid content-length value '5a'") == 0; }); } + + +BOOST_AUTO_TEST_CASE(error4) +{ + // invalid negative content-lenght value + + istringstream s1("HELLO\nkey:value\ncontent-length:-5\n\nWORLD" + null); + istream s2(s1.rdbuf()); + + BOOST_CHECK_EXCEPTION(read_message(s2), exception, [](const exception& e) { + return strcmp(e.what(), "stomp error: content-length value out of range '-5'") == 0; + }); +}