Last active
August 29, 2015 14:16
-
-
Save misterdjules/741cb578638b18f139ef to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/lib/net.js b/lib/net.js | |
index 284c37d..d7c741a 100644 | |
--- a/lib/net.js | |
+++ b/lib/net.js | |
@@ -843,6 +843,17 @@ function connect(self, address, port, addressType, localAddress, localPort) { | |
} | |
} | |
+/* | |
+ * This function returns `true` if `value` is a string that represents a valid | |
+ * decimal or hexadecimal number, false otherwise. | |
+ */ | |
+function stringRepresentsNumber(numberString) { | |
+ assert(typeof numberString === 'string'); | |
+ | |
+ var integerValue = numberString | 0; | |
+ return parseInt(numberString, 10) === integerValue || | |
+ parseInt(numberString, 16) === integerValue; | |
+} | |
Socket.prototype.connect = function(options, cb) { | |
if (this.write !== Socket.prototype.write) | |
@@ -892,7 +903,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 +916,17 @@ 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) | |
+ var validPortNumber = util.isNumber(options.port) && | |
+ isFinite(options.port); | |
+ var validPortString = util.isString(options.port) && | |
+ stringRepresentsNumber(options.port); | |
+ | |
+ if (options.port !== undefined && !validPortNumber && !validPortString) | |
throw new TypeError('port should be a 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..8164e90 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,6 +54,7 @@ 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: '' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment