Skip to content

Commit

Permalink
fix: sockLen being miscalculated when removing sockets
Browse files Browse the repository at this point in the history
Order of operations issue with + and ternary statements:

    console.log(0 + '' ? 'a' : 'b') // a

The code:

    freeLen + this.sockets[name] ? this.sockets[name].length : 0;

Is equivalent to:

    (freeLen + this.sockets[name]) ? this.sockets[name].length : 0;

Because `this.sockets[name]` is an array, `(freeLen + this.sockets[name])` evaluates to a string, which is true, so the left side of the ternary expression was always being chosen.
  • Loading branch information
cixel committed Apr 10, 2018
1 parent 7cbca8e commit 91cb383
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion lib/_http_agent.js
Expand Up @@ -380,7 +380,7 @@ Agent.prototype.removeSocket = function removeSocket(s, options) {

// [patch start]
var freeLen = this.freeSockets[name] ? this.freeSockets[name].length : 0;
var sockLen = freeLen + this.sockets[name] ? this.sockets[name].length : 0;
var sockLen = freeLen + (this.sockets[name] ? this.sockets[name].length : 0);
// [patch end]

if (this.requests[name] && this.requests[name].length && sockLen < this.maxSockets) {
Expand Down

0 comments on commit 91cb383

Please sign in to comment.