Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Lib/test/test_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -2472,6 +2472,22 @@ def test_waitpid(self):
status = os.waitpid(pid, 0)
self.assertEqual(status, (pid, 0))

@unittest.skipUnless(hasattr(os, 'wait3'), "os.wait3() is required")
def test_wait3_rusage_initialized(self):
# Ensure a successful wait3() call where no child was ready to report
# its exit status does not return uninitialized memory in the rusage
# structure. See bpo-36279.
args = [sys.executable, '-c', 'import sys; sys.stdin.read()']
proc = subprocess.Popen(args, stdin=subprocess.PIPE)
try:
pid, status, rusage = os.wait3(os.WNOHANG)
self.assertEqual(0, pid)
self.assertEqual(0, status)
self.assertEqual(0, sum(rusage))
finally:
proc.stdin.close()
proc.wait()


class SpawnTests(unittest.TestCase):
def create_args(self, *, with_env=False, use_bytes=False):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The :func:`os.wait3` function could return uninitialized memory if the system
call succeeded, but no child process exit status was reported.
6 changes: 6 additions & 0 deletions Modules/posixmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -7354,6 +7354,12 @@ wait_helper(pid_t pid, int status, struct rusage *ru)
if (pid == -1)
return posix_error();

/* If wait succeeded but no child was ready to report status, ru will not
* have been populated. Clear it to avoid returning uninitialized stack to
* the caller. */
if (pid == 0)
memset(ru, 0, sizeof(*ru));

if (struct_rusage == NULL) {
PyObject *m = PyImport_ImportModuleNoBlock("resource");
if (m == NULL)
Expand Down