Ctypes c_void_p overflow

Joseph L. Casale jcasale at activenetwerx.com
Thu May 5 17:20:08 EDT 2016


> I generally avoid c_void_p because its lenient from_param method
> (called to convert arguments) doesn't provide much type safety. If a
> bug causes an incorrect argument to be passed, I prefer getting an
> immediate ctypes.ArgumentError rather than a segfault or data
> corruption. For example, when a C API returns a void pointer as a
> handle for an opaque structure or object, I prefer to handle it as a
> pointer to an empty Structure subclass, as follows:
>
>     class _ContosoHandle(ctypes.Structure):
>         pass
>
>     ContosoHandle = ctypes.POINTER(_ContosoHandle)
>
>     lib.CreateContoso.restype = ContosoHandle
>     lib.DestroyContoso.argtypes = (ContosoHandle,)
>
> ctypes will raise an ArgumentError if DestroyContoso is called with
> arguments such as 123456789 or "Crash Me".

After typing up a response with all the detail, your reply helped me see
the error.

Thank you so much for all that detail, it was very much appreciated!
jlc


More information about the Python-list mailing list