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

File tree

3 files changed

+21
-13
lines changed

3 files changed

+21
-13
lines changed

lib/_http_agent.js

+14-7
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 (const socket of ObjectValues(this.sockets)) {
204-
socket.on('keylog', this[kOnKeylog]);
203+
const sockets = ObjectValues(this.sockets);
204+
for (let i = 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 (const sockets of sets) {
428+
for (let sk = 0; sk < sets.length; sk++) {
429+
const sockets = sets[sk];
430+
428431
if (sockets[name]) {
429432
const index = 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 = function destroy() {
493-
for (const set of [this.freeSockets, this.sockets]) {
494-
for (const key of ObjectKeys(set)) {
495-
for (const setName of set[key]) {
496-
setName.destroy();
496+
const sets = [this.freeSockets, this.sockets];
497+
for (let s = 0; s < sets.length; s++) {
498+
const set = sets[s];
499+
const keys = ObjectKeys(set);
500+
for (let v = 0; v < keys.length; v++) {
501+
const setName = set[keys[v]];
502+
for (let n = 0; n < setName.length; n++) {
503+
setName[n].destroy();
497504
}
498505
}
499506
}

lib/_http_outgoing.js

+4-3
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
} else if (ArrayIsArray(headers)) {
392393
if (headers.length && ArrayIsArray(headers[0])) {
393-
for (const entry of headers) {
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
throw new ERR_INVALID_ARG_VALUE('headers', headers);

lib/_http_server.js

+3-3
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 (const name of res.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)