[New-bugs-announce] [issue43423] Subprocess IndexError possible in _communicate

Chris Griffith report at bugs.python.org
Sat Mar 6 13:52:42 EST 2021


New submission from Chris Griffith <teckchris at gmail.com>:

It is possible to run into an IndexError in the subprocess module's _communicate function.

```
    return run(
  File "subprocess.py", line 491, in run
  File "subprocess.py", line 1024, in communicate
  File "subprocess.py", line 1418, in _communicate
IndexError: list index out of range
```

The lines in question are: 

```
            if stdout is not None:
                stdout = stdout[0]
            if stderr is not None:
                stderr = stderr[0]
```

I believe this is due to the fact there is no safety checking to make sure that self._stdout_buff and self._stderr_buff have any content in them after being set to empty lists. 

The fix I suggest is to change the checks from `if stdout is not None` to simply `if stdout` to make sure it is a populated list. 

Example fixed code: 

```
            if stdout:
                stdout = stdout[0]
            if stderr:
                stderr = stderr[0]
```

If a more stringent check is required, we could expand that out to check type and length, such as `isinstance(stdout, list) and len(stdout) > 0:` but that is more then necessary currently.

----------
components: Library (Lib)
messages: 388211
nosy: cdgriffith
priority: normal
severity: normal
status: open
title: Subprocess IndexError possible in _communicate
type: crash
versions: Python 3.10, Python 3.8, Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue43423>
_______________________________________


More information about the New-bugs-announce mailing list