Skip to content

Commit

Permalink
HashedWheelTimer.newTimeout(...) may overflow
Browse files Browse the repository at this point in the history
Motivation:

We dont protect from overflow and so the timer may fire too early if a large timeout is used.

Modifications:

Add overflow guard and a test.

Result:

Fixes netty#7760.
  • Loading branch information
normanmaurer committed Mar 3, 2018
1 parent 0a8e1aa commit 48df2f6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
5 changes: 5 additions & 0 deletions common/src/main/java/io/netty/util/HashedWheelTimer.java
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,11 @@ public Timeout newTimeout(TimerTask task, long delay, TimeUnit unit) {
// Add the timeout to the timeout queue which will be processed on the next tick.
// During processing all the queued HashedWheelTimeouts will be added to the correct HashedWheelBucket.
long deadline = System.nanoTime() + unit.toNanos(delay) - startTime;

// Guard against overflow.
if (delay > 0 && deadline < 0) {
deadline = Long.MAX_VALUE;
}
HashedWheelTimeout timeout = new HashedWheelTimeout(this, task, deadline);
timeouts.add(timeout);
return timeout;
Expand Down
15 changes: 15 additions & 0 deletions common/src/test/java/io/netty/util/HashedWheelTimerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,21 @@ public void reportPendingTimeouts() throws InterruptedException {
timer.stop();
}

@Test
public void testOverflow() throws InterruptedException {
final HashedWheelTimer timer = new HashedWheelTimer();
final CountDownLatch latch = new CountDownLatch(1);
Timeout timeout = timer.newTimeout(new TimerTask() {
@Override
public void run(Timeout timeout) {
latch.countDown();
}
}, Long.MAX_VALUE, TimeUnit.MILLISECONDS);
assertFalse(latch.await(1, TimeUnit.SECONDS));
timeout.cancel();
timer.stop();
}

private static TimerTask createNoOpTimerTask() {
return new TimerTask() {
@Override
Expand Down

0 comments on commit 48df2f6

Please sign in to comment.