-
-
Notifications
You must be signed in to change notification settings - Fork 33.9k
gh-144100: Fix crash for POINTER(str) used in ctypes argtypes #144108
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
base: main
Are you sure you want to change the base?
gh-144100: Fix crash for POINTER(str) used in ctypes argtypes #144108
Conversation
This comment was marked as resolved.
This comment was marked as resolved.
|
@vstinner This is my proposed solution if you have time please review it. |
| with self.assertWarns(DeprecationWarning): | ||
| BadType = ctypes.POINTER("BugTrigger") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer to avoid deprecated code path. You should be able to use:
| with self.assertWarns(DeprecationWarning): | |
| BadType = ctypes.POINTER("BugTrigger") | |
| class BadType(ctypes._Pointer): | |
| # _type_ is not defined on purpose | |
| pass |
| if os.name == "nt": | ||
| libc = ctypes.WinDLL("kernel32.dll") | ||
| func = libc.GetCurrentProcessId | ||
| else: | ||
| libc = ctypes.CDLL(None) | ||
| func = libc.getpid | ||
|
|
||
| func.argtypes = (BadType,) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use the Python C API:
| if os.name == "nt": | |
| libc = ctypes.WinDLL("kernel32.dll") | |
| func = libc.GetCurrentProcessId | |
| else: | |
| libc = ctypes.CDLL(None) | |
| func = libc.getpid | |
| func.argtypes = (BadType,) | |
| func = ctypes.pythonapi.Py_GetVersion | |
| func.argtypes = (BadType,) |
| ptr.set_type(c_int) | ||
| self.assertIs(ptr._type_, c_int) | ||
|
|
||
| class TestPointerStringProto(unittest.TestCase): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer to add the test to the end of PointersTestCase, instead of adding a new test case.
| import gc | ||
| import sys | ||
| import unittest | ||
| import os |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This import can be removed with my proposed code.
Co-authored-by: Victor Stinner <vstinner@python.org>
Co-authored-by: Victor Stinner <vstinner@python.org>
It fixes a crash in
ctypesthat occur when a deprecatedPOINTER(str)type was used inargtypes.When such a pointer type was encountered during argument conversion,
ctypeswould hit an internal assertion and abort the interpreter in debug builds.This PR replaces the assertion with proper error handling and raises a Python exception instead.