Skip to content

Commit 5c69f60

Browse files
[3.12] gh-116404: Handle errors correctly in wait_helper in posixmodule (GH-116405) (#116406)
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 88fdb08 commit 5c69f60

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
@@ -9245,36 +9245,39 @@ wait_helper(PyObject *module, pid_t pid, int status, struct rusage *ru)
92459245
if (!result)
92469246
return NULL;
92479247

9248+
int pos = 0;
9249+
92489250
#ifndef doubletime
92499251
#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
92509252
#endif
92519253

9252-
PyStructSequence_SET_ITEM(result, 0,
9253-
PyFloat_FromDouble(doubletime(ru->ru_utime)));
9254-
PyStructSequence_SET_ITEM(result, 1,
9255-
PyFloat_FromDouble(doubletime(ru->ru_stime)));
9256-
#define SET_INT(result, index, value)\
9257-
PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
9258-
SET_INT(result, 2, ru->ru_maxrss);
9259-
SET_INT(result, 3, ru->ru_ixrss);
9260-
SET_INT(result, 4, ru->ru_idrss);
9261-
SET_INT(result, 5, ru->ru_isrss);
9262-
SET_INT(result, 6, ru->ru_minflt);
9263-
SET_INT(result, 7, ru->ru_majflt);
9264-
SET_INT(result, 8, ru->ru_nswap);
9265-
SET_INT(result, 9, ru->ru_inblock);
9266-
SET_INT(result, 10, ru->ru_oublock);
9267-
SET_INT(result, 11, ru->ru_msgsnd);
9268-
SET_INT(result, 12, ru->ru_msgrcv);
9269-
SET_INT(result, 13, ru->ru_nsignals);
9270-
SET_INT(result, 14, ru->ru_nvcsw);
9271-
SET_INT(result, 15, ru->ru_nivcsw);
9272-
#undef SET_INT
9273-
9274-
if (PyErr_Occurred()) {
9275-
Py_DECREF(result);
9276-
return NULL;
9277-
}
9254+
#define SET_RESULT(CALL) \
9255+
do { \
9256+
PyObject *item = (CALL); \
9257+
if (item == NULL) { \
9258+
Py_DECREF(result); \
9259+
return NULL; \
9260+
} \
9261+
PyStructSequence_SET_ITEM(result, pos++, item); \
9262+
} while(0)
9263+
9264+
SET_RESULT(PyFloat_FromDouble(doubletime(ru->ru_utime)));
9265+
SET_RESULT(PyFloat_FromDouble(doubletime(ru->ru_stime)));
9266+
SET_RESULT(PyLong_FromLong(ru->ru_maxrss));
9267+
SET_RESULT(PyLong_FromLong(ru->ru_ixrss));
9268+
SET_RESULT(PyLong_FromLong(ru->ru_idrss));
9269+
SET_RESULT(PyLong_FromLong(ru->ru_isrss));
9270+
SET_RESULT(PyLong_FromLong(ru->ru_minflt));
9271+
SET_RESULT(PyLong_FromLong(ru->ru_majflt));
9272+
SET_RESULT(PyLong_FromLong(ru->ru_nswap));
9273+
SET_RESULT(PyLong_FromLong(ru->ru_inblock));
9274+
SET_RESULT(PyLong_FromLong(ru->ru_oublock));
9275+
SET_RESULT(PyLong_FromLong(ru->ru_msgsnd));
9276+
SET_RESULT(PyLong_FromLong(ru->ru_msgrcv));
9277+
SET_RESULT(PyLong_FromLong(ru->ru_nsignals));
9278+
SET_RESULT(PyLong_FromLong(ru->ru_nvcsw));
9279+
SET_RESULT(PyLong_FromLong(ru->ru_nivcsw));
9280+
#undef SET_RESULT
92789281

92799282
return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
92809283
}

0 commit comments

Comments
 (0)