Skip to content

Commit

Permalink
Check for EOF when parsing enums in a .sym file. Fixes ebroecker#465
Browse files Browse the repository at this point in the history
  • Loading branch information
Seneda committed Apr 7, 2020
1 parent 1808290 commit cb6d171
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/canmatrix/formats/sym.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,9 +354,12 @@ class Mode(object):
while not line[5:].strip().endswith(')'):
line = line.split('//')[0]
if sys.version_info > (3, 0): # is there a clean way to to it?
line += ' ' + f.readline().decode(sym_import_encoding).strip()
next_line = f.readline().decode(sym_import_encoding)
else:
line += ' ' + next(f).decode(sym_import_encoding).strip()
next_line = next(f).decode(sym_import_encoding)
if next_line is "":
raise EOFError("Reached EOF before finding terminator for enum :\"{}\"".format(line))
line += next_line.strip()
line = line.split('//')[0]
temp_array = line[5:].strip().rstrip(')').split('(', 1)
val_table_name = temp_array[0]
Expand Down
29 changes: 29 additions & 0 deletions src/canmatrix/tests/test_sym.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,32 @@ def tests_parse_float(variable_type, bit_length):
frame = matrix.frames[0]
signal = frame.signals[0]
assert signal.is_float

def test_unterminated_enum():
f = io.BytesIO(
textwrap.dedent(
'''\
FormatVersion=5.0 // Do not edit this line!
Title="Untitled
{ENUMS}
enum Categories(0="Animal", 1="Vegetable", 3="Mineral"
{SENDRECEIVE}
[Symbol1]
ID=000h
DLC=8
Var=Signal unsigned 0,16
'''
).encode('utf-8'),
)
# Missing ')' at the end of enum used to cause infinite loop

matrix = canmatrix.formats.sym.load(f)

assert len(matrix.load_errors) == 1

assert isinstance(matrix.load_errors[0], EOFError)

0 comments on commit cb6d171

Please sign in to comment.