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
Crash when subclassing ctypes.Union #82549
Comments
Ran into Segfaults while trying to use pysnmp with 3.8.0rc1. $ python3.8
Python 3.8.0rc1 (default, Oct 2 2019, 14:15:18)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ctypes
>>> class in6_addr_U(ctypes.Union):
... _fields_ = [
... ('__u6_addr8', ctypes.c_uint8 * 16),
... ('__u6_addr16', ctypes.c_uint16 * 8),
... ('__u6_addr32', ctypes.c_uint32 * 4),
... ]
...
Segmentation fault $ docker run -it python:3.8.0rc1-slim
Python 3.8.0rc1 (default, Oct 2 2019, 23:38:42)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ctypes
>>> class in6_addr_U(ctypes.Union):
... _fields_ = [
... ('__u6_addr8', ctypes.c_uint8 * 16),
... ('__u6_addr16', ctypes.c_uint16 * 8),
... ('__u6_addr32', ctypes.c_uint32 * 4),
... ]
...
$ The code is from here: https://github.com/etingof/pysnmp/blob/master/pysnmp/carrier/sockmsg.py#L47-L52 |
I am adding 3.8 regression since the report says 3.8.0b4 works and segfaults with 3.8.0RC1. |
Also crashes on Windows (x64 and x86), and on 3.7.5rc1, so this is likely in our code. |
I get this assertion in a debug build: Assertion failed: actual_type_index <= MAX_ELEMENTS, file c:\projects\cpython\modules\_ctypes\stgdict.c, line 718 |
Increasing MAX_ELEMENTS fixes it, but I'm not sure what other impacts there are from doing that. +Vinay who added the array handling that's hitting the limit. |
Given this limit can easily be hit by user code, I'd like to see it turned into a proper check with an exception (or a dynamic array) rather than just an assertion. We should not segfault here. while (length > 0) {
actual_types[actual_type_index++] = &edict->ffi_type_pointer;
assert(actual_type_index <= MAX_ELEMENTS);
length--;
} |
Agreed, MAX_ELEMENTS was set to be an upper bound which shouldn't be hit. I'll investigate with OP's example data and see where the bug is. |
(fix released in 3.8.0 and 3.7.5) |
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: