Skip to content

Commit 4a113be

Browse files
aduh95Trott
authored andcommittedFeb 1, 2021
http: refactor to avoid unsafe array iteration
PR-URL: nodejs#37124 Reviewed-By: Zijian Liu <[email protected]> Reviewed-By: Darshan Sen <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent e30e035 commit 4a113be

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
@@ -201,8 +201,9 @@ function maybeEnableKeylog(eventName) {
201201
agent.emit('keylog', keylog, this);
202202
};
203203
// Existing sockets will start listening on keylog now.
204-
for (const socket of ObjectValues(this.sockets)) {
205-
socket.on('keylog', this[kOnKeylog]);
204+
const sockets = ObjectValues(this.sockets);
205+
for (let i = 0; i < sockets.length; i++) {
206+
sockets[i].on('keylog', this[kOnKeylog]);
206207
}
207208
}
208209
}
@@ -426,7 +427,9 @@ Agent.prototype.removeSocket = function removeSocket(s, options) {
426427
if (!s.writable)
427428
ArrayPrototypePush(sets, this.freeSockets);
428429

429-
for (const sockets of sets) {
430+
for (let sk = 0; sk < sets.length; sk++) {
431+
const sockets = sets[sk];
432+
430433
if (sockets[name]) {
431434
const index = ArrayPrototypeIndexOf(sockets[name], s);
432435
if (index !== -1) {
@@ -492,10 +495,14 @@ Agent.prototype.reuseSocket = function reuseSocket(socket, req) {
492495
};
493496

494497
Agent.prototype.destroy = function destroy() {
495-
for (const set of [this.freeSockets, this.sockets]) {
496-
for (const key of ObjectKeys(set)) {
497-
for (const setName of set[key]) {
498-
setName.destroy();
498+
const sets = [this.freeSockets, this.sockets];
499+
for (let s = 0; s < sets.length; s++) {
500+
const set = sets[s];
501+
const keys = ObjectKeys(set);
502+
for (let v = 0; v < keys.length; v++) {
503+
const setName = set[keys[v]];
504+
for (let n = 0; n < setName.length; n++) {
505+
setName[n].destroy();
499506
}
500507
}
501508
}

‎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)
Please sign in to comment.