Skip to content

Commit c82ac89

Browse files
authored
Check for EOF when parsing enums in a .sym file. Fixes #465 (#468)
* Check for EOF when parsing enums in a .sym file. Fixes #465 * Fix test to check for different exception in the python2.7 case
1 parent 1808290 commit c82ac89

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

src/canmatrix/formats/sym.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -354,9 +354,12 @@ class Mode(object):
354354
while not line[5:].strip().endswith(')'):
355355
line = line.split('//')[0]
356356
if sys.version_info > (3, 0): # is there a clean way to to it?
357-
line += ' ' + f.readline().decode(sym_import_encoding).strip()
357+
next_line = f.readline().decode(sym_import_encoding)
358358
else:
359-
line += ' ' + next(f).decode(sym_import_encoding).strip()
359+
next_line = next(f).decode(sym_import_encoding)
360+
if next_line is "":
361+
raise EOFError("Reached EOF before finding terminator for enum :\"{}\"".format(line))
362+
line += next_line.strip()
360363
line = line.split('//')[0]
361364
temp_array = line[5:].strip().rstrip(')').split('(', 1)
362365
val_table_name = temp_array[0]

src/canmatrix/tests/test_sym.py

+32
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# -*- coding: utf-8 -*-
22
import io
3+
import sys
34
import textwrap
45

56
import pytest
@@ -141,3 +142,34 @@ def tests_parse_float(variable_type, bit_length):
141142
frame = matrix.frames[0]
142143
signal = frame.signals[0]
143144
assert signal.is_float
145+
146+
def test_unterminated_enum():
147+
f = io.BytesIO(
148+
textwrap.dedent(
149+
'''\
150+
FormatVersion=5.0 // Do not edit this line!
151+
Title="Untitled
152+
153+
{ENUMS}
154+
enum Categories(0="Animal", 1="Vegetable", 3="Mineral"
155+
156+
{SENDRECEIVE}
157+
158+
[Symbol1]
159+
ID=000h
160+
DLC=8
161+
Var=Signal unsigned 0,16
162+
163+
'''
164+
).encode('utf-8'),
165+
)
166+
# Missing ')' at the end of enum used to cause infinite loop
167+
168+
matrix = canmatrix.formats.sym.load(f)
169+
170+
assert len(matrix.load_errors) == 1
171+
if sys.version_info > (3, 0):
172+
assert isinstance(matrix.load_errors[0], EOFError)
173+
else:
174+
assert isinstance(matrix.load_errors[0], StopIteration)
175+

0 commit comments

Comments
 (0)