Skip to content
This repository was archived by the owner on Apr 22, 2023. It is now read-only.

Commit 0c47219

Browse files
committed
timers: fix handling of large timeouts
Don't use the double-negate trick to coalesce the timeout argument into a number, it produces the wrong result for very large timeouts. Example: setTimeout(cb, 1e10); // doesn't work, ~~1e10 == 1410065408
1 parent 9126dd2 commit 0c47219

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

lib/timers.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,9 @@ exports.active = function(item) {
170170
exports.setTimeout = function(callback, after) {
171171
var timer;
172172

173-
after = ~~after;
174-
if (after < 1 || after > TIMEOUT_MAX) {
173+
after *= 1; // coalesce to number or NaN
174+
175+
if (!(after >= 1 && after <= TIMEOUT_MAX)) {
175176
after = 1; // schedule on next tick, follows browser behaviour
176177
}
177178

@@ -222,8 +223,9 @@ exports.setInterval = function(callback, repeat) {
222223

223224
if (process.domain) timer.domain = process.domain;
224225

225-
repeat = ~~repeat;
226-
if (repeat < 1 || repeat > TIMEOUT_MAX) {
226+
repeat *= 1; // coalesce to number or NaN
227+
228+
if (!(repeat >= 1 && repeat <= TIMEOUT_MAX)) {
227229
repeat = 1; // schedule on next tick, follows browser behaviour
228230
}
229231

test/simple/test-timers.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ var inputs = [
4545
1,
4646
1.0,
4747
10,
48-
2147483648 // browser behaviour: timeouts > 2^31-1 run on next tick
48+
2147483648, // browser behaviour: timeouts > 2^31-1 run on next tick
49+
12345678901234 // ditto
4950
];
5051

5152
var timeouts = [];

0 commit comments

Comments
 (0)