]> git.ipfire.org Git - thirdparty/snapper.git/commitdiff
- minor code rearrangement 1159/head
authorArvin Schnell <aschnell@suse.de>
Wed, 24 Jun 2026 05:44:22 +0000 (07:44 +0200)
committerArvin Schnell <aschnell@suse.de>
Wed, 24 Jun 2026 05:44:22 +0000 (07:44 +0200)
stomp/Stomp.cc
stomp/testsuite/read1.cc

index cf9ef0e3c957e656dc49e9b4053ddc9481099a4f..a10576e10ecac0f68502bb03ba18717f1ba7bf74 100644 (file)
@@ -21,6 +21,7 @@
 
 
 #include <regex>
+#include <limits>
 
 #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<ssize_t>::max())
+           {
+               throw runtime_error("stomp error: content-length value out of range '" + str + "'");
+           }
+
+           return static_cast<ssize_t>(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<ssize_t>(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;
index 5124e17e066633e55c8e0630ed98e53e79f2db13..a6720a57cbcf65de8e63e45787cbbc47b05672c6 100644 (file)
@@ -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;
+    });
+}