svn commit: r1929252 - in apr/apr/branches/1.8.x: . support/unix

[email protected] Tue, 21 Oct 2025 15:32:52 -0000
Newsgroups gmane.comp.apache.apr.cvs
Message-ID <176106077224.1275556.15652592769199134836@svn02-us-east.apache.org>
Author: rpluem
Date: Tue Oct 21 15:32:51 2025
New Revision: 1929252

Log:
Merge r1929188 from trunk:

* 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 <[email protected]>]
Reviewed by: rpluem

Modified:
   apr/apr/branches/1.8.x/   (props changed)
   apr/apr/branches/1.8.x/CHANGES
   apr/apr/branches/1.8.x/support/unix/waitio.c

Modified: apr/apr/branches/1.8.x/CHANGES
==============================================================================
--- apr/apr/branches/1.8.x/CHANGES	Tue Oct 21 12:45:03 2025	(r1929251)
+++ apr/apr/branches/1.8.x/CHANGES	Tue Oct 21 15:32:51 2025	(r1929252)
@@ -1,6 +1,9 @@
                                                      -*- coding: utf-8 -*-
 Changes for APR 1.8.0
 
+  *) apr_wait_for_io_or_timeout: Fix integer overflow for larger timeout values.
+     PR 69542 [Alejandro Perez <[email protected]>]
+
   *) testmmap: Avoid a crash after test_file_open() fails. [Graham
      Leggett]
 

Modified: apr/apr/branches/1.8.x/support/unix/waitio.c
==============================================================================
--- apr/apr/branches/1.8.x/support/unix/waitio.c	Tue Oct 21 12:45:03 2025	(r1929251)
+++ apr/apr/branches/1.8.x/support/unix/waitio.c	Tue Oct 21 15:32:51 2025	(r1929252)
@@ -40,15 +40,22 @@ apr_status_t apr_wait_for_io_or_timeout(
                                         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);