]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
delint waitFor2Data 12931/head
authorOtto Moerbeek <otto.moerbeek@open-xchange.com>
Tue, 20 Jun 2023 14:57:21 +0000 (16:57 +0200)
committerOtto Moerbeek <otto.moerbeek@open-xchange.com>
Thu, 6 Jul 2023 13:46:42 +0000 (15:46 +0200)
pdns/misc.cc

index 5c9c81785fa43d31bd5d181f731b548f73cb0e3d..4bbc865f6bed5f0d16d1bab96412442b2c4f1d1b 100644 (file)
@@ -439,35 +439,40 @@ int waitForMultiData(const set<int>& fds, const int seconds, const int useconds,
 }
 
 // returns -1 in case of error, 0 if no data is available, 1 if there is. In the first two cases, errno is set
-int waitFor2Data(int fd1, int fd2, int seconds, int useconds, int*fd)
+int waitFor2Data(int fd1, int fd2, int seconds, int useconds, int* fdPtr)
 {
-  int ret;
-
-  struct pollfd pfds[2];
-  memset(&pfds[0], 0, 2*sizeof(struct pollfd));
+  std::array<pollfd,2> pfds{};
+  memset(pfds.data(), 0, pfds.size() * sizeof(struct pollfd));
   pfds[0].fd = fd1;
   pfds[1].fd = fd2;
 
   pfds[0].events= pfds[1].events = POLLIN;
 
-  int nsocks = 1 + (fd2 >= 0); // fd2 can optionally be -1
+  int nsocks = 1 + static_cast<int>(fd2 >= 0); // fd2 can optionally be -1
 
-  if(seconds >= 0)
-    ret = poll(pfds, nsocks, seconds * 1000 + useconds/1000);
-  else
-    ret = poll(pfds, nsocks, -1);
-  if(!ret || ret < 0)
+  int ret{};
+  if (seconds >= 0) {
+    ret = poll(pfds.data(), nsocks, seconds * 1000 + useconds / 1000);
+  }
+  else {
+    ret = poll(pfds.data(), nsocks, -1);
+  }
+  if (ret <= 0) {
     return ret;
+  }
 
-  if((pfds[0].revents & POLLIN) && !(pfds[1].revents & POLLIN))
-    *fd = pfds[0].fd;
-  else if((pfds[1].revents & POLLIN) && !(pfds[0].revents & POLLIN))
-    *fd = pfds[1].fd;
+  if ((pfds[0].revents & POLLIN) != 0 && (pfds[1].revents & POLLIN) == 0) {
+    *fdPtr = pfds[0].fd;
+  }
+  else if ((pfds[1].revents & POLLIN) != 0 && (pfds[0].revents & POLLIN) == 0) {
+    *fdPtr = pfds[1].fd;
+  }
   else if(ret == 2) {
-    *fd = pfds[dns_random_uint32()%2].fd;
+    *fdPtr = pfds.at(dns_random_uint32() % 2).fd;
+  }
+  else {
+    *fdPtr = -1; // should never happen
   }
-  else
-    *fd = -1; // should never happen
 
   return 1;
 }