Additional error checking

This commit is contained in:
Jan Petykiewicz 2020-04-18 15:35:29 -07:00
parent 62fca39a69
commit 06de10062d
2 changed files with 16 additions and 0 deletions

View File

@ -1826,6 +1826,9 @@ def read_interval(stream: io.BufferedIOBase) -> Tuple[Optional[int], Optional[in
`(lower, upper)`, where `(lower, upper)`, where
`lower` can be `None` if there is an implicit lower bound of `0` `lower` can be `None` if there is an implicit lower bound of `0`
`upper` can be `None` if there is no upper bound (`inf`) `upper` can be `None` if there is no upper bound (`inf`)
Raises:
InvalidDataError: On malformed data.
""" """
interval_type = read_uint(stream) interval_type = read_uint(stream)
if interval_type == 0: if interval_type == 0:
@ -1839,6 +1842,8 @@ def read_interval(stream: io.BufferedIOBase) -> Tuple[Optional[int], Optional[in
return v, v return v, v
elif interval_type == 4: elif interval_type == 4:
return read_uint(stream), read_uint(stream) return read_uint(stream), read_uint(stream)
else:
raise InvalidDataError('Unrecognized interval type: {}'.format(interval_type))
def write_interval(stream: io.BufferedIOBase, def write_interval(stream: io.BufferedIOBase,
@ -2151,13 +2156,22 @@ class Validation:
Returns: Returns:
Number of bytes written. Number of bytes written.
Raises:
InvalidDataError: if the checksum type can't be handled.
""" """
if self.checksum_type == 0: if self.checksum_type == 0:
return write_uint(stream, 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: elif self.checksum_type == 1:
return write_uint(stream, 1) + write_u32(stream, self.checksum) return write_uint(stream, 1) + write_u32(stream, self.checksum)
elif self.checksum_type == 2: elif self.checksum_type == 2:
return write_uint(stream, 2) + write_u32(stream, self.checksum) return write_uint(stream, 2) + write_u32(stream, self.checksum)
else:
raise InvalidDataError('Unrecognized checksum type: '
'{}'.format(self.checksum_type))
def __repr__(self) -> str: def __repr__(self) -> str:
return 'Validation(type: {} sum: {})'.format(self.checksum_type, self.checksum) return 'Validation(type: {} sum: {})'.format(self.checksum_type, self.checksum)

View File

@ -1865,6 +1865,8 @@ class Path(Record):
return PathExtensionScheme.HalfWidth, None return PathExtensionScheme.HalfWidth, None
elif ext_scheme == 3: elif ext_scheme == 3:
return PathExtensionScheme.Arbitrary, read_sint(stream) return PathExtensionScheme.Arbitrary, read_sint(stream)
else:
raise InvalidDataError('Invalid ext_scheme: {}'.format(ext_scheme))
optional['extension_start'] = get_pathext(scheme_start) optional['extension_start'] = get_pathext(scheme_start)
optional['extension_end'] = get_pathext(scheme_end) optional['extension_end'] = get_pathext(scheme_end)