]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
Be more careful saving errno in makeClientSocket() and closesocket() 12392/head
authorOtto Moerbeek <otto.moerbeek@open-xchange.com>
Fri, 6 Jan 2023 14:45:46 +0000 (15:45 +0100)
committerOtto Moerbeek <otto.moerbeek@open-xchange.com>
Fri, 6 Jan 2023 14:45:46 +0000 (15:45 +0100)
pdns/misc.cc
pdns/recursordist/pdns_recursor.cc

index 7e5cb070636ce1d2d5d21ef1cb74d1c47b93effb..cdf86a7d6040deeac7f795ac3451d60a57b2fe44 100644 (file)
@@ -984,13 +984,16 @@ bool setReceiveSocketErrors(int sock, int af)
 }
 
 // Closes a socket.
-int closesocket( int socket )
+int closesocket(int socket)
 {
-  int ret=::close(socket);
-  if(ret < 0 && errno == ECONNRESET) // see ticket 192, odd BSD behaviour
+  int ret = ::close(socket);
+  if(ret < 0 && errno == ECONNRESET) // see ticket 192, odd BSD behaviour
     return 0;
-  if(ret < 0)
-    throw PDNSException("Error closing socket: "+stringerror());
+  }
+  if (ret < 0) {
+    int err = errno;
+    throw PDNSException("Error closing socket: " + stringerror(err));
+  }
   return ret;
 }
 
index 6e9c4f9ac193e5d1744228f08cc49cf57737c68b..2bc87299587cf4c577a5712990c602a919ca329b 100644 (file)
@@ -154,7 +154,8 @@ int UDPClientSocks::makeClientSocket(int family)
     return ret;
   }
   if (ret < 0) {
-    throw PDNSException("Making a socket for resolver (family = " + std::to_string(family) + "): " + stringerror());
+    int err = errno;
+    throw PDNSException("Making a socket for resolver (family = " + std::to_string(family) + "): " + stringerror(err));
   }
 
   // The loop below runs the body with [tries-1 tries-2 ... 1]. Last iteration with tries == 1 is special: it uses a kernel
@@ -165,7 +166,7 @@ int UDPClientSocks::makeClientSocket(int family)
   int tries = 2; // hit the reliable kernel random case for OpenBSD immediately (because it will match tries==1 below), using sysctl net.inet.udp.baddynamic to exclude ports
 #endif
   ComboAddress sin;
-  while (--tries) {
+  while (--tries != 0) {
     in_port_t port;
 
     if (tries == 1) { // last iteration: fall back to kernel 'random'
@@ -174,17 +175,20 @@ int UDPClientSocks::makeClientSocket(int family)
     else {
       do {
         port = g_minUdpSourcePort + dns_random(g_maxUdpSourcePort - g_minUdpSourcePort + 1);
-      } while (g_avoidUdpSourcePorts.count(port));
+      } while (g_avoidUdpSourcePorts.count(port) != 0);
     }
 
     sin = pdns::getQueryLocalAddress(family, port); // does htons for us
-    if (::bind(ret, reinterpret_cast<struct sockaddr*>(&sin), sin.getSocklen()) >= 0)
+    if (::bind(ret, reinterpret_cast<struct sockaddr*>(&sin), sin.getSocklen()) >= 0) {
       break;
+    }
   }
 
-  if (!tries) {
+  int err = errno;
+
+  if (tries == 0) {
     closesocket(ret);
-    throw PDNSException("Resolver binding to local query client socket on " + sin.toString() + ": " + stringerror());
+    throw PDNSException("Resolver binding to local query client socket on " + sin.toString() + ": " + stringerror(err));
   }
 
   try {