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
segfault in ctypes.cast #66182
Comments
I get a segfault when trying to cast a string to a structure.
>>> import ctypes
>>> class Struct(ctypes.Structure):
... _fields_ = [('a', ctypes.c_uint32)]
...
>>> s = '0'*100
>>> ctypes.cast(s,Struct)
Segmentation fault The docs (https://docs.python.org/2/library/ctypes.html#ctypes.cast) say that >>> ctypes.cast(range(10),Struct)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.4/ctypes/__init__.py", line 488, in cast
return _cast(obj, obj, typ)
ctypes.ArgumentError: argument 1: <class 'TypeError'>: wrong type |
You need to cast to a pointer type, i.e. POINTER(Struct). Trying to cast to just Struct should raise a TypeError. Instead this revealed a bug in cast_check_pointertype (3.4.1): http://hg.python.org/cpython/file/c0e311e010fc/Modules/_ctypes/_ctypes.c#l5225 dict->proto is NULL in the Struct type's stgdict, so PyUnicode_Check(dict->proto) segfaults. A simple fix is to add a check for this on line 5235: if (dict && dict->proto) { Then cast will raise the expected TypeError from line 5242 on return from line 5255. |
I'll provide a patch but I don't know which test file to use, can somebody please advise. |
IMHO, Lib/ctypes/test/test_cast.py is the relevant test. Mark, do you still wish to provide a fix for that? |
The backport to 2.7 needs some help. I can't do it on my laptop for the next week, but I'll try to get to it eventually. Feel free to get there first. |
Still needs a backport to 2.7 |
No longer needed, I close the issue. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: