8000 Fix improper parsing of hex bytes · rs/node-netmask@ec1b5b5 · GitHub
[go: up one dir, main page]

Skip to content

Commit ec1b5b5

Browse files
committed
Fix improper parsing of hex bytes
1 parent 4678fd8 commit ec1b5b5

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

lib/netmask.coffee

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@ ip2long = (ip) ->
1010
if b.length is 0 or b.length > 4 then throw new Error('Invalid IP')
1111
for byte, i in b
1212
if byte and byte[0] == '0'
13-
# make sure 0 prefixed bytes are parsed as octal
14-
byte = parseInt(byte, 8)
13+
if byte.length > 2 and (byte[1] == 'x' or byte[1] == 'x')
14+
# make sure 0x prefixed bytes are parsed as hex
15+
byte = parseInt(byte, 16)
16+
else
17+
# make sure 0 prefixed bytes are parsed as octal
18+
byte = parseInt(byte, 8)
1519
else
1620
byte = parseInt(byte, 10)
1721
if isNaN(byte) then throw new Error("Invalid byte: #{byte}")

test/netmasks.coffee

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,13 @@ vows.describe('Netmask contains IP')
5757
topic: -> new Netmask('31.0.0.0/8')
5858
'contains IP 31.5.5.5': (block) -> assert.ok block.contains('31.5.5.5')
5959
'does not contain IP 031.5.5.5 (25.5.5.5)': (block) -> assert.ok not block.contains('031.5.5.5')
60+
'does not contain IP 0x31.5.5.5 (49.5.5.5)': (block) -> assert.ok not block.contains('0x31.5.5.5')
61+
'does not contain IP 0X31.5.5.5 (49.5.5.5)': (block) -> assert.ok not block.contains('0X31.5.5.5')
6062
'block 127.0.0.0/8':
6163
topic: -> new Netmask('127.0.0.0/8')
6264
'contains IP 127.0.0.2': (block) -> assert.ok block.contains('127.0.0.2')
6365
'contains IP 0177.0.0.2 (127.0.0.2)': (block) -> assert.ok block.contains('0177.0.0.2')
66+
'contains IP 0x7f.0.0.2 (127.0.0.2)': (block) -> assert.ok block.contains('0x7f.0.0.2')
6467
.export(module)
6568

6669
vows.describe('Netmask forEach')

0 commit comments

Comments
 (0)
0