Skip to content

Commit a034493

Browse files
[3.11] gh-105375: Improve error handling in _ctypes (GH-105593) (#105664)
Prevent repeated PyLong_FromVoidPtr() from possibly overwriting the current exception. (cherry picked from commit e8998e4) Co-authored-by: Erlend E. Aasland <[email protected]>
1 parent 9187747 commit a034493

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix bugs in :mod:`_ctypes` where exceptions could end up being overwritten.

Modules/_ctypes/callbacks.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -499,12 +499,22 @@ long Call_GetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
499499

500500
{
501501
PyObject *py_rclsid = PyLong_FromVoidPtr((void *)rclsid);
502+
if (py_rclsid == NULL) {
503+
Py_DECREF(func);
504+
PyErr_WriteUnraisable(context ? context : Py_None);
505+
return E_FAIL;
506+
}
502507
PyObject *py_riid = PyLong_FromVoidPtr((void *)riid);
508+
if (py_riid == NULL) {
509+
Py_DECREF(func);
510+
Py_DECREF(py_rclsid);
511+
PyErr_WriteUnraisable(context ? context : Py_None);
512+
return E_FAIL;
513+
}
503514
PyObject *py_ppv = PyLong_FromVoidPtr(ppv);
504-
if (!py_rclsid || !py_riid || !py_ppv) {
505-
Py_XDECREF(py_rclsid);
506-
Py_XDECREF(py_riid);
507-
Py_XDECREF(py_ppv);
515+
if (py_ppv == NULL) {
516+
Py_DECREF(py_rclsid);
517+
Py_DECREF(py_riid);
508518
Py_DECREF(func);
509519
PyErr_WriteUnraisable(context ? context : Py_None);
510520
return E_FAIL;

0 commit comments

Comments
 (0)