1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2026-04-05 17:50:11 +02:00

Fix ifconfig hex mask conversion using lstrip('0x') instead of [2:]

lstrip('0x') strips any character in {'0','x'} from the left, not the
literal prefix "0x". This causes incorrect mask conversion for masks
with leading zero hex digits (e.g. 0x00000000 -> empty string instead
of 0.0.0.0).

Replace lstrip('0x') with [2:] to correctly remove only the '0x'
prefix. Fixes both the legacy ipv4_mask field and the ipv4[] list.

Fixes #685

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Alex Towell
2026-03-25 03:12:34 -05:00
parent 585ff83a2e
commit 2f9377cb67
2 changed files with 17 additions and 2 deletions

View File

@@ -264,7 +264,7 @@ def _process(proc_data: List[JSONDictType]) -> List[JSONDictType]:
try:
if entry['ipv4_mask'].startswith('0x'):
new_mask = entry['ipv4_mask']
new_mask = new_mask.lstrip('0x')
new_mask = new_mask[2:]
new_mask = '.'.join(str(int(i, 16)) for i in [new_mask[i:i + 2] for i in range(0, len(new_mask), 2)])
entry['ipv4_mask'] = new_mask
except (ValueError, TypeError, AttributeError):
@@ -289,7 +289,7 @@ def _process(proc_data: List[JSONDictType]) -> List[JSONDictType]:
try:
if ip_address['mask'].startswith('0x'):
new_mask = ip_address['mask']
new_mask = new_mask.lstrip('0x')
new_mask = new_mask[2:]
new_mask = '.'.join(str(int(i, 16)) for i in [new_mask[i:i + 2] for i in range(0, len(new_mask), 2)])
ip_address['mask'] = new_mask
except (ValueError, TypeError, AttributeError):

View File

@@ -148,6 +148,21 @@ class MyTests(unittest.TestCase):
"""
self.assertEqual(jc.parsers.ifconfig.parse(self.osx_freebsd12_ifconfig_extra_fields4, quiet=True), self.freebsd12_ifconfig_extra_fields4_json)
def test_ifconfig_hex_mask_all_zeros(self):
"""
Test 'ifconfig' with 0x00000000 netmask (FreeBSD/macOS hex format).
Regression test: lstrip('0x') incorrectly strips leading '0' chars
from the hex digits, producing wrong mask for all-zero masks.
"""
data = (
'lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384\n'
'\toptions=1203<RXCSUM,TXCSUM,TXSTATUS,SW_TIMESTAMP>\n'
'\tinet 192.168.1.1 netmask 0x00000000\n'
)
result = jc.parsers.ifconfig.parse(data, quiet=True)
self.assertEqual(result[0]['ipv4_mask'], '0.0.0.0')
self.assertEqual(result[0]['ipv4'][0]['mask'], '0.0.0.0')
def test_ifconfig_utun_ipv4(self):
"""
Test 'ifconfig' with ipv4 utun addresses (macOS)