From dd18def7db2ff0810988c904c208995cbcb5adea Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Fri, 29 Jan 2021 15:03:42 +0100 Subject: [PATCH] async_hooks: refactor to avoid unsafe array iteration PR-URL: https://github.com/nodejs/node/pull/37125 Reviewed-By: Zijian Liu Reviewed-By: Darshan Sen --- lib/async_hooks.js | 15 +++++++-------- lib/internal/async_hooks.js | 4 ++-- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/lib/async_hooks.js b/lib/async_hooks.js index 90b48ebe4b2754..338a4c44f2e9cc 100644 --- a/lib/async_hooks.js +++ b/lib/async_hooks.js @@ -87,7 +87,7 @@ class AsyncHook { // enable()/disable() are run during their execution. The following // references are reassigned to the tmp arrays if a hook is currently being // processed. - const [hooks_array, hook_fields] = getHookArrays(); + const { 0: hooks_array, 1: hook_fields } = getHookArrays(); // Each hook is only allowed to be added once. if (ArrayPrototypeIncludes(hooks_array, this)) @@ -116,7 +116,7 @@ class AsyncHook { } disable() { - const [hooks_array, hook_fields] = getHookArrays(); + const { 0: hooks_array, 1: hook_fields } = getHookArrays(); const index = ArrayPrototypeIndexOf(hooks_array, this); if (index === -1) @@ -193,8 +193,7 @@ class AsyncResource { emitBefore(asyncId, this[trigger_async_id_symbol], this); try { - const ret = thisArg === undefined ? - fn(...args) : + const ret = ReflectApply(fn, thisArg, args); return ret; @@ -302,7 +301,7 @@ class AsyncLocalStorage { run(store, callback, ...args) { // Avoid creation of an AsyncResource if store is already active if (ObjectIs(store, this.getStore())) { - return callback(...args); + return ReflectApply(callback, null, args); } const resource = new AsyncResource('AsyncLocalStorage', defaultAlsResourceOpts); @@ -310,17 +309,17 @@ class AsyncLocalStorage { // It is ok because emitDestroy only schedules calling the hook return resource.emitDestroy().runInAsyncScope(() => { this.enterWith(store); - return callback(...args); + return ReflectApply(callback, null, args); }); } exit(callback, ...args) { if (!this.enabled) { - return callback(...args); + return ReflectApply(callback, null, args); } this.disable(); try { - return callback(...args); + return ReflectApply(callback, null, args); } finally { this._enable(); } diff --git a/lib/internal/async_hooks.js b/lib/internal/async_hooks.js index 28d0655d97c7f6..a9c5a171c73447 100644 --- a/lib/internal/async_hooks.js +++ b/lib/internal/async_hooks.js @@ -428,14 +428,14 @@ function clearDefaultTriggerAsyncId() { function defaultTriggerAsyncIdScope(triggerAsyncId, block, ...args) { if (triggerAsyncId === undefined) - return block(...args); + return ReflectApply(block, null, args); // CHECK(NumberIsSafeInteger(triggerAsyncId)) // CHECK(triggerAsyncId > 0) const oldDefaultTriggerAsyncId = async_id_fields[kDefaultTriggerAsyncId]; async_id_fields[kDefaultTriggerAsyncId] = triggerAsyncId; try { - return block(...args); + return ReflectApply(block, null, args); } finally { async_id_fields[kDefaultTriggerAsyncId] = oldDefaultTriggerAsyncId; }