Skip to content

Commit

Permalink
Graceful error handling of blank line in TI-TXT.
Browse files Browse the repository at this point in the history
  • Loading branch information
eerimoq committed Oct 4, 2018
1 parent 0709ad9 commit ce37abc
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
9 changes: 6 additions & 3 deletions bincopy.py
Original file line number Diff line number Diff line change
Expand Up @@ -813,19 +813,22 @@ def add_ti_txt(self, records, overwrite=False):
if eof_found:
raise Error("bad file terminator")

record = record.strip()

if len(record) < 1:
raise Error("bad record length")

if record[0] == 'q':
# EOL found.
eof_found = True
elif record[0] == '@':
# Section address found.
try:
address = int(record[1:], 16)
except ValueError:
raise Error("bad section address")
else:
# Try to decode the data.
try:
data = bytearray(binascii.unhexlify(record.strip().replace(' ', '')))
data = bytearray(binascii.unhexlify(record.replace(' ', '')))
except (TypeError, binascii.Error):
raise Error("bad data")

Expand Down
7 changes: 7 additions & 0 deletions tests/files/bad_ti_txt_blank_line.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@0100
21 46 01 36 01 21 47 01 36 00 7E FE 09 D2 19 01

21 46 01 7E 17 C2 00 01 FF 5F 16 XX 21 48 01 19
19 4E 79 23 46 23 96 57 78 23 9E DA 3F 01 B2 CA
3F 01 56 70 2B 5E 71 2B 72 2B 73 21 46 01 34 21
q
3 changes: 2 additions & 1 deletion tests/test_bincopy.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ def test_bad_ti_txt(self):
('bad_ti_txt_record_short.txt', 'missing section address'),
('bad_ti_txt_record_long.txt', 'bad record length'),
('bad_ti_txt_no_offset.txt', 'missing section address'),
('bad_ti_txt_no_q.txt', 'missing file terminator')
('bad_ti_txt_no_q.txt', 'missing file terminator'),
('bad_ti_txt_blank_line.txt', 'bad record length')
]

for filename, message in datas:
Expand Down

0 comments on commit ce37abc

Please sign in to comment.