[issue43423] Subprocess IndexError possible in _communicate

2021-03-06 Thread Chris Griffith


Change by Chris Griffith :


--
keywords: +patch
pull_requests: +23542
stage:  -> patch review
pull_request: https://github.com/python/cpython/pull/24777

___
Python tracker 
<https://bugs.python.org/issue43423>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43423] Subprocess IndexError possible in _communicate

2021-03-06 Thread Chris Griffith


New submission from Chris Griffith :

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 
<https://bugs.python.org/issue43423>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com