From ed578883d53cdc13146377f20ab6136fa7e1411c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Sch=C3=A4r?= Date: Fri, 20 Nov 2015 22:16:55 +0100 Subject: [PATCH 1/4] net: variable ex used before defined --- lib/net.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/net.js b/lib/net.js index d11fef84c2e1a9..76ebb7040c2dd7 100644 --- a/lib/net.js +++ b/lib/net.js @@ -1058,15 +1058,17 @@ function afterConnect(status, handle, req, readable, writable) { self._connecting = false; var details; if (req.localAddress && req.localPort) { - ex.localAddress = req.localAddress; - ex.localPort = req.localPort; - details = ex.localAddress + ':' + ex.localPort; + details = req.localAddress + ':' + req.localPort; } var ex = exceptionWithHostPort(status, 'connect', req.address, req.port, details); + if (req.localAddress && req.localPort) { + ex.localAddress = req.localAddress; + ex.localPort = req.localPort; + } self._destroy(ex); } } From 751f4f4f9f0f02519e34273c8d830cb457a9a235 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Sch=C3=A4r?= Date: Sat, 21 Nov 2015 13:41:31 +0100 Subject: [PATCH 2/4] net: add localAddress/Port for better error msgs The code generating the errror messages was already there, but it could never execute because these properties were not set. --- lib/net.js | 2 ++ test/parallel/test-net-connect-local-error.js | 21 +++++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 test/parallel/test-net-connect-local-error.js diff --git a/lib/net.js b/lib/net.js index 76ebb7040c2dd7..4c9f3c22a4e153 100644 --- a/lib/net.js +++ b/lib/net.js @@ -817,6 +817,8 @@ function connect(self, address, port, addressType, localAddress, localPort) { req.oncomplete = afterConnect; req.address = address; req.port = port; + req.localAddress = localAddress; + req.localPort = localPort; if (addressType === 4) err = self._handle.connect(req, address, port); diff --git a/test/parallel/test-net-connect-local-error.js b/test/parallel/test-net-connect-local-error.js new file mode 100644 index 00000000000000..590065cac4b1ab --- /dev/null +++ b/test/parallel/test-net-connect-local-error.js @@ -0,0 +1,21 @@ +'use strict'; +var common = require('../common'); +var assert = require('assert'); +var net = require('net'); + +var client = net.connect({ + port: common.PORT + 1, + localPort: common.PORT, + localAddress: '127.0.0.1' +}); + +var onErrorCalled = false; +client.on('error', function(err) { + assert.equal(err.localPort, common.PORT); + assert.equal(err.localAddress, '127.0.0.1'); + onErrorCalled = true; +}); + +process.on('exit', function() { + assert.ok(onErrorCalled); +}); From 021ccb074803dab1e74a0bec6a411d8b4c103d99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Sch=C3=A4r?= Date: Mon, 23 Nov 2015 12:44:50 +0100 Subject: [PATCH 3/4] net: small optimization As suggested by @evanlucas --- lib/net.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/net.js b/lib/net.js index 4c9f3c22a4e153..f9865a96c9f220 100644 --- a/lib/net.js +++ b/lib/net.js @@ -1067,7 +1067,7 @@ function afterConnect(status, handle, req, readable, writable) { req.address, req.port, details); - if (req.localAddress && req.localPort) { + if (details) { ex.localAddress = req.localAddress; ex.localPort = req.localPort; } From d4ca05100210f686cc8e3b062768f312adf86c18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Sch=C3=A4r?= Date: Mon, 23 Nov 2015 19:53:31 +0100 Subject: [PATCH 4/4] test: improvements to test-net-connect-local-error As suggested by @evanlucas and @cjihrig --- test/parallel/test-net-connect-local-error.js | 20 +++++++------------ 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/test/parallel/test-net-connect-local-error.js b/test/parallel/test-net-connect-local-error.js index 590065cac4b1ab..197b283bdf3621 100644 --- a/test/parallel/test-net-connect-local-error.js +++ b/test/parallel/test-net-connect-local-error.js @@ -1,21 +1,15 @@ 'use strict'; -var common = require('../common'); -var assert = require('assert'); -var net = require('net'); +const common = require('../common'); +const assert = require('assert'); +const net = require('net'); var client = net.connect({ port: common.PORT + 1, localPort: common.PORT, - localAddress: '127.0.0.1' + localAddress: common.localhostIPv4 }); -var onErrorCalled = false; -client.on('error', function(err) { +client.on('error', common.mustCall(function onError(err) { assert.equal(err.localPort, common.PORT); - assert.equal(err.localAddress, '127.0.0.1'); - onErrorCalled = true; -}); - -process.on('exit', function() { - assert.ok(onErrorCalled); -}); + assert.equal(err.localAddress, common.localhostIPv4); +}));