Skip to content

Commit 8c28ddb

Browse files
gh-105375: Improve error handling in _ctypes
Prevent repeated PyLong_FromVoidPtr() from possibly overwriting the current exception.
1 parent 89aac6f commit 8c28ddb

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
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: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -479,13 +479,20 @@ long Call_GetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
479479

480480
{
481481
PyObject *py_rclsid = PyLong_FromVoidPtr((void *)rclsid);
482+
if (py_rclsid == NULL) {
483+
PyErr_WriteUnraisable(context ? context : Py_None);
484+
return E_FAIL;
485+
}
482486
PyObject *py_riid = PyLong_FromVoidPtr((void *)riid);
487+
if (py_riid == NULL) {
488+
Py_DECREF(py_rclsid);
489+
PyErr_WriteUnraisable(context ? context : Py_None);
490+
return E_FAIL;
491+
}
483492
PyObject *py_ppv = PyLong_FromVoidPtr(ppv);
484-
if (!py_rclsid || !py_riid || !py_ppv) {
485-
Py_XDECREF(py_rclsid);
486-
Py_XDECREF(py_riid);
487-
Py_XDECREF(py_ppv);
488-
Py_DECREF(func);
493+
if (py_ppv == NULL) {
494+
Py_DECREF(py_rclsid);
495+
Py_DECREF(py_riid);
489496
PyErr_WriteUnraisable(context ? context : Py_None);
490497
return E_FAIL;
491498
}

0 commit comments

Comments
 (0)