net: allow port 0 in connect() · nodejs/node@9d2b89d · GitHub
Skip to content

Commit 9d2b89d

Browse files
committed
net: allow port 0 in connect()
The added validation allows non-negative numbers and numeric strings. All other values result in a thrown exception. Fixes: nodejs/node-v0.x-archive#9194 PR-URL: nodejs/node-v0.x-archive#9268 Reviewed-By: Julien Gilli <julien.gilli@joyent.com> Reviewed-By: Trevor Norris <trev.norris@gmail.com> Reviewed-By: James M Snell <jasnell@users.noreply.github.com>
1 parent 31421af commit 9d2b89d

4 files changed

Lines changed: 97 additions & 33 deletions

File tree

‎lib/net.js‎

Lines changed: 11 additions & 3 deletions

‎src/tcp_wrap.cc‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ void TCPWrap::Connect(const FunctionCallbackInfo<Value>& args) {
350350

351351
CHECK(args[0]->IsObject());
352352
CHECK(args[1]->IsString());
353-
CHECK(args[2]->Uint32Value());
353+
CHECK(args[2]->IsUint32());
354354

355355
Local<Object> req_wrap_obj = args[0].As<Object>();
356356
node::Utf8Value ip_address(env->isolate(), args[1]);
@@ -381,7 +381,7 @@ void TCPWrap::Connect6(const FunctionCallbackInfo<Value>& args) {
381381

382382
CHECK(args[0]->IsObject());
383383
CHECK(args[1]->IsString());
384-
CHECK(args[2]->Uint32Value());
384+
CHECK(args[2]->IsUint32());
385385

386386
Local<Object> req_wrap_obj = args[0].As<Object>();
387387
node::Utf8Value ip_address(env->isolate(), args[1]);

‎test/parallel/test-net-create-connection.js‎

Lines changed: 74 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,99 @@ var assert = require('assert');
33
var net = require('net');
44

55
var tcpPort = common.PORT;
6+
var expectedConnections = 7;
67
var clientConnected = 0;
78
var serverConnected = 0;
89

910
var server = net.createServer(function(socket) {
1011
socket.end();
11-
if (++serverConnected === 4) {
12+
if (++serverConnected === expectedConnections) {
1213
server.close();
1314
}
1415
});
16+
1517
server.listen(tcpPort, 'localhost', function() {
1618
function cb() {
1719
++clientConnected;
1820
}
1921

22+
function fail(opts, errtype, msg) {
23+
assert.throws(function() {
24+
var client = net.createConnection(opts, cb);
25+
}, function (err) {
26+
return err instanceof errtype && msg === err.message;
27+
});
28+
}
29+
2030
net.createConnection(tcpPort).on('connect', cb);
2131
net.createConnection(tcpPort, 'localhost').on('connect', cb);
2232
net.createConnection(tcpPort, cb);
2333
net.createConnection(tcpPort, 'localhost', cb);
34+
net.createConnection(tcpPort + '', 'localhost', cb);
35+
net.createConnection({port: tcpPort + ''}).on('connect', cb);
36+
net.createConnection({port: '0x' + tcpPort.toString(16)}, cb);
37+
38+
fail({
39+
port: true
40+
}, TypeError, 'port should be a number or string: true');
41+
42+
fail({
43+
port: false
44+
}, TypeError, 'port should be a number or string: false');
45+
46+
fail({
47+
port: []
48+
}, TypeError, 'port should be a number or string: ');
49+
50+
fail({
51+
port: {}
52+
}, TypeError, 'port should be a number or string: [object Object]');
53+
54+
fail({
55+
port: null
56+
}, TypeError, 'port should be a number or string: null');
57+
58+
fail({
59+
port: ''
60+
}, RangeError, 'port should be >= 0 and < 65536: ');
61+
62+
fail({
63+
port: ' '
64+
}, RangeError, 'port should be >= 0 and < 65536: ');
2465

25-
assert.throws(function () {
26-
net.createConnection({
27-
port: 'invalid!'
28-
}, cb);
29-
});
66+
fail({
67+
port: '0x'
68+
}, RangeError, 'port should be >= 0 and < 65536: 0x');
69+
70+
fail({
71+
port: '-0x1'
72+
}, RangeError, 'port should be >= 0 and < 65536: -0x1');
73+
74+
fail({
75+
port: NaN
76+
}, RangeError, 'port should be >= 0 and < 65536: NaN');
77+
78+
fail({
79+
port: Infinity
80+
}, RangeError, 'port should be >= 0 and < 65536: Infinity');
81+
82+
fail({
83+
port: -1
84+
}, RangeError, 'port should be >= 0 and < 65536: -1');
85+
86+
fail({
87+
port: 65536
88+
}, RangeError, 'port should be >= 0 and < 65536: 65536');
3089
});
3190

32-
process.on('exit', function() {
33-
assert.equal(clientConnected, 4);
91+
// Try connecting to random ports, but do so once the server is closed
92+
server.on('close', function() {
93+
function nop() {}
94+
95+
net.createConnection({port: 0}).on('error', nop);
96+
net.createConnection({port: undefined}).on('error', nop);
3497
});
3598

99+
process.on('exit', function() {
100+
assert.equal(clientConnected, expectedConnections);
101+
});

‎test/parallel/test-net-localerror.js‎

Lines changed: 10 additions & 20 deletions

0 commit comments

Comments
 (0)