Skip to content

Commit c5d0978

Browse files
Fix ignored error returns in sys initialization, _ctypes and _interpchannels (#160614)
1 parent a9049ee commit c5d0978

4 files changed

Lines changed: 40 additions & 16 deletions

File tree

‎Modules/_ctypes/_ctypes.c‎

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2479,22 +2479,30 @@ PyCSimpleType_init(PyObject *self, PyObject *args, PyObject *kwds)
24792479
}
24802480
assert(sw_info);
24812481
#ifdef WORDS_BIGENDIAN
2482-
PyObject_SetAttrString(self, "__ctype_le__", swapped);
2483-
PyObject_SetAttrString(self, "__ctype_be__", self);
2484-
PyObject_SetAttrString(swapped, "__ctype_be__", self);
2485-
PyObject_SetAttrString(swapped, "__ctype_le__", swapped);
2482+
if (PyObject_SetAttrString(self, "__ctype_le__", swapped) < 0 ||
2483+
PyObject_SetAttrString(self, "__ctype_be__", self) < 0 ||
2484+
PyObject_SetAttrString(swapped, "__ctype_be__", self) < 0 ||
2485+
PyObject_SetAttrString(swapped, "__ctype_le__", swapped) < 0)
2486+
{
2487+
Py_DECREF(swapped);
2488+
return -1;
2489+
}
24862490
/* We are creating the type for the OTHER endian */
24872491
sw_info->format = _ctypes_alloc_format_string("<", stginfo->format+1);
24882492
#else
2489-
PyObject_SetAttrString(self, "__ctype_be__", swapped);
2490-
PyObject_SetAttrString(self, "__ctype_le__", self);
2491-
PyObject_SetAttrString(swapped, "__ctype_le__", self);
2492-
PyObject_SetAttrString(swapped, "__ctype_be__", swapped);
2493+
if (PyObject_SetAttrString(self, "__ctype_be__", swapped) < 0 ||
2494+
PyObject_SetAttrString(self, "__ctype_le__", self) < 0 ||
2495+
PyObject_SetAttrString(swapped, "__ctype_le__", self) < 0 ||
2496+
PyObject_SetAttrString(swapped, "__ctype_be__", swapped) < 0)
2497+
{
2498+
Py_DECREF(swapped);
2499+
return -1;
2500+
}
24932501
/* We are creating the type for the OTHER endian */
24942502
sw_info->format = _ctypes_alloc_format_string(">", stginfo->format+1);
24952503
#endif
24962504
Py_DECREF(swapped);
2497-
if (PyErr_Occurred()) {
2505+
if (sw_info->format == NULL) {
24982506
return -1;
24992507
}
25002508
};

‎Modules/_interpchannelsmodule.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2638,7 +2638,7 @@ _channelid_shared(PyThreadState *tstate, PyObject *obj, _PyXIData_t *data)
26382638
if (st == NULL) {
26392639
return -1;
26402640
}
2641-
if (!PyObject_IsInstance(obj, (PyObject *)st->ChannelIDType)) {
2641+
if (!PyObject_TypeCheck(obj, st->ChannelIDType)) {
26422642
PyErr_Format(PyExc_TypeError,
26432643
"'_id' attribute must be an %N object, not %T",
26442644
st->ChannelIDType, obj);

‎Python/pylifecycle.c‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3211,8 +3211,14 @@ init_sys_streams(PyThreadState *tstate)
32113211
config->stdio_errors);
32123212
if (std == NULL)
32133213
goto error;
3214-
PySys_SetObject("__stdin__", std);
3215-
_PySys_SetAttr(&_Py_ID(stdin), std);
3214+
if (PySys_SetObject("__stdin__", std) < 0) {
3215+
Py_DECREF(std);
3216+
goto error;
3217+
}
3218+
if (_PySys_SetAttr(&_Py_ID(stdin), std) < 0) {
3219+
Py_DECREF(std);
3220+
goto error;
3221+
}
32163222
Py_DECREF(std);
32173223

32183224
/* Set sys.stdout */
@@ -3222,8 +3228,14 @@ init_sys_streams(PyThreadState *tstate)
32223228
config->stdio_errors);
32233229
if (std == NULL)
32243230
goto error;
3225-
PySys_SetObject("__stdout__", std);
3226-
_PySys_SetAttr(&_Py_ID(stdout), std);
3231+
if (PySys_SetObject("__stdout__", std) < 0) {
3232+
Py_DECREF(std);
3233+
goto error;
3234+
}
3235+
if (_PySys_SetAttr(&_Py_ID(stdout), std) < 0) {
3236+
Py_DECREF(std);
3237+
goto error;
3238+
}
32273239
Py_DECREF(std);
32283240

32293241
#if 1 /* Disable this if you have trouble debugging bootstrap stuff */

‎Python/sysmodule.c‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4380,8 +4380,12 @@ _PySys_Create(PyThreadState *tstate, PyObject **sysmod_p)
43804380
PyUnstable_Module_SetGIL(sysmod, Py_MOD_GIL_NOT_USED);
43814381
#endif
43824382

4383-
PyModule_AddIntMacro(sysmod, EXIT_SUCCESS);
4384-
PyModule_AddIntMacro(sysmod, EXIT_FAILURE);
4383+
if (PyModule_AddIntMacro(sysmod, EXIT_SUCCESS) < 0) {
4384+
goto error;
4385+
}
4386+
if (PyModule_AddIntMacro(sysmod, EXIT_FAILURE) < 0) {
4387+
goto error;
4388+
}
43854389

43864390
PyObject *sysdict = PyModule_GetDict(sysmod);
43874391
if (sysdict == NULL) {

0 commit comments

Comments
 (0)