Skip to content

Commit

Permalink
net: fix socket._getpeername
Browse files Browse the repository at this point in the history
Fixes: #43009

If calling `this._handle.getpeername` returns an error at the first
call, its result shouldn't be cached to `this._peername`.

Signed-off-by: Daeyeon Jeong [email protected]

PR-URL: #43010
Reviewed-By: Paolo Insogna <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Zeyu "Alex" Yang <[email protected]>
  • Loading branch information
daeyeon authored and targos committed Jul 31, 2022
1 parent e3fe40e commit 47ef142
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -785,9 +785,10 @@ Socket.prototype._getpeername = function() {
if (!this._handle || !this._handle.getpeername) {
return this._peername || {};
} else if (!this._peername) {
this._peername = {};
// FIXME(bnoordhuis) Throw when the return value is not 0?
this._handle.getpeername(this._peername);
const out = {};
const err = this._handle.getpeername(out);
if (err) return out;
this._peername = out;
}
return this._peername;
};
Expand Down
23 changes: 23 additions & 0 deletions test/parallel/test-net-remote-address.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';

const common = require('../common');
const net = require('net');
const { strictEqual } = require('assert');

const server = net.createServer();

server.listen(common.mustCall(function() {
const socket = net.connect({ port: server.address().port });

strictEqual(socket.connecting, true);
strictEqual(socket.remoteAddress, undefined);

socket.on('connect', common.mustCall(function() {
strictEqual(socket.remoteAddress !== undefined, true);
socket.end();
}));

socket.on('end', common.mustCall(function() {
server.close();
}));
}));

0 comments on commit 47ef142

Please sign in to comment.