Skip to content

Commit 602aaf2

Browse files
aduh95targos
authored andcommitted
async_hooks: refactor to avoid unsafe array iteration
PR-URL: #37125 Reviewed-By: Zijian Liu <lxxyxzj@gmail.com> Reviewed-By: Darshan Sen <raisinten@gmail.com>
1 parent 634bedc commit 602aaf2

File tree

2 files changed

+9
-10
lines changed

2 files changed

+9
-10
lines changed

lib/async_hooks.js

+7-8
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class AsyncHook {
8989
// enable()/disable() are run during their execution. The following
9090
// references are reassigned to the tmp arrays if a hook is currently being
9191
// processed.
92-
const [hooks_array, hook_fields] = getHookArrays();
92+
const { 0: hooks_array, 1: hook_fields } = getHookArrays();
9393

9494
// Each hook is only allowed to be added once.
9595
if (ArrayPrototypeIncludes(hooks_array, this))
@@ -118,7 +118,7 @@ class AsyncHook {
118118
}
119119

120120
disable() {
121-
const [hooks_array, hook_fields] = getHookArrays();
121+
const { 0: hooks_array, 1: hook_fields } = getHookArrays();
122122

123123
const index = ArrayPrototypeIndexOf(hooks_array, this);
124124
if (index === -1)
@@ -195,8 +195,7 @@ class AsyncResource {
195195
emitBefore(asyncId, this[trigger_async_id_symbol], this);
196196

197197
try {
198-
const ret = thisArg === undefined ?
199-
fn(...args) :
198+
const ret =
200199
ReflectApply(fn, thisArg, args);
201200

202201
return ret;
@@ -303,25 +302,25 @@ class AsyncLocalStorage {
303302
run(store, callback, ...args) {
304303
// Avoid creation of an AsyncResource if store is already active
305304
if (ObjectIs(store, this.getStore())) {
306-
return callback(...args);
305+
return ReflectApply(callback, null, args);
307306
}
308307
const resource = new AsyncResource('AsyncLocalStorage',
309308
defaultAlsResourceOpts);
310309
// Calling emitDestroy before runInAsyncScope avoids a try/finally
311310
// It is ok because emitDestroy only schedules calling the hook
312311
return resource.emitDestroy().runInAsyncScope(() => {
313312
this.enterWith(store);
314-
return callback(...args);
313+
return ReflectApply(callback, null, args);
315314
});
316315
}
317316

318317
exit(callback, ...args) {
319318
if (!this.enabled) {
320-
return callback(...args);
319+
return ReflectApply(callback, null, args);
321320
}
322321
this.disable();
323322
try {
324-
return callback(...args);
323+
return ReflectApply(callback, null, args);
325324
} finally {
326325
this._enable();
327326
}

lib/internal/async_hooks.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -421,14 +421,14 @@ function clearDefaultTriggerAsyncId() {
421421

422422
function defaultTriggerAsyncIdScope(triggerAsyncId, block, ...args) {
423423
if (triggerAsyncId === undefined)
424-
return block(...args);
424+
return ReflectApply(block, null, args);
425425
// CHECK(NumberIsSafeInteger(triggerAsyncId))
426426
// CHECK(triggerAsyncId > 0)
427427
const oldDefaultTriggerAsyncId = async_id_fields[kDefaultTriggerAsyncId];
428428
async_id_fields[kDefaultTriggerAsyncId] = triggerAsyncId;
429429

430430
try {
431-
return block(...args);
431+
return ReflectApply(block, null, args);
432432
} finally {
433433
async_id_fields[kDefaultTriggerAsyncId] = oldDefaultTriggerAsyncId;
434434
}

0 commit comments

Comments
 (0)