Skip to content

Commit

Permalink
Disallow creating invalid int_ts entirely
Browse files Browse the repository at this point in the history
  • Loading branch information
puddly committed May 16, 2020
1 parent 5cdcafe commit ca54a2e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
10 changes: 9 additions & 1 deletion tests/test_types_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ def test_int_too_short():
t.uint16_t.deserialize(b"\x00")


def test_int_out_of_bounds():
with pytest.raises(ValueError):
t.uint8_t(-1)

with pytest.raises(ValueError):
t.uint8_t(0xFF + 1)


def test_bytes():
data = b"abcde\x00\xff"

Expand Down Expand Up @@ -95,7 +103,7 @@ class TestList(t.LVList, item_type=t.uint8_t, length_type=t.uint8_t):

assert isinstance(d, TestList)

with pytest.raises(OverflowError):
with pytest.raises(ValueError):
TestList([1, 2, 0xFFFF, 4]).serialize()


Expand Down
15 changes: 14 additions & 1 deletion zigpy_znp/types/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,21 @@ class int_t(int):
_signed = True
_size = None

def __new__(cls, value):
instance = int.__new__(cls, value)

if instance._signed is not None and instance._size is not None:
# It's a concrete int_t type, check to make sure it's valid
instance.serialize()

return instance

def serialize(self) -> bytes:
return self.to_bytes(self._size, "little", signed=self._signed)
try:
return self.to_bytes(self._size, "little", signed=self._signed)
except OverflowError as e:
# OverflowError is not a subclass of ValueError, making it annoying to catch
raise ValueError(str(e)) from e

@classmethod
def deserialize(cls, data: bytes) -> typing.Tuple["int_t", bytes]:
Expand Down

0 comments on commit ca54a2e

Please sign in to comment.