]> granicus.if.org Git - pdns/commitdiff
dnsdist: Use std::max() to compute the size of the incoming buffer
authorRemi Gacogne <remi.gacogne@powerdns.com>
Fri, 11 Oct 2019 14:44:25 +0000 (16:44 +0200)
committerRemi Gacogne <remi.gacogne@powerdns.com>
Tue, 15 Oct 2019 08:06:54 +0000 (10:06 +0200)
pdns/dnsdist-tcp.cc
pdns/dnsdistdist/doh.cc

index bea6c584ba24fbdaebe32865cb631b6b739f51e9..3c1b7459f6438c8ec1fa532da955e20e31f5c01b 100644 (file)
@@ -1154,7 +1154,7 @@ static void handleIO(std::shared_ptr<IncomingTCPConnectionState>& state, struct
 
         /* allocate a bit more memory to be able to spoof the content, get an answer from the cache
            or to add ECS without allocating a new buffer */
-        state->d_buffer.resize((state->d_querySize + static_cast<size_t>(512)) < s_maxPacketCacheEntrySize ? s_maxPacketCacheEntrySize : (state->d_querySize + static_cast<size_t>(512)));
+        state->d_buffer.resize(std::max(state->d_querySize + static_cast<size_t>(512), s_maxPacketCacheEntrySize));
         state->d_currentPos = 0;
       }
     }
index 76647dac22ac5975de2f10a06080a1cf063ebf33..03ee4bf58a2c19b06dce855feff293c85e0f03b5 100644 (file)
@@ -306,7 +306,7 @@ static int processDOHQuery(DOHUnit* du)
     uint16_t len = du->query.length();
     /* We reserve at least 512 additional bytes to be able to add EDNS, but we also want
        at least s_maxPacketCacheEntrySize bytes to be able to spoof the content or fill the answer from the packet cache */
-    du->query.resize((du->query.size() + 512 < s_maxPacketCacheEntrySize) ? s_maxPacketCacheEntrySize : (du->query.size() + 512));
+    du->query.resize(std::max(du->query.size() + 512, s_maxPacketCacheEntrySize));
     size_t bufferSize = du->query.size();
     auto query = const_cast<char*>(du->query.c_str());
     struct dnsheader* dh = reinterpret_cast<struct dnsheader*>(query);
@@ -610,7 +610,7 @@ try
     std::string query;
     /* We reserve at least 512 additional bytes to be able to add EDNS, but we also want
        at least s_maxPacketCacheEntrySize bytes to be able to fill the answer from the packet cache */
-    query.reserve(((req->entity.len + 512) < s_maxPacketCacheEntrySize) ? s_maxPacketCacheEntrySize : (req->entity.len + 512));
+    query.reserve(std::max(req->entity.len + 512, s_maxPacketCacheEntrySize));
     query.assign(req->entity.base, req->entity.len);
     doh_dispatch_query(dsc, self, req, std::move(query), local, remote);
   }
@@ -637,8 +637,8 @@ try
       /* rough estimate so we hopefully don't need a need allocation later */
       /* We reserve at least 512 additional bytes to be able to add EDNS, but we also want
          at least s_maxPacketCacheEntrySize bytes to be able to fill the answer from the packet cache */
-      size_t estimate = ((sdns.size() * 3) / 4);
-      decoded.reserve(((estimate + 512) < s_maxPacketCacheEntrySize) ? s_maxPacketCacheEntrySize : (estimate + 512));
+      const size_t estimate = ((sdns.size() * 3) / 4);
+      decoded.reserve(std::max(estimate + 512, s_maxPacketCacheEntrySize));
       if(B64Decode(sdns, decoded) < 0) {
         h2o_send_error_400(req, "Bad Request", "Unable to decode BASE64-URL", 0);
         ++dsc->df->d_badrequests;