* Avoid an overflow when converting the given INT64 timeout in microseconds
  to an INT32 timeout in milliseconds.

PR: 69542
Obtained from: https://github.com/apache/apr/pull/62
Submitted by: [Alejandro Perez <alejandroperezpestana99@gmail.com>]
Reviewed by: rpluem


git-svn-id: https://svn.apache.org/repos/asf/apr/apr/trunk@1929188 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/CHANGES b/CHANGES
index f5b92a6..5031a80 100644
--- a/CHANGES
+++ b/CHANGES
@@ -1,6 +1,9 @@
                                                      -*- coding: utf-8 -*-
 Changes for APR 2.0.0
 
+  *) apr_wait_for_io_or_timeout: Fix integer overflow for larger timeout values.
+     PR 69542 [Alejandro Perez <alejandroperezpestana99@gmail.com>]
+
   *) apr_strings: Add apr_strqtok() function to tokenise quoted strings.
      [Graham Leggett]
 
diff --git a/support/unix/waitio.c b/support/unix/waitio.c
index a3c6a83..ebc18be 100644
--- a/support/unix/waitio.c
+++ b/support/unix/waitio.c
@@ -40,15 +40,22 @@
                                         int for_read)
 {
     struct pollfd pfd;
+    apr_interval_time_t raw_timeout;
     int rc, timeout;
 
-    timeout    = f        ? f->timeout        : s->timeout;
+    raw_timeout = f ? f->timeout : s->timeout;
+    if (raw_timeout > ((apr_interval_time_t)INT_MAX) * 1000) {
+        /* timeout value exceeds maximum allowed (~25 days in microseconds)
+         * capping to INT_MAX milliseconds to avoid overflow */
+        timeout = INT_MAX;
+    } else {
+        /* convert microseconds to milliseconds (round up) */
+        timeout = raw_timeout > 0 ? (int)((raw_timeout + 999) / 1000) : (int)raw_timeout;
+    }
+
     pfd.fd     = f        ? f->filedes        : s->socketdes;
     pfd.events = for_read ? POLLIN            : POLLOUT;
 
-    if (timeout > 0) {
-        timeout = (timeout + 999) / 1000;
-    }
     do {
         rc = poll(&pfd, 1, timeout);
     } while (rc == -1 && errno == EINTR);