Skip to content

Commit

Permalink
Prevent index out of bounds error when reading empty log file
Browse files Browse the repository at this point in the history
When trying to run utt hello after manually deleting all the content
in the report-file. I did this to start over.

The error comes from add_entry.py on line 26. If the report-file is
empty you can't put the cursor on position -1

```python
with open(filename, "rb+") as file:
    file.seek(-1, os.SEEK_END)  # if the file is empty, position -1 will cause OSError
   ...
```

I fixed it by catching the exception and letting the error pass,
whilst setting;

prepend_new_line = False

to pass integration-testing I also had to let errno.ENOENT
(FileNotFound) pass without raising an Exception - If the file doesn't
exist it gets created.

Issue: larose#19
  • Loading branch information
Kent Martin authored and larose committed Mar 14, 2020
1 parent 6e473a0 commit 6da0a56
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions utt/components/add_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ def _append_line_to_file(filename, line, insert_new_line_before):
file.seek(-1, os.SEEK_END)
last_char = file.read(1)
prepend_new_line = last_char != b"\n"
except OSError as os_err:
if os_err.errno not in [errno.EINVAL, errno.ENOENT]:
raise
prepend_new_line = False
except EnvironmentError as exc:
if exc.errno != errno.ENOENT:
raise
Expand Down

0 comments on commit 6da0a56

Please sign in to comment.