diff --git a/chirp/bitwise.py b/chirp/bitwise.py index 723675f7..be892c2e 100644 --- a/chirp/bitwise.py +++ b/chirp/bitwise.py @@ -854,6 +854,13 @@ def items(self): yield key, self._generators[key] +def parse_count(string): + if string.startswith('0x'): + return int(string, 16) + else: + return int(string) + + class Processor: _types = { "u8": u8DataElement, @@ -956,7 +963,7 @@ def parse_defn(self, defn): else: if defn[1][0] == "array": sym = defn[1][1][0] - count = int(defn[1][1][1][1]) + count = parse_count(defn[1][1][1][1]) else: count = 1 sym = defn[1] @@ -997,7 +1004,7 @@ def parse_struct_decl(self, struct): deftype = struct[-1] if deftype[0] == "array": name = deftype[1][0][1] - count = int(deftype[1][1][1]) + count = parse_count(deftype[1][1][1]) elif deftype[0] == "symbol": name = deftype[1] count = 1 diff --git a/tests/unit/test_bitwise.py b/tests/unit/test_bitwise.py index f44b99b5..6da0ea11 100644 --- a/tests/unit/test_bitwise.py +++ b/tests/unit/test_bitwise.py @@ -104,6 +104,14 @@ def test_int_array_of_one(self): obj.foo[0] = 1 self.assertEqual(b'\x01\x01\x02\x03', data.get_packed()) + def test_int_array_of_zero(self): + data = memmap.MemoryMapBytes(bytes(b'\x00\x01\x02\x03')) + obj = bitwise.parse('u8 foo[0x0]; u8 bar;', data) + # Make sure we can't access any elements of zero-length foo + self.assertRaises(IndexError, lambda: obj.foo[0]) + # Make sure bar is still at the front, unaffected by zero-length foo + self.assertEqual(0, obj.bar) + def test_int_array_set_raw(self): data = memmap.MemoryMapBytes(bytes(b'\x00\x01\x02\x03')) obj = bitwise.parse('u8 foo[4];', data)