Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle overflow in Deadline calculation to ensure correct behavior for large time values. #512

Merged
merged 1 commit into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
10 changes: 10 additions & 0 deletions httpcore5/src/test/java/org/apache/hc/core5/util/TestDeadline.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,14 @@ void testValue() {
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.");
}
}
Loading