diff --git a/jc/parsers/ifconfig.py b/jc/parsers/ifconfig.py index 021f5026..23d67fdc 100644 --- a/jc/parsers/ifconfig.py +++ b/jc/parsers/ifconfig.py @@ -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): diff --git a/tests/test_ifconfig.py b/tests/test_ifconfig.py index 9f9953b4..785a9807 100644 --- a/tests/test_ifconfig.py +++ b/tests/test_ifconfig.py @@ -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 mtu 16384\n' + '\toptions=1203\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)