Browse Source

net: fix abort on bad address input

Backport-PR-URL: https://github.com/nodejs/node/pull/14390
PR-URL: https://github.com/nodejs/node/pull/13726
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
v6.x
Ruben Bridgewater 8 years ago
committed by Myles Borins
parent
commit
792acc17bf
No known key found for this signature in database GPG Key ID: 933B01F40B5CA946
  1. 10
      lib/net.js
  2. 23
      test/parallel/test-net-better-error-messages-path.js

10
lib/net.js

@ -912,8 +912,9 @@ Socket.prototype.connect = function(options, cb) {
this._sockname = null; this._sockname = null;
} }
var pipe = !!options.path; const path = options.path;
debug('pipe', pipe, options.path); var pipe = !!path;
debug('pipe', pipe, path);
if (!this._handle) { if (!this._handle) {
this._handle = pipe ? new Pipe() : new TCP(); this._handle = pipe ? new Pipe() : new TCP();
@ -930,7 +931,10 @@ Socket.prototype.connect = function(options, cb) {
this.writable = true; this.writable = true;
if (pipe) { if (pipe) {
connect(this, options.path); if (typeof path !== 'string') {
throw new TypeError('"path" option must be a string: ' + path);
}
connect(this, path);
} else { } else {
lookupAndConnect(this, options); lookupAndConnect(this, options);
} }

23
test/parallel/test-net-better-error-messages-path.js

@ -2,12 +2,21 @@
const common = require('../common'); const common = require('../common');
const net = require('net'); const net = require('net');
const assert = require('assert'); 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) { c.on('connect', common.mustNotCall());
assert.strictEqual(e.code, 'ENOENT'); c.on('error', common.mustCall(function(e) {
assert.strictEqual(e.message, `connect ENOENT ${fp}`); assert.strictEqual(e.code, 'ENOENT');
})); assert.strictEqual(e.message, `connect ENOENT ${fp}`);
}));
}
{
assert.throws(
() => net.createConnection({ path: {} }),
/"path" option must be a string: \[object Object]/
);
}

Loading…
Cancel
Save