From 2f9377cb67734e5c3d200e373a9cc46e47b02934 Mon Sep 17 00:00:00 2001 From: Alex Towell Date: Wed, 25 Mar 2026 03:12:34 -0500 Subject: [PATCH] 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) --- jc/parsers/ifconfig.py | 4 ++-- tests/test_ifconfig.py | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) 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)