Skip to content

Commit

Permalink
Fix bitwise hex count values and array of zero
Browse files Browse the repository at this point in the history
In the parser "count" has always allowed hex values (0x12) but we
would not actually interpret those properly. Also, arrays of zero
are useful sometimes so add a test to make sure those work (with hex
counts only to make them stand out as intentional).
  • Loading branch information
kk7ds committed Jan 29, 2025
1 parent ce8c16b commit 4bceef2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
11 changes: 9 additions & 2 deletions chirp/bitwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/test_bitwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 4bceef2

Please sign in to comment.