From 57eb4882977e4d2d142b7c2ee00d3699064250e5 Mon Sep 17 00:00:00 2001 From: Remi Gacogne Date: Fri, 11 Oct 2019 16:44:25 +0200 Subject: [PATCH] dnsdist: Use std::max() to compute the size of the incoming buffer --- pdns/dnsdist-tcp.cc | 2 +- pdns/dnsdistdist/doh.cc | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/pdns/dnsdist-tcp.cc b/pdns/dnsdist-tcp.cc index bea6c584b..3c1b7459f 100644 --- a/pdns/dnsdist-tcp.cc +++ b/pdns/dnsdist-tcp.cc @@ -1154,7 +1154,7 @@ static void handleIO(std::shared_ptr& 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(512)) < s_maxPacketCacheEntrySize ? s_maxPacketCacheEntrySize : (state->d_querySize + static_cast(512))); + state->d_buffer.resize(std::max(state->d_querySize + static_cast(512), s_maxPacketCacheEntrySize)); state->d_currentPos = 0; } } diff --git a/pdns/dnsdistdist/doh.cc b/pdns/dnsdistdist/doh.cc index 76647dac2..03ee4bf58 100644 --- a/pdns/dnsdistdist/doh.cc +++ b/pdns/dnsdistdist/doh.cc @@ -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(du->query.c_str()); struct dnsheader* dh = reinterpret_cast(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; -- 2.40.0