]> git.ipfire.org Git - thirdparty/pdns.git/blobdiff - pdns/dns_random.cc
Merge pull request #8223 from PowerDNS/omoerbeek-patch-1
[thirdparty/pdns.git] / pdns / dns_random.cc
index bb6e8e615dd900664ecb844cbaf457369ec00869..48b910c8f4f85d7c2a561f664de3c52fc37dd922 100644 (file)
@@ -281,11 +281,25 @@ uint32_t dns_random(uint32_t upper_bound) {
 #endif
   case RNG_URANDOM: {
       uint32_t num = 0;
+      size_t attempts = 5;
       do {
-        if (read(urandom_fd, &num, sizeof(num)) < 0) {
+        ssize_t got = read(urandom_fd, &num, sizeof(num));
+        if (got < 0) {
+          if (errno == EINTR) {
+            continue;
+          }
+
           (void)close(urandom_fd);
           throw std::runtime_error("Cannot read random device");
         }
+        else if (static_cast<size_t>(got) != sizeof(num)) {
+          /* short read, let's retry */
+          if (attempts == 0) {
+            throw std::runtime_error("Too many short reads on random device");
+          }
+          attempts--;
+          continue;
+        }
       }
       while(num < min);