From 06de10062d3e015af2124e2a25ccf42d1790334f Mon Sep 17 00:00:00 2001 From: Jan Petykiewicz Date: Sat, 18 Apr 2020 15:35:29 -0700 Subject: [PATCH] Additional error checking --- fatamorgana/basic.py | 14 ++++++++++++++ fatamorgana/records.py | 2 ++ 2 files changed, 16 insertions(+) diff --git a/fatamorgana/basic.py b/fatamorgana/basic.py index 9861397..13cf703 100644 --- a/fatamorgana/basic.py +++ b/fatamorgana/basic.py @@ -1826,6 +1826,9 @@ def read_interval(stream: io.BufferedIOBase) -> Tuple[Optional[int], Optional[in `(lower, upper)`, where `lower` can be `None` if there is an implicit lower bound of `0` `upper` can be `None` if there is no upper bound (`inf`) + + Raises: + InvalidDataError: On malformed data. """ interval_type = read_uint(stream) if interval_type == 0: @@ -1839,6 +1842,8 @@ def read_interval(stream: io.BufferedIOBase) -> Tuple[Optional[int], Optional[in return v, v elif interval_type == 4: return read_uint(stream), read_uint(stream) + else: + raise InvalidDataError('Unrecognized interval type: {}'.format(interval_type)) def write_interval(stream: io.BufferedIOBase, @@ -2151,13 +2156,22 @@ class Validation: Returns: Number of bytes written. + + Raises: + InvalidDataError: if the checksum type can't be handled. """ if self.checksum_type == 0: return write_uint(stream, 0) + elif self.checksum is None: + raise InvalidDataError('Checksum is empty but type is ' + '{}'.format(self.checksum_type)) elif self.checksum_type == 1: return write_uint(stream, 1) + write_u32(stream, self.checksum) elif self.checksum_type == 2: return write_uint(stream, 2) + write_u32(stream, self.checksum) + else: + raise InvalidDataError('Unrecognized checksum type: ' + '{}'.format(self.checksum_type)) def __repr__(self) -> str: return 'Validation(type: {} sum: {})'.format(self.checksum_type, self.checksum) diff --git a/fatamorgana/records.py b/fatamorgana/records.py index 3d81f14..5d8e613 100644 --- a/fatamorgana/records.py +++ b/fatamorgana/records.py @@ -1865,6 +1865,8 @@ class Path(Record): return PathExtensionScheme.HalfWidth, None elif ext_scheme == 3: return PathExtensionScheme.Arbitrary, read_sint(stream) + else: + raise InvalidDataError('Invalid ext_scheme: {}'.format(ext_scheme)) optional['extension_start'] = get_pathext(scheme_start) optional['extension_end'] = get_pathext(scheme_end)