Skip to content

Commit d0a5b1c

Browse files
committed
Fixes python#23564: Fix a partially broken sanity check in the _posixsubprocess
internals regarding how fds_to_pass were passed to the child. The bug had no actual impact as subprocess.py already avoided it.
1 parent 2cd1b3b commit d0a5b1c

3 files changed

Lines changed: 34 additions & 3 deletions

File tree

Lib/test/test_subprocess.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2312,8 +2312,6 @@ def test_fork_exec(self):
23122312
func = lambda: None
23132313
gc.enable()
23142314

2315-
executable_list = "exec" # error: must be a sequence
2316-
23172315
for args, exe_list, cwd, env_list in (
23182316
(123, [b"exe"], None, [b"env"]),
23192317
([b"arg"], 123, None, [b"env"]),
@@ -2331,6 +2329,34 @@ def test_fork_exec(self):
23312329
if not gc_enabled:
23322330
gc.disable()
23332331

2332+
@support.cpython_only
2333+
def test_fork_exec_sorted_fd_sanity_check(self):
2334+
# Issue #23564: sanity check the fork_exec() fds_to_keep sanity check.
2335+
import _posixsubprocess
2336+
gc_enabled = gc.isenabled()
2337+
try:
2338+
gc.enable()
2339+
2340+
for fds_to_keep in (
2341+
(-1, 2, 3, 4, 5), # Negative number.
2342+
('str', 4), # Not an int.
2343+
(18, 23, 42, 2**63), # Out of range.
2344+
(5, 4), # Not sorted.
2345+
(6, 7, 7, 8), # Duplicate.
2346+
):
2347+
with self.assertRaises(
2348+
ValueError,
2349+
msg='fds_to_keep={}'.format(fds_to_keep)) as c:
2350+
_posixsubprocess.fork_exec(
2351+
[b"false"], [b"false"],
2352+
True, fds_to_keep, None, [b"env"],
2353+
-1, -1, -1, -1,
2354+
1, 2, 3, 4,
2355+
True, True, None)
2356+
self.assertIn('fds_to_keep', str(c.exception))
2357+
finally:
2358+
if not gc_enabled:
2359+
gc.disable()
23342360

23352361

23362362
@unittest.skipUnless(mswindows, "Windows specific tests")

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ Release date: TBA
1111
Core and Builtins
1212
-----------------
1313

14+
- Issue #23564: Fixed a partially broken sanity check in the _posixsubprocess
15+
internals regarding how fds_to_pass were passed to the child. The bug had
16+
no actual impact as subprocess.py already avoided it.
17+
1418
- Issue #25388: Fixed tokenizer crash when processing undecodable source code
1519
with a null byte.
1620

Modules/_posixsubprocess.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,11 @@ _sanity_check_python_fd_sequence(PyObject *fd_sequence)
109109
for (seq_idx = 0; seq_idx < seq_len; ++seq_idx) {
110110
PyObject* py_fd = PySequence_Fast_GET_ITEM(fd_sequence, seq_idx);
111111
long iter_fd = PyLong_AsLong(py_fd);
112-
if (iter_fd < 0 || iter_fd < prev_fd || iter_fd > INT_MAX) {
112+
if (iter_fd < 0 || iter_fd <= prev_fd || iter_fd > INT_MAX) {
113113
/* Negative, overflow, not a Long, unsorted, too big for a fd. */
114114
return 1;
115115
}
116+
prev_fd = iter_fd;
116117
}
117118
return 0;
118119
}

0 commit comments

Comments
 (0)