From: Otto Moerbeek Date: Fri, 30 Oct 2020 09:10:43 +0000 (+0100) Subject: Fix reserve botch, as noted by @rgacogne X-Git-Tag: dnsdist-1.6.0-alpha0~11^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f99748e209d0639b344eacf07688872cc9e2cb80;p=thirdparty%2Fpdns.git Fix reserve botch, as noted by @rgacogne --- diff --git a/pdns/protozero.hh b/pdns/protozero.hh index f257b03882..7070b43e74 100644 --- a/pdns/protozero.hh +++ b/pdns/protozero.hh @@ -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 {