Skip to content

Instantly share code, notes, and snippets.

@misterdjules
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save misterdjules/7e4f25997b3805792aac to your computer and use it in GitHub Desktop.
Save misterdjules/7e4f25997b3805792aac to your computer and use it in GitHub Desktop.
iff --git a/lib/net.js b/lib/net.js
index 284c37d..1491994 100644
--- a/lib/net.js
+++ b/lib/net.js
@@ -843,6 +843,13 @@ function connect(self, address, port, addressType, localAddress, localPort) {
}
}
+/*
+ * This function returns `true` if `value` is a string that represents a valid
+ * decimal or hexadecimal non-negative number, false otherwise.
+ */
+function isNonNegativeNumeric(n) {
+ return !isNaN(parseFloat(n)) && isFinite(n) && n >= 0;
+}
Socket.prototype.connect = function(options, cb) {
if (this.write !== Socket.prototype.write)
@@ -892,7 +899,6 @@ Socket.prototype.connect = function(options, cb) {
} else {
var dns = require('dns');
var host = options.host || 'localhost';
- var port = options.port | 0;
var localAddress = options.localAddress;
var localPort = options.localPort;
var dnsopts = {
@@ -906,9 +912,13 @@ Socket.prototype.connect = function(options, cb) {
if (localPort && !util.isNumber(localPort))
throw new TypeError('localPort should be a number: ' + localPort);
- if (options.port !== undefined && parseInt(options.port, 10) !== port)
- throw new TypeError('port should be a number: ' + options.port);
+ var validPortNumber = isNonNegativeNumeric(options.port);
+ if (options.port !== undefined && !validPortNumber)
+ throw new TypeError('port should be a non-negative number: ' + options.port);
+ // At this point, we know that options.port represents a valid number,
+ // or is undefined, so we can safely convert it to an integer.
+ var port = options.port | 0;
if (port < 0 || port > 65535)
throw new RangeError('port should be >= 0 and < 65536: ' + port);
diff --git a/test/simple/test-net-create-connection.js b/test/simple/test-net-create-connection.js
index 5ee5325..1b5b4ec 100644
--- a/test/simple/test-net-create-connection.js
+++ b/test/simple/test-net-create-connection.js
@@ -24,7 +24,7 @@ var assert = require('assert');
var net = require('net');
var tcpPort = common.PORT;
-var expectedConnections = 6;
+var expectedConnections = 7;
var clientConnected = 0;
var serverConnected = 0;
@@ -54,42 +54,43 @@ server.listen(tcpPort, 'localhost', function() {
net.createConnection(tcpPort, 'localhost', cb);
net.createConnection(tcpPort + '', 'localhost', cb);
net.createConnection({port: tcpPort + ''}).on('connect', cb);
+ net.createConnection({port: '0x' + tcpPort.toString(16)}).on('connect', cb);
fail({
port: ''
- }, TypeError, 'port should be a number: ');
+ }, TypeError, 'port should be a non-negative number: ');
fail({
port: 'invalid!'
- }, TypeError, 'port should be a number: invalid!');
+ }, TypeError, 'port should be a non-negative number: invalid!');
fail({
port: true
- }, TypeError, 'port should be a number: true');
+ }, TypeError, 'port should be a non-negative number: true');
fail({
port: false
- }, TypeError, 'port should be a number: false');
+ }, TypeError, 'port should be a non-negative number: false');
fail({
port: []
- }, TypeError, 'port should be a number: ');
+ }, TypeError, 'port should be a non-negative number: ');
fail({
port: {}
- }, TypeError, 'port should be a number: [object Object]');
+ }, TypeError, 'port should be a non-negative number: [object Object]');
fail({
port: null
- }, TypeError, 'port should be a number: null');
+ }, TypeError, 'port should be a non-negative number: null');
fail({
port: NaN
- }, TypeError, 'port should be a number: NaN');
+ }, TypeError, 'port should be a non-negative number: NaN');
fail({
port: Infinity
- }, TypeError, 'port should be a number: Infinity');
+ }, TypeError, 'port should be a non-negative number: Infinity');
fail({
host: 'localhost',
@@ -99,7 +100,7 @@ server.listen(tcpPort, 'localhost', function() {
fail({
host: 'localhost',
port: -1
- }, RangeError, 'port should be >= 0 and < 65536: -1');
+ }, TypeError, 'port should be a non-negative number: -1');
});
// Try connecting to random ports, but do so once the server is closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment