Skip to content
This repository has been archived by the owner on Feb 8, 2024. It is now read-only.

Commit

Permalink
Added gaurd against long overflow in linear
Browse files Browse the repository at this point in the history
Linear backoff algorithm could result in long overflow
causing a negative number to be returned that would cause the
application to crash.
  • Loading branch information
TomKimsey committed May 25, 2021
1 parent 6860cbb commit 90fd480
Showing 1 changed file with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,20 @@ public abstract class AutomaticRetryCodeLinear extends AutomaticRetryCode {
* @return number of milliseconds delay before specified retry number
*/
protected long getDelayMillisBeforeRetry(int retry) {
long delayMillisBeforeRetry = retry * getLinearSlopeMillis();

// Check if above maximum delay, set to maximum if larger
if (delayMillisBeforeRetry > getMaxDelayMillisBeforeRetry()) {
delayMillisBeforeRetry = getMaxDelayMillisBeforeRetry();
long delayMillisBeforeRetry = 0;

// Check that delayMillisBeforeRetry will not overflow long
if ((Long.MAX_VALUE / getLinearSlopeMillis()) > retry) {
delayMillisBeforeRetry = retry * getLinearSlopeMillis();

// Check if above maximum delay, set to maximum if larger
if (delayMillisBeforeRetry > getMaxDelayMillisBeforeRetry()) {
delayMillisBeforeRetry = getMaxDelayMillisBeforeRetry();
}
} else {
// If retry number is large enough to cause long overflow, set to max long.
delayMillisBeforeRetry = Long.MAX_VALUE;
}

return delayMillisBeforeRetry;
Expand Down

0 comments on commit 90fd480

Please sign in to comment.