You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ab...@apache.org on 2025/01/10 15:52:42 UTC

(httpcomponents-core) branch master updated: Handle overflow in Deadline calculation to ensure correct behavior for large time values. (#512)

This is an automated email from the ASF dual-hosted git repository.

abernal pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/httpcomponents-core.git


The following commit(s) were added to refs/heads/master by this push:
     new fd5c61742 Handle overflow in Deadline calculation to ensure correct behavior for large time values. (#512)
fd5c61742 is described below

commit fd5c61742090834da464dae5c2307cc8b86ba199
Author: Arturo Bernal <ab...@apache.org>
AuthorDate: Fri Jan 10 16:52:36 2025 +0100

    Handle overflow in Deadline calculation to ensure correct behavior for large time values. (#512)
---
 httpcore5/src/main/java/org/apache/hc/core5/util/Deadline.java |  5 ++---
 .../src/test/java/org/apache/hc/core5/util/TestDeadline.java   | 10 ++++++++++
 2 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/httpcore5/src/main/java/org/apache/hc/core5/util/Deadline.java b/httpcore5/src/main/java/org/apache/hc/core5/util/Deadline.java
index 8d5b70c7b..29c493f06 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/util/Deadline.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/util/Deadline.java
@@ -83,9 +83,8 @@ public class Deadline {
      */
     public static Deadline calculate(final long timeMillis, final TimeValue timeValue) {
         if (TimeValue.isPositive(timeValue)) {
-            // TODO handle unlikely overflow
-            final long deadline = timeMillis + timeValue.toMilliseconds();
-            return deadline < 0 ? Deadline.MAX_VALUE : Deadline.fromUnixMilliseconds(deadline);
+            return Deadline.fromUnixMilliseconds(timeMillis +
+                    Math.min(timeValue.toMilliseconds(), Long.MAX_VALUE - timeMillis));
         }
         return Deadline.MAX_VALUE;
     }
diff --git a/httpcore5/src/test/java/org/apache/hc/core5/util/TestDeadline.java b/httpcore5/src/test/java/org/apache/hc/core5/util/TestDeadline.java
index 9677ad107..d4e6b4784 100644
--- a/httpcore5/src/test/java/org/apache/hc/core5/util/TestDeadline.java
+++ b/httpcore5/src/test/java/org/apache/hc/core5/util/TestDeadline.java
@@ -130,4 +130,14 @@ class TestDeadline {
         final Deadline deadline = Deadline.fromUnixMilliseconds(nowPlusOneMin);
         Assertions.assertEquals(nowPlusOneMin, deadline.getValue());
     }
+
+    @Test
+    void testOverflowHandling() {
+        final long currentTime = Long.MAX_VALUE - 5000; // Simulate close to overflow
+        final TimeValue tenSeconds = TimeValue.ofMilliseconds(10000); // 10 seconds
+        final Deadline deadline = Deadline.calculate(currentTime, tenSeconds);
+
+        Assertions.assertEquals(Deadline.MAX_VALUE, deadline,
+                "Overflow should result in the maximum deadline value.");
+    }
 }