I'm trying to setup a SIGSEGV signal handler for Python using a C extension module:
import my_extension_module
import ctypes
my_extension_module.setup_segfault_handler()
ctypes.string_at(0) # i would like this to error and not segfault
I've seen several questions like this one and functions like longjmp
or setjmp
, but none of these seem to work how I intend.
I am aware that not terminating after a SIGSEGV is a bad idea for a number of reasons, but I need it nonetheless.
Here is my current code:
static jmp_buf buf;
void handle(int signum) {
PyErr_SetString(PyExc_RuntimeError, "segmentation fault occured");
longjmp(buf, 1);
}
static PyObject* method_setup() {
signal(SIGSEGV, handle);
setjmp(buf);
Py_RETURN_NONE;
}