Skip to content

Commit 4678fd8

Browse files
committed
Fix improper parsing of octal bytes
1 parent b95d113 commit 4678fd8

File tree

3 files changed

+29
-10
lines changed

3 files changed

+29
-10
lines changed

lib/netmask.coffee

+7-1
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,14 @@ ip2long = (ip) ->
99
b = (ip + '').split('.');
1010
if b.length is 0 or b.length > 4 then throw new Error('Invalid IP')
1111
for byte, i in b
12-
if isNaN parseInt(byte, 10) then throw new Error("Invalid byte: #{byte}")
12+
if byte and byte[0] == '0'
13+
# make sure 0 prefixed bytes are parsed as octal
14+
byte = parseInt(byte, 8)
15+
else
16+
byte = parseInt(byte, 10)
17+
if isNaN(byte) then throw new Error("Invalid byte: #{byte}")
1318
if byte < 0 or byte > 255 then throw new Error("Invalid byte: #{byte}")
19+
b[i] = byte
1420
return ((b[0] or 0) << 24 | (b[1] or 0) << 16 | (b[2] or 0) << 8 | (b[3] or 0)) >>> 0
1521

1622

package.json

+14-9
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,29 @@
66
"homepage": "https://github.com/rs/node-netmask",
77
"bugs": "https://github.com/rs/node-netmask/issues",
88
"license": "MIT",
9-
"repository":
10-
{
9+
"repository": {
1110
"type": "git",
1211
"url": "git://github.com/rs/node-netmask.git"
1312
},
14-
"keywords": ["net", "mask", "ip", "network", "cidr", "netmask", "subnet", "ipcalc"],
13+
"keywords": [
14+
"net",
15+
"mask",
16+
"ip",
17+
"network",
18+
"cidr",
19+
"netmask",
20+
"subnet",
21+
"ipcalc"
22+
],
1523
"main": "./lib/netmask",
16-
"scripts":
17-
{
24+
"scripts": {
1825
"prepublish": "coffee -c lib/*.coffee",
1926
"test": "vows --spec test/*"
2027
},
21-
"engines":
22-
{
28+
"engines": {
2329
"node": ">= 0.4.0"
2430
},
25-
"devDependencies":
26-
{
31+
"devDependencies": {
2732
"coffee-script": ">=1.2.0",
2833
"vows": "*"
2934
}

test/netmasks.coffee

+8
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ vows.describe('Netmask contains IP')
5353
'block 192.168.0.0/24':
5454
topic: -> new Netmask('192.168.0.0/24')
5555
'does not contain block 192.168': (block) -> assert.ok not block.contains('192.168')
56+
'block 31.0.0.0/8':
57+
topic: -> new Netmask('31.0.0.0/8')
58+
'contains IP 31.5.5.5': (block) -> assert.ok block.contains('31.5.5.5')
59+
'does not contain IP 031.5.5.5 (25.5.5.5)': (block) -> assert.ok not block.contains('031.5.5.5')
60+
'block 127.0.0.0/8':
61+
topic: -> new Netmask('127.0.0.0/8')
62+
'contains IP 127.0.0.2': (block) -> assert.ok block.contains('127.0.0.2')
63+
'contains IP 0177.0.0.2 (127.0.0.2)': (block) -> assert.ok block.contains('0177.0.0.2')
5664
.export(module)
5765

5866
vows.describe('Netmask forEach')

0 commit comments

Comments
 (0)