mirror of
https://github.com/zebrajr/node.git
synced 2026-01-15 12:15:26 +00:00
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 PR-URL: https://github.com/nodejs/node/pull/43010 Reviewed-By: Paolo Insogna <paolo@cowtech.it> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Zeyu "Alex" Yang <himself65@outlook.com>
This commit is contained in:
@@ -780,9 +780,10 @@ Socket.prototype._getpeername = function() {
|
||||
if (!this._handle || !this._handle.getpeername || this.connecting) {
|
||||
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;
|
||||
};
|
||||
|
||||
23
test/parallel/test-net-remote-address.js
Normal file
23
test/parallel/test-net-remote-address.js
Normal file
@@ -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();
|
||||
}));
|
||||
}));
|
||||
Reference in New Issue
Block a user