Repository navigation
Handle xglob errors in olemeta/olemap/oletimes instead of crashing - #900
Open
qianeric-backup wants to merge 1 commit into
Open
qianeric-backup wants to merge 1 commit into
qianeric-backup wants to merge 1 commit into
Conversation
`xglob.iter_files()` yields `(container, filename, data)` where `data` is an
**exception instance** when the file cannot be read — `PathNotFoundException` for a
missing path, other exceptions for unreadable zip members. `olevba` handles this
explicitly (`isinstance(data, PathNotFoundException)` -> `log.error('Given path %r
does not exist!' % filename)`), but the three "inspect one file" tools do not:
```
$ olemeta /nope.doc
Traceback (most recent call last):
File ".../olemeta.py", line 170, in <module>
ole = olefile.OleFileIO(data)
File ".../olefile/olefile.py", line 1231, in open
self.fp = open(filename, mode)
TypeError: expected str, bytes or os.PathLike object, not PathNotFoundException
$ olemap /nope.doc # same
$ oletimes /nope.doc # same
```
Each of the three files even carries the marker `# TODO: handle xglob errors` right
above the loop, so the gap is already documented in-tree.
## Change
The `TODO` in `olemeta.py`, `olemap.py` and `oletimes.py` is replaced with the same
guard `olevba` uses, plus a non-zero exit so scripts can tell the difference:
```python
error_count = 0
for container, filename, data in xglob.iter_files(...):
# handle errors coming from xglob: data is an exception instance, not
# file content, so it must not be handed over to olefile
if isinstance(data, Exception):
if isinstance(data, PathNotFoundException):
sys.stderr.write('ERROR: Given path %r does not exist!\n' % filename)
else:
sys.stderr.write('ERROR: Exception opening/reading %r: %s\n' % (filename, data))
error_count += 1
continue
...
if error_count:
sys.exit(1)
```
| command | before | after |
|---|---|---|
| `olemeta /nope.doc` | `TypeError: expected str, bytes or os.PathLike object, not PathNotFoundException` | `ERROR: Given path '/nope.doc' does not exist!`, exit 1 |
| `olemap /nope.doc` | same traceback | same message, exit 1 |
| `oletimes /nope.doc` | same traceback | same message, exit 1 |
| `olemeta real.doc` | works | unchanged, exit 0 |
| unreadable zip member | traceback | `ERROR: Exception opening/reading ...`, exit 1 |
## Verification
```
$ olemeta /nope.doc ; echo $?
ERROR: Given path '/nope.doc' does not exist!
1 # TypeError traceback, exit 1 before the change
$ oletimes /nope.doc ; echo $? # ditto
$ olemap /nope.doc ; echo $? # ditto
$ olemeta tests/test-data/msodde/harmless-clean.doc ; echo $?
+---------------------+------------- ... # table as before
0
```
`olevba`, `rtfobj`, `ftguess` and `oleid` have their own variants of this loop (no
`TODO` marker, different code paths); they still traceback on a missing path. I left
them out to keep this reviewable — say the word and I will send the same guard for
`ftguess.process_file()` and `oleid`.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
xglob.iter_files()yields(container, filename, data)wheredatais anexception instance when the file cannot be read —
PathNotFoundExceptionfor amissing path, other exceptions for unreadable zip members.
olevbahandles thisexplicitly (
isinstance(data, PathNotFoundException)->log.error('Given path %r does not exist!' % filename)), but the three "inspect one file" tools do not:Each of the three files even carries the marker
# TODO: handle xglob errorsrightabove the loop, so the gap is already documented in-tree.
Change
The
TODOinolemeta.py,olemap.pyandoletimes.pyis replaced with the sameguard
olevbauses, plus a non-zero exit so scripts can tell the difference:olemeta /nope.docTypeError: expected str, bytes or os.PathLike object, not PathNotFoundExceptionERROR: Given path '/nope.doc' does not exist!, exit 1olemap /nope.docoletimes /nope.docolemeta real.docERROR: Exception opening/reading ..., exit 1Verification
olevba,rtfobj,ftguessandoleidhave their own variants of this loop (noTODOmarker, different code paths); they still traceback on a missing path. I leftthem out to keep this reviewable — say the word and I will send the same guard for
ftguess.process_file()andoleid.