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

2 files changed

Lines changed: 9 additions & 10 deletions

File tree

‎lib/async_hooks.js‎

Lines changed: 7 additions & 8 deletions
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
constindex=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-
constret=thisArg===undefined ?
199-
fn(...args) :
198+
constret=
200199
ReflectApply(fn,thisArg,args);
201200

202201
returnret;
@@ -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-
returncallback(...args);
305+
returnReflectApply(callback,null,args);
307306
}
308307
constresource=newAsyncResource('AsyncLocalStorage',
309308
defaultAlsResourceOpts);
310309
// Calling emitDestroy before runInAsyncScope avoids a try/finally
311310
// It is ok because emitDestroy only schedules calling the hook
312311
returnresource.emitDestroy().runInAsyncScope(()=>{
313312
this.enterWith(store);
314-
returncallback(...args);
313+
returnReflectApply(callback,null,args);
315314
});
316315
}
317316

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

‎lib/internal/async_hooks.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,14 +421,14 @@ function clearDefaultTriggerAsyncId() {
421421

422422
functiondefaultTriggerAsyncIdScope(triggerAsyncId,block, ...args){
423423
if(triggerAsyncId===undefined)
424-
returnblock(...args);
424+
returnReflectApply(block,null,args);
425425
// CHECK(NumberIsSafeInteger(triggerAsyncId))
426426
// CHECK(triggerAsyncId > 0)
427427
constoldDefaultTriggerAsyncId=async_id_fields[kDefaultTriggerAsyncId];
428428
async_id_fields[kDefaultTriggerAsyncId]=triggerAsyncId;
429429

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

0 commit comments

Comments
 (0)