Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net: fix abort on bad address input #13726

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const WriteWrap = process.binding('stream_wrap').WriteWrap;
const async_id_symbol = process.binding('async_wrap').async_id_symbol;
const { newUid, setInitTriggerId } = require('async_hooks');
const nextTick = require('internal/process/next_tick').nextTick;
const errors = require('internal/errors');

var cluster;
var dns;
Expand Down Expand Up @@ -872,6 +873,10 @@ function internalConnect(

var err;

if (typeof address !== 'string') {
Copy link
Contributor

@mscdex mscdex Jun 16, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move this to .connect() instead, under the if (pipe) check? internalConnect() is also called for non-pipe connections and so it would be an unnecessary/redundant check in those cases (since the address would be validated at time of lookup() there).

throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'address', 'string');
}

if (localAddress || localPort) {
debug('binding to localAddress: %s and localPort: %d (addressType: %d)',
localAddress, localPort, addressType);
Expand Down
22 changes: 15 additions & 7 deletions test/parallel/test-net-better-error-messages-path.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@
const common = require('../common');
const net = require('net');
const assert = require('assert');
const fp = '/tmp/fadagagsdfgsdf';
const c = net.connect(fp);

c.on('connect', common.mustNotCall());
{
const fp = '/tmp/fadagagsdfgsdf';
const c = net.connect(fp);

c.on('error', common.mustCall(function(e) {
assert.strictEqual(e.code, 'ENOENT');
assert.strictEqual(e.message, `connect ENOENT ${fp}`);
}));
c.on('connect', common.mustNotCall());
c.on('error', common.mustCall(function(e) {
assert.strictEqual(e.code, 'ENOENT');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expectsError ditto

assert.strictEqual(e.message, `connect ENOENT ${fp}`);
}));
}

{
assert.throws(() => {
net.createConnection({ path: {} });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use assert.throws() here.

}, TypeError);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re: asserting error messages, Could you use expectsError

}