Skip to content

Commit cf980b0

Browse files
committed
net: check and throw on error for getsockname
This commit attempts fix a TODO in net.js: TODO(bnoordhuis) Check err and throw? PR-URL: #12871 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Santiago Gimeno <[email protected]> Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent 771568a commit cf980b0

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

lib/net.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1473,8 +1473,10 @@ Object.defineProperty(Server.prototype, 'listening', {
14731473
Server.prototype.address = function() {
14741474
if (this._handle && this._handle.getsockname) {
14751475
var out = {};
1476-
this._handle.getsockname(out);
1477-
// TODO(bnoordhuis) Check err and throw?
1476+
var err = this._handle.getsockname(out);
1477+
if (err) {
1478+
throw errnoException(err, 'address');
1479+
}
14781480
return out;
14791481
} else if (this._pipeName) {
14801482
return this._pipeName;

test/parallel/test-socket-address.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const net = require('net');
5+
6+
// This tests checks that if server._handle.getsockname
7+
// returns an error number, an error is thrown.
8+
9+
const server = net.createServer({});
10+
server.listen(0, common.mustCall(function() {
11+
server._handle.getsockname = function(out) {
12+
return -1;
13+
};
14+
assert.throws(() => this.address(),
15+
/^Error: address ([\w|\s-\d])+$/);
16+
server.close();
17+
}));

0 commit comments

Comments
 (0)