Skip to content

Commit 682107c

Browse files
ZackerySpytzdw
authored andcommitted
bpo-36279: Ensure os.wait3() rusage is initialized (GH-15111)
Co-Authored-By: David Wilson <[email protected]>
1 parent d91d4de commit 682107c

3 files changed

Lines changed: 25 additions & 0 deletions

File tree

‎Lib/test/test_wait3.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
"""
33

44
import os
5+
import subprocess
6+
import sys
57
import time
68
import unittest
79
from test.fork_wait import ForkWait
@@ -31,6 +33,22 @@ def wait_impl(self, cpid):
3133
self.assertEqual(status, 0, "cause = %d, exit = %d" % (status&0xff, status>>8))
3234
self.assertTrue(rusage)
3335

36+
def test_wait3_rusage_initialized(self):
37+
# Ensure a successful wait3() call where no child was ready to report
38+
# its exit status does not return uninitialized memory in the rusage
39+
# structure. See bpo-36279.
40+
args = [sys.executable, '-c', 'import sys; sys.stdin.read()']
41+
proc = subprocess.Popen(args, stdin=subprocess.PIPE)
42+
try:
43+
pid, status, rusage = os.wait3(os.WNOHANG)
44+
self.assertEqual(0, pid)
45+
self.assertEqual(0, status)
46+
self.assertEqual(0, sum(rusage))
47+
finally:
48+
proc.stdin.close()
49+
proc.wait()
50+
51+
3452
def tearDownModule():
3553
reap_children()
3654

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix potential use of uninitialized memory in :func:`os.wait3`.

‎Modules/posixmodule.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7489,6 +7489,12 @@ wait_helper(pid_t pid, int status, struct rusage *ru)
74897489
if (pid == -1)
74907490
return posix_error();
74917491

7492+
// If wait succeeded but no child was ready to report status, ru will not
7493+
// have been populated.
7494+
if (pid == 0) {
7495+
memset(ru, 0, sizeof(*ru));
7496+
}
7497+
74927498
if (struct_rusage == NULL) {
74937499
PyObject *m = PyImport_ImportModuleNoBlock("resource");
74947500
if (m == NULL)

0 commit comments

Comments
 (0)