-
-
Notifications
You must be signed in to change notification settings - Fork 32k
ctypes: NULL-dereference when using py_result restype #132417
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Labels
extension-modules
C modules in the Modules dir
topic-ctypes
type-crash
A hard crash of the interpreter, possibly with a core dump
Comments
NicolasT
added a commit
to NicolasT/cpython
that referenced
this issue
Apr 11, 2025
…andling (GH-MMMMMM)
NicolasT
added a commit
to NicolasT/cpython
that referenced
this issue
Apr 11, 2025
NicolasT
added a commit
to NicolasT/cpython
that referenced
this issue
Apr 11, 2025
NicolasT
added a commit
to NicolasT/cpython
that referenced
this issue
Apr 11, 2025
picnixz
pushed a commit
that referenced
this issue
Apr 12, 2025
…tions returning `PyObject *` (#132418) Some functions (such as `PyErr_Occurred`) with a `restype` set to `ctypes.py_object` may return NULL without setting an exception.
miss-islington
pushed a commit
to miss-islington/cpython
that referenced
this issue
Apr 12, 2025
…g functions returning `PyObject *` (pythonGH-132418) Some functions (such as `PyErr_Occurred`) with a `restype` set to `ctypes.py_object` may return NULL without setting an exception. (cherry picked from commit 2aab2db) Co-authored-by: Nicolas Trangez <[email protected]>
picnixz
pushed a commit
that referenced
this issue
Apr 12, 2025
…ng functions returning `PyObject *` (GH-132418) (#132425) GH-132417: ctypes: Fix potential `Py_DECREF(NULL)` when handling functions returning `PyObject *` (GH-132418) Some functions (such as `PyErr_Occurred`) with a `restype` set to `ctypes.py_object` may return NULL without setting an exception. (cherry picked from commit 2aab2db) Co-authored-by: Nicolas Trangez <[email protected]>
Thank you for the report & fix! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
extension-modules
C modules in the Modules dir
topic-ctypes
type-crash
A hard crash of the interpreter, possibly with a core dump
Crash report
What happened?
When using
ctypes
with a function which returns aPyObject *
, usingrestype
ctypes.py_object
, and the function returnsNULL
, there's aPy_DECREF
of the result, leading to a segfault:The issue lies in
GetResult
: whenO_get
is the result handler, it callsPy_DECREF
on the result value, even though that can beNULL
.O_get
handles theNULL
case correctly, setting an exception if none was set, and passing on theNULL
. Code after thePy_DECREF
inGetResult
also handles theNULL
case correctly, so changing thePy_DECREF
into aPy_XDECREF
makes things work.Pull request incoming.
CPython versions tested on:
CPython 3.10, CPython 3.13, CPython main branch
Operating systems tested on:
Linux
Output from running 'python -VV' on the command line:
Python 3.14.0a7+ (heads/main-dirty:deda47d6e18, Apr 11 2025, 20:18:59) [GCC 14.2.1 20240912 (Red Hat 14.2.1-3)]
Linked PRs
Py_DECREF(NULL)
when handling functions returningPyObject *
(GH-132418) #132425The text was updated successfully, but these errors were encountered: