Skip to content

Commit

Permalink
timers: fix arbitrary object clearImmediate errors
Browse files Browse the repository at this point in the history
Fix errors that are caused by invoking clearImmediate
with arbitrary objects.

fixes: #37806

PR-URL: #37824
Fixes: #37806
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Antoine du Hamel <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
  • Loading branch information
Linkgoron authored and targos committed May 1, 2021
1 parent 9f1f215 commit e4d1042
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/internal/timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,11 +275,11 @@ ImmediateList.prototype.append = function(item) {
// Removes an item from the linked list, adjusting the pointers of adjacent
// items and the linked list's head or tail pointers as necessary
ImmediateList.prototype.remove = function(item) {
if (item._idleNext !== null) {
if (item._idleNext) {
item._idleNext._idlePrev = item._idlePrev;
}

if (item._idlePrev !== null) {
if (item._idlePrev) {
item._idlePrev._idleNext = item._idleNext;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ function clearImmediate(immediate) {
toggleImmediateRef(false);
immediate[kRefed] = null;

if (destroyHooksExist()) {
if (destroyHooksExist() && immediate[async_id_symbol] !== undefined) {
emitDestroy(immediate[async_id_symbol]);
}

Expand Down
12 changes: 12 additions & 0 deletions test/parallel/test-repl-clear-immediate-crash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';
const common = require('../common');
const child_process = require('child_process');
const assert = require('assert');

// Regression test for https://github.com/nodejs/node/issues/37806:
const proc = child_process.spawn(process.execPath, ['-i']);
proc.on('error', common.mustNotCall());
proc.on('exit', common.mustCall((code) => {
assert.strictEqual(code, 0);
}));
proc.stdin.write('clearImmediate({});\n.exit\n');
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';
require('../common');

// This test makes sure clearing timers with
// objects doesn't throw
clearImmediate({});
clearTimeout({});
clearInterval({});

0 comments on commit e4d1042

Please sign in to comment.