Skip to content

Handle xglob errors in olemeta/olemap/oletimes instead of crashing - #900

Open
qianeric-backup wants to merge 1 commit into
decalage2:masterfrom
qianeric-backup:fix/xglob-errors-olemeta-olemap-oletimes
Open

qianeric-backup wants to merge 1 commit into
decalage2:masterfrom
qianeric-backup:fix/xglob-errors-olemeta-olemap-oletimes

Conversation

@qianeric-backup

Copy link
Copy Markdown

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:

    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.

`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`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant