Skip to content

Commit 285fe5c

Browse files
committed
validate_netmask: ensure 32 bit expansion
Ensure that the bit string representation of a netmask is the full 32 bits before validating the left most bits are 1s. See #18
1 parent 33954e7 commit 285fe5c

1 file changed

Lines changed: 6 additions & 1 deletion

File tree

iptools/ipv4.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,10 @@ def validate_netmask(s):
285285
True
286286
>>> validate_netmask('128.0.0.1')
287287
False
288+
>>> validate_netmask('1.255.255.0')
289+
False
290+
>>> validate_netmask('0.255.255.0')
291+
False
288292
289293
290294
:param s: String to validate as a dotted-quad notation netmask.
@@ -293,7 +297,8 @@ def validate_netmask(s):
293297
:raises: TypeError
294298
"""
295299
if validate_ip(s):
296-
mask = bin(ip2network(s))[2:]
300+
# Convert to binary string, strip '0b' prefix, 0 pad to 32 bits
301+
mask = bin(ip2network(s))[2:].zfill(32)
297302
# all left most bits must be 1, all right most must be 0
298303
seen0 = False
299304
for c in mask:

0 commit comments

Comments
 (0)