From: Otto Moerbeek Date: Tue, 2 Jul 2024 10:02:52 +0000 (+0200) Subject: Also convert callers of readWithTimeout(), as suggested by @rgacogne X-Git-Tag: rec-5.2.0-alpha1~207^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=refs%2Fpull%2F14396%2Fhead;p=thirdparty%2Fpdns.git Also convert callers of readWithTimeout(), as suggested by @rgacogne --- diff --git a/modules/remotebackend/httpconnector.cc b/modules/remotebackend/httpconnector.cc index 38950b15da..acff9dfb7c 100644 --- a/modules/remotebackend/httpconnector.cc +++ b/modules/remotebackend/httpconnector.cc @@ -431,23 +431,19 @@ int HTTPConnector::recv_message(Json& output) if (d_socket == nullptr) { return -1; // cannot receive :( } - char buffer[4096]; - int rd = -1; - time_t t0 = 0; + std::array buffer{}; + time_t time0 = 0; arl.initialize(&resp); try { - t0 = time((time_t*)nullptr); - while (!arl.ready() && (labs(time((time_t*)nullptr) - t0) <= timeout)) { - rd = d_socket->readWithTimeout(buffer, sizeof(buffer), timeout); - if (rd == 0) { + time0 = time(nullptr); + while (!arl.ready() && (labs(time(nullptr) - time0) <= timeout)) { + auto readBytes = d_socket->readWithTimeout(buffer.data(), buffer.size(), timeout); + if (readBytes == 0) { throw NetworkError("EOF while reading"); } - if (rd < 0) { - throw NetworkError(std::string(strerror(rd))); - } - arl.feed(std::string(buffer, rd)); + arl.feed(std::string(buffer.data(), readBytes)); } // timeout occurred. if (!arl.ready()) { @@ -470,13 +466,12 @@ int HTTPConnector::recv_message(Json& output) throw PDNSException("Received unacceptable HTTP status code " + std::to_string(resp.status) + " from HTTP endpoint " + d_addr.toStringWithPort()); } - int rv = -1; std::string err; output = Json::parse(resp.body, err); if (output != nullptr) { - return resp.body.size(); + return static_cast(resp.body.size()); } g_log << Logger::Error << "Cannot parse JSON reply: " << err << endl; - return rv; + return -1; } diff --git a/pdns/ixfrutils.cc b/pdns/ixfrutils.cc index 40b6b2495d..385ac490b0 100644 --- a/pdns/ixfrutils.cc +++ b/pdns/ixfrutils.cc @@ -53,8 +53,8 @@ uint32_t getSerialFromPrimary(const ComboAddress& primary, const DNSName& zone, string reply; reply.resize(4096); // will throw a NetworkError on timeout - ssize_t got = s.readWithTimeout(&reply[0], reply.size(), timeout); - if (got < 0 || static_cast(got) < sizeof(dnsheader)) { + size_t got = s.readWithTimeout(reply.data(), reply.size(), timeout); + if (got < sizeof(dnsheader)) { throw std::runtime_error("Invalid response size " + std::to_string(got)); } diff --git a/pdns/webserver.cc b/pdns/webserver.cc index 754484558a..8a257c66fe 100644 --- a/pdns/webserver.cc +++ b/pdns/webserver.cc @@ -525,11 +525,10 @@ void WebServer::serveConnection(const std::shared_ptr& client) const { try { while(!req.complete) { - int bytes; - char buf[16000]; - bytes = client->readWithTimeout(buf, sizeof(buf), timeout); + std::array buf{}; + auto bytes = client->readWithTimeout(buf.data(), buf.size(), timeout); if (bytes > 0) { - string data = string(buf, bytes); + string data = string(buf.data(), bytes); req.complete = yarl.feed(data); } else { // read error OR EOF