From cf980b0311ec37154c3ad70bffe69b2b929c7660 Mon Sep 17 00:00:00 2001 From: Daniel Bevenius Date: Sat, 6 May 2017 12:33:28 +0200 Subject: [PATCH] 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: https://github.com/nodejs/node/pull/12871 Reviewed-By: Anna Henningsen Reviewed-By: Santiago Gimeno Reviewed-By: Refael Ackermann Reviewed-By: James M Snell Reviewed-By: Joyee Cheung Reviewed-By: Colin Ihrig --- lib/net.js | 6 ++++-- test/parallel/test-socket-address.js | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 test/parallel/test-socket-address.js diff --git a/lib/net.js b/lib/net.js index f1061922ca6150..2da278a32d2f50 100644 --- a/lib/net.js +++ b/lib/net.js @@ -1473,8 +1473,10 @@ Object.defineProperty(Server.prototype, 'listening', { Server.prototype.address = function() { if (this._handle && this._handle.getsockname) { var out = {}; - this._handle.getsockname(out); - // TODO(bnoordhuis) Check err and throw? + var err = this._handle.getsockname(out); + if (err) { + throw errnoException(err, 'address'); + } return out; } else if (this._pipeName) { return this._pipeName; diff --git a/test/parallel/test-socket-address.js b/test/parallel/test-socket-address.js new file mode 100644 index 00000000000000..3e05f89f083223 --- /dev/null +++ b/test/parallel/test-socket-address.js @@ -0,0 +1,17 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const net = require('net'); + +// This tests checks that if server._handle.getsockname +// returns an error number, an error is thrown. + +const server = net.createServer({}); +server.listen(0, common.mustCall(function() { + server._handle.getsockname = function(out) { + return -1; + }; + assert.throws(() => this.address(), + /^Error: address ([\w|\s-\d])+$/); + server.close(); +}));