Skip to content

Commit

Permalink
Fix improper parsing of hex bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
rs committed Mar 19, 2021
1 parent 4678fd8 commit ec1b5b5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/netmask.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ ip2long = (ip) ->
if b.length is 0 or b.length > 4 then throw new Error('Invalid IP')
for byte, i in b
if byte and byte[0] == '0'
# make sure 0 prefixed bytes are parsed as octal
byte = parseInt(byte, 8)
if byte.length > 2 and (byte[1] == 'x' or byte[1] == 'x')
# make sure 0x prefixed bytes are parsed as hex
byte = parseInt(byte, 16)
else
# make sure 0 prefixed bytes are parsed as octal
byte = parseInt(byte, 8)
else
byte = parseInt(byte, 10)
if isNaN(byte) then throw new Error("Invalid byte: #{byte}")
Expand Down
3 changes: 3 additions & 0 deletions test/netmasks.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,13 @@ vows.describe('Netmask contains IP')
topic: -> new Netmask('31.0.0.0/8')
'contains IP 31.5.5.5': (block) -> assert.ok block.contains('31.5.5.5')
'does not contain IP 031.5.5.5 (25.5.5.5)': (block) -> assert.ok not block.contains('031.5.5.5')
'does not contain IP 0x31.5.5.5 (49.5.5.5)': (block) -> assert.ok not block.contains('0x31.5.5.5')
'does not contain IP 0X31.5.5.5 (49.5.5.5)': (block) -> assert.ok not block.contains('0X31.5.5.5')
'block 127.0.0.0/8':
topic: -> new Netmask('127.0.0.0/8')
'contains IP 127.0.0.2': (block) -> assert.ok block.contains('127.0.0.2')
'contains IP 0177.0.0.2 (127.0.0.2)': (block) -> assert.ok block.contains('0177.0.0.2')
'contains IP 0x7f.0.0.2 (127.0.0.2)': (block) -> assert.ok block.contains('0x7f.0.0.2')
.export(module)

vows.describe('Netmask forEach')
Expand Down

0 comments on commit ec1b5b5

Please sign in to comment.