Skip to content

Commit d44b268

Browse files
Linkgoronruyadorno
authored andcommitted
timers: fix arbitrary object clearImmediate errors
Fix errors that are caused by invoking clearImmediate with arbitrary objects. fixes: #37806 PR-URL: #37824 Fixes: #37806 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
1 parent ac60d01 commit d44b268

4 files changed

+23
-3
lines changed

lib/internal/timers.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -279,11 +279,11 @@ ImmediateList.prototype.append = function(item) {
279279
// Removes an item from the linked list, adjusting the pointers of adjacent
280280
// items and the linked list's head or tail pointers as necessary
281281
ImmediateList.prototype.remove = function(item) {
282-
if (item._idleNext !== null) {
282+
if (item._idleNext) {
283283
item._idleNext._idlePrev = item._idlePrev;
284284
}
285285

286-
if (item._idlePrev !== null) {
286+
if (item._idlePrev) {
287287
item._idlePrev._idleNext = item._idleNext;
288288
}
289289

lib/timers.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ function clearImmediate(immediate) {
283283
toggleImmediateRef(false);
284284
immediate[kRefed] = null;
285285

286-
if (destroyHooksExist()) {
286+
if (destroyHooksExist() && immediate[async_id_symbol] !== undefined) {
287287
emitDestroy(immediate[async_id_symbol]);
288288
}
289289

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict';
2+
const common = require('../common');
3+
const child_process = require('child_process');
4+
const assert = require('assert');
5+
6+
// Regression test for https://github.com/nodejs/node/issues/37806:
7+
const proc = child_process.spawn(process.execPath, ['-i']);
8+
proc.on('error', common.mustNotCall());
9+
proc.on('exit', common.mustCall((code) => {
10+
assert.strictEqual(code, 0);
11+
}));
12+
proc.stdin.write('clearImmediate({});\n.exit\n');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict';
2+
require('../common');
3+
4+
// This test makes sure clearing timers with
5+
// objects doesn't throw
6+
clearImmediate({});
7+
clearTimeout({});
8+
clearInterval({});

0 commit comments

Comments
 (0)