]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
Fix reserve botch, as noted by @rgacogne
authorOtto Moerbeek <otto.moerbeek@open-xchange.com>
Fri, 30 Oct 2020 09:10:43 +0000 (10:10 +0100)
committerOtto Moerbeek <otto@drijf.net>
Tue, 10 Nov 2020 08:17:13 +0000 (09:17 +0100)
pdns/protozero.hh

index f257b03882797a38630e5ce8a14fa6e21cdcd1de..7070b43e744ec9264458060a1bc65e8c1cc1464f 100644 (file)
@@ -37,10 +37,14 @@ namespace pdns {
       // Start a new messagebuf, containing separate data for the response part
       Message(std::string::size_type sz1, std::string::size_type sz2) : d_message{d_msgbuf}, d_response{d_rspbuf}
       {
-        // This is extra space in addition to what's already there
-        // Different from what string.reserve() does
-        d_message.reserve(sz1);
-        d_response.reserve(sz2);
+        if (d_msgbuf.capacity() < d_msgbuf.size() + sz1) {
+          // This is extra space in addition to what's already there
+          // Different from what string.reserve() does.
+          d_message.reserve(sz1);
+        }
+        if (d_rspbuf.capacity() < d_rspbuf.size() + sz2) {
+          d_response.reserve(sz2);
+        }
       }
 
       // Construct a Message with (partially) constructed content
@@ -50,8 +54,13 @@ namespace pdns {
         // We expect to grow the buffers, in the end the d_message will contains the (grown) d_response
         // This is extra space in addition to what's already there
         // Different from what string.reserve() does
-        d_message.reserve(sz1 + d_rspbuf.length() + sz2);
-        d_response.reserve(sz2);
+        std::string::size_type extra = sz1 + d_rspbuf.length() + sz2;
+        if (d_msgbuf.capacity() < d_msgbuf.size() + extra) {
+          d_message.reserve(extra);
+        }
+        if (d_rspbuf.capacity() < d_rspbuf.size() + sz2) {
+          d_response.reserve(sz2);
+        }
       }
       const std::string& getMessageBuf() const
       {