Skip to content

Commit

Permalink
timers: be more defensive with intervals
Browse files Browse the repository at this point in the history
It's possible for user-code to flip an existing timeout to
be an interval during its execution, in which case the
current code would crash due to start being undefined. Fix
this by providing a default start value within rearm.

PR-URL: #18579
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
  • Loading branch information
apapirovski committed Feb 8, 2018
1 parent 1b05d7b commit 568b6a5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
3 changes: 1 addition & 2 deletions lib/timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -442,8 +442,7 @@ function ontimeout(timer, start) {
rearm(timer, start);
}


function rearm(timer, start) {
function rearm(timer, start = TimerWrap.now()) {
// // Do not re-arm unenroll'd or closed timers.
if (timer._idleTimeout === -1) return;

Expand Down
12 changes: 12 additions & 0 deletions test/parallel/test-timers-timeout-to-interval.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';
const common = require('../common');

// This isn't officially supported but nonetheless is something that is
// currently possible and as such it shouldn't cause the process to crash

const t = setTimeout(common.mustCall(() => {
if (t._repeat) {
clearInterval(t);
}
t._repeat = true;
}, 2), 1);

0 comments on commit 568b6a5

Please sign in to comment.