From fd20648ed7f23cda809ebf8f69fd304faf7a86cd Mon Sep 17 00:00:00 2001 From: Daeyeon Jeong Date: Mon, 9 May 2022 00:39:34 +0900 Subject: [PATCH 1/3] net: fix socket._getpeername Fixes: https://github.com/nodejs/node/issues/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 daeyeon.dev@gmail.com --- lib/net.js | 6 ++++-- test/parallel/test-net-remote-address.js | 23 +++++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 test/parallel/test-net-remote-address.js diff --git a/lib/net.js b/lib/net.js index 4318beb501b028..85b283e01e3e8b 100644 --- a/lib/net.js +++ b/lib/net.js @@ -736,9 +736,11 @@ Socket.prototype._getpeername = function() { if (!this._handle || !this._handle.getpeername || this.connecting) { return this._peername || {}; } else if (!this._peername) { - this._peername = {}; + const out = {}; + const err = this._handle.getpeername(out); // FIXME(bnoordhuis) Throw when the return value is not 0? - this._handle.getpeername(this._peername); + if (err) return {}; + this._peername = out; } return this._peername; }; diff --git a/test/parallel/test-net-remote-address.js b/test/parallel/test-net-remote-address.js new file mode 100644 index 00000000000000..a116cb99d3bcab --- /dev/null +++ b/test/parallel/test-net-remote-address.js @@ -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(); + })); +})); From df47a462b1c7b8f6e8085b4daf2c2d137b9b7528 Mon Sep 17 00:00:00 2001 From: Daeyeon Jeong Date: Mon, 9 May 2022 01:46:56 +0900 Subject: [PATCH 2/3] fix: returning `out` Signed-off-by: Daeyeon Jeong daeyeon.dev@gmail.com --- lib/net.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/net.js b/lib/net.js index 85b283e01e3e8b..b294ab6128b259 100644 --- a/lib/net.js +++ b/lib/net.js @@ -739,7 +739,7 @@ Socket.prototype._getpeername = function() { const out = {}; const err = this._handle.getpeername(out); // FIXME(bnoordhuis) Throw when the return value is not 0? - if (err) return {}; + if (err) return out; this._peername = out; } return this._peername; From 0fe4a49133204434f4477b9a67220370219c3434 Mon Sep 17 00:00:00 2001 From: Daeyeon Jeong Date: Tue, 24 May 2022 20:12:04 +0900 Subject: [PATCH 3/3] fixup: remove todo on _getpeername Signed-off-by: Daeyeon Jeong daeyeon.dev@gmail.com --- lib/net.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/net.js b/lib/net.js index b294ab6128b259..95080e3dc3a872 100644 --- a/lib/net.js +++ b/lib/net.js @@ -738,7 +738,6 @@ Socket.prototype._getpeername = function() { } else if (!this._peername) { const out = {}; const err = this._handle.getpeername(out); - // FIXME(bnoordhuis) Throw when the return value is not 0? if (err) return out; this._peername = out; }