Skip to content

Commit d69bef6

Browse files
[3.11] gh-116404: Handle errors correctly in wait_helper in posixmodule (GH-116405) (#116407)
gh-116404: Handle errors correctly in `wait_helper` in `posixmodule` (GH-116405) (cherry picked from commit 22ccf13) Co-authored-by: Nikita Sobolev <[email protected]>
1 parent 23c17f3 commit d69bef6

File tree

1 file changed

+29
-26
lines changed

1 file changed

+29
-26
lines changed

Modules/posixmodule.c

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8392,36 +8392,39 @@ wait_helper(PyObject *module, pid_t pid, int status, struct rusage *ru)
83928392
if (!result)
83938393
return NULL;
83948394

8395+
int pos = 0;
8396+
83958397
#ifndef doubletime
83968398
#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
83978399
#endif
83988400

8399-
PyStructSequence_SET_ITEM(result, 0,
8400-
PyFloat_FromDouble(doubletime(ru->ru_utime)));
8401-
PyStructSequence_SET_ITEM(result, 1,
8402-
PyFloat_FromDouble(doubletime(ru->ru_stime)));
8403-
#define SET_INT(result, index, value)\
8404-
PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
8405-
SET_INT(result, 2, ru->ru_maxrss);
8406-
SET_INT(result, 3, ru->ru_ixrss);
8407-
SET_INT(result, 4, ru->ru_idrss);
8408-
SET_INT(result, 5, ru->ru_isrss);
8409-
SET_INT(result, 6, ru->ru_minflt);
8410-
SET_INT(result, 7, ru->ru_majflt);
8411-
SET_INT(result, 8, ru->ru_nswap);
8412-
SET_INT(result, 9, ru->ru_inblock);
8413-
SET_INT(result, 10, ru->ru_oublock);
8414-
SET_INT(result, 11, ru->ru_msgsnd);
8415-
SET_INT(result, 12, ru->ru_msgrcv);
8416-
SET_INT(result, 13, ru->ru_nsignals);
8417-
SET_INT(result, 14, ru->ru_nvcsw);
8418-
SET_INT(result, 15, ru->ru_nivcsw);
8419-
#undef SET_INT
8420-
8421-
if (PyErr_Occurred()) {
8422-
Py_DECREF(result);
8423-
return NULL;
8424-
}
8401+
#define SET_RESULT(CALL) \
8402+
do { \
8403+
PyObject *item = (CALL); \
8404+
if (item == NULL) { \
8405+
Py_DECREF(result); \
8406+
return NULL; \
8407+
} \
8408+
PyStructSequence_SET_ITEM(result, pos++, item); \
8409+
} while(0)
8410+
8411+
SET_RESULT(PyFloat_FromDouble(doubletime(ru->ru_utime)));
8412+
SET_RESULT(PyFloat_FromDouble(doubletime(ru->ru_stime)));
8413+
SET_RESULT(PyLong_FromLong(ru->ru_maxrss));
8414+
SET_RESULT(PyLong_FromLong(ru->ru_ixrss));
8415+
SET_RESULT(PyLong_FromLong(ru->ru_idrss));
8416+
SET_RESULT(PyLong_FromLong(ru->ru_isrss));
8417+
SET_RESULT(PyLong_FromLong(ru->ru_minflt));
8418+
SET_RESULT(PyLong_FromLong(ru->ru_majflt));
8419+
SET_RESULT(PyLong_FromLong(ru->ru_nswap));
8420+
SET_RESULT(PyLong_FromLong(ru->ru_inblock));
8421+
SET_RESULT(PyLong_FromLong(ru->ru_oublock));
8422+
SET_RESULT(PyLong_FromLong(ru->ru_msgsnd));
8423+
SET_RESULT(PyLong_FromLong(ru->ru_msgrcv));
8424+
SET_RESULT(PyLong_FromLong(ru->ru_nsignals));
8425+
SET_RESULT(PyLong_FromLong(ru->ru_nvcsw));
8426+
SET_RESULT(PyLong_FromLong(ru->ru_nivcsw));
8427+
#undef SET_RESULT
84258428

84268429
return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
84278430
}

0 commit comments

Comments
 (0)