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

Conversation

arturobernalg
Copy link
Member

Fixes overflow handling in Deadline calculation to ensure proper behavior when adding large time values, returning Deadline.MAX_VALUE in case of overflow.

@@ -83,8 +83,11 @@ 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();
final long timeToAdd = timeValue.toMilliseconds();
Copy link
Member

@ok2c ok2c Jan 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@arturobernalg Should not this do the same, just with a bit less code? I also think timeToAdd > 0 and deadline < 0 are unnecessary.

        if (TimeValue.isPositive(timeValue)) {
            return Deadline.fromUnixMilliseconds(timeMillis +
                    Math.min(timeValue.toMilliseconds(), Long.MAX_VALUE - timeMillis));
        }
        return Deadline.MAX_VALUE;

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ok2c Updated the implementation as suggested. Thanks for the feedback!

@arturobernalg arturobernalg merged commit fd5c617 into apache:master Jan 10, 2025
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants