Skip to content

Commit 7196ac1

Browse files
aduh95targos
authored andcommitted
http: refactor to avoid unsafe array iteration
PR-URL: #37124 Reviewed-By: Zijian Liu <lxxyxzj@gmail.com> Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent e712650 commit 7196ac1

3 files changed

Lines changed: 21 additions & 13 deletions

File tree

‎lib/_http_agent.js‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,9 @@ function maybeEnableKeylog(eventName) {
200200
agent.emit('keylog',keylog,this);
201201
};
202202
// Existing sockets will start listening on keylog now.
203-
for(constsocketofObjectValues(this.sockets)){
204-
socket.on('keylog',this[kOnKeylog]);
203+
constsockets=ObjectValues(this.sockets);
204+
for(leti=0;i<sockets.length;i++){
205+
sockets[i].on('keylog',this[kOnKeylog]);
205206
}
206207
}
207208
}
@@ -424,7 +425,9 @@ Agent.prototype.removeSocket = function removeSocket(s, options) {
424425
if(!s.writable)
425426
ArrayPrototypePush(sets,this.freeSockets);
426427

427-
for(constsocketsofsets){
428+
for(letsk=0;sk<sets.length;sk++){
429+
constsockets=sets[sk];
430+
428431
if(sockets[name]){
429432
constindex=ArrayPrototypeIndexOf(sockets[name],s);
430433
if(index!==-1){
@@ -490,10 +493,14 @@ Agent.prototype.reuseSocket = function reuseSocket(socket, req) {
490493
};
491494

492495
Agent.prototype.destroy=functiondestroy(){
493-
for(constsetof[this.freeSockets,this.sockets]){
494-
for(constkeyofObjectKeys(set)){
495-
for(constsetNameofset[key]){
496-
setName.destroy();
496+
constsets=[this.freeSockets,this.sockets];
497+
for(lets=0;s<sets.length;s++){
498+
constset=sets[s];
499+
constkeys=ObjectKeys(set);
500+
for(letv=0;v<keys.length;v++){
501+
constsetName=set[keys[v]];
502+
for(letn=0;n<setName.length;n++){
503+
setName[n].destroy();
497504
}
498505
}
499506
}

‎lib/_http_outgoing.js‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
const{
2525
ArrayIsArray,
26+
ArrayPrototypeForEach,
2627
ArrayPrototypeJoin,
2728
ArrayPrototypePush,
2829
ArrayPrototypeUnshift,
@@ -390,9 +391,9 @@ function _storeHeader(firstLine, headers) {
390391
}
391392
}elseif(ArrayIsArray(headers)){
392393
if(headers.length&&ArrayIsArray(headers[0])){
393-
for(constentryofheaders){
394-
processHeader(this,state,entry[0],entry[1],true);
395-
}
394+
ArrayPrototypeForEach(headers,(entry)=>
395+
processHeader(this,state,entry[0],entry[1],true)
396+
);
396397
}else{
397398
if(headers.length%2!==0){
398399
thrownewERR_INVALID_ARG_VALUE('headers',headers);

‎lib/_http_server.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
const{
2525
ArrayIsArray,
26+
ArrayPrototypeForEach,
2627
ArrayPrototypePush,
2728
ArrayPrototypeShift,
2829
Error,
@@ -417,9 +418,8 @@ Server.prototype[EE.captureRejectionSymbol] = function(err, event, ...args) {
417418
const[,res]=args;
418419
if(!res.headersSent&&!res.writableEnded){
419420
// Don't leak headers.
420-
for(constnameofres.getHeaderNames()){
421-
res.removeHeader(name);
422-
}
421+
ArrayPrototypeForEach(res.getHeaderNames(),
422+
(name)=>res.removeHeader(name));
423423
res.statusCode=500;
424424
res.end(STATUS_CODES[500]);
425425
}else{

0 commit comments

Comments
 (0)