Skip to content

Commit 3bad165

Browse files
authored
[3.6] bpo-31178: Avoid concatenating bytes with str in subprocess error (pythonGH-3066) (python#3388)
Avoid concatenating bytes with str in the typically rare subprocess error path (exec failed). Includes a mock based unittest to exercise the codepath. (cherry picked from commit 3fc499b)
1 parent 3aea3c2 commit 3bad165

File tree

3 files changed

+54
-3
lines changed

3 files changed

+54
-3
lines changed

Lib/subprocess.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -1314,15 +1314,18 @@ def _execute_child(self, args, executable, preexec_fn, close_fds,
13141314
try:
13151315
exception_name, hex_errno, err_msg = (
13161316
errpipe_data.split(b':', 2))
1317+
# The encoding here should match the encoding
1318+
# written in by the subprocess implementations
1319+
# like _posixsubprocess
1320+
err_msg = err_msg.decode()
13171321
except ValueError:
13181322
exception_name = b'SubprocessError'
13191323
hex_errno = b'0'
1320-
err_msg = (b'Bad exception data from child: ' +
1321-
repr(errpipe_data))
1324+
err_msg = 'Bad exception data from child: {!r}'.format(
1325+
bytes(errpipe_data))
13221326
child_exception_type = getattr(
13231327
builtins, exception_name.decode('ascii'),
13241328
SubprocessError)
1325-
err_msg = err_msg.decode(errors="surrogatepass")
13261329
if issubclass(child_exception_type, OSError) and hex_errno:
13271330
errno_num = int(hex_errno, 16)
13281331
child_exec_never_called = (err_msg == "noexec")

Lib/test/test_subprocess.py

+47
Original file line numberDiff line numberDiff line change
@@ -1545,6 +1545,53 @@ def test_exception_bad_args_0(self):
15451545
else:
15461546
self.fail("Expected OSError: %s" % desired_exception)
15471547

1548+
# We mock the __del__ method for Popen in the next two tests
1549+
# because it does cleanup based on the pid returned by fork_exec
1550+
# along with issuing a resource warning if it still exists. Since
1551+
# we don't actually spawn a process in these tests we can forego
1552+
# the destructor. An alternative would be to set _child_created to
1553+
# False before the destructor is called but there is no easy way
1554+
# to do that
1555+
class PopenNoDestructor(subprocess.Popen):
1556+
def __del__(self):
1557+
pass
1558+
1559+
@mock.patch("subprocess._posixsubprocess.fork_exec")
1560+
def test_exception_errpipe_normal(self, fork_exec):
1561+
"""Test error passing done through errpipe_write in the good case"""
1562+
def proper_error(*args):
1563+
errpipe_write = args[13]
1564+
# Write the hex for the error code EISDIR: 'is a directory'
1565+
err_code = '{:x}'.format(errno.EISDIR).encode()
1566+
os.write(errpipe_write, b"OSError:" + err_code + b":")
1567+
return 0
1568+
1569+
fork_exec.side_effect = proper_error
1570+
1571+
with self.assertRaises(IsADirectoryError):
1572+
self.PopenNoDestructor(["non_existent_command"])
1573+
1574+
@mock.patch("subprocess._posixsubprocess.fork_exec")
1575+
def test_exception_errpipe_bad_data(self, fork_exec):
1576+
"""Test error passing done through errpipe_write where its not
1577+
in the expected format"""
1578+
error_data = b"\xFF\x00\xDE\xAD"
1579+
def bad_error(*args):
1580+
errpipe_write = args[13]
1581+
# Anything can be in the pipe, no assumptions should
1582+
# be made about its encoding, so we'll write some
1583+
# arbitrary hex bytes to test it out
1584+
os.write(errpipe_write, error_data)
1585+
return 0
1586+
1587+
fork_exec.side_effect = bad_error
1588+
1589+
with self.assertRaises(subprocess.SubprocessError) as e:
1590+
self.PopenNoDestructor(["non_existent_command"])
1591+
1592+
self.assertIn(repr(error_data), str(e.exception))
1593+
1594+
15481595
def test_restore_signals(self):
15491596
# Code coverage for both values of restore_signals to make sure it
15501597
# at least does not blow up.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix string concatenation bug in rare error path in the subprocess module

0 commit comments

Comments
 (0)