Skip to content

Instantly share code, notes, and snippets.

@ytlvy
Forked from haileys/segv-handler.c
Created December 17, 2015 08:25
Show Gist options
  • Save ytlvy/c3e90ef8ee4b1e60622e to your computer and use it in GitHub Desktop.
Save ytlvy/c3e90ef8ee4b1e60622e to your computer and use it in GitHub Desktop.
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
void
segv() // <--- never gets called
{
fprintf(stderr, "segv\n");
exit(0);
}
void
recur()
{
recur();
}
int
main()
{
stack_t ss;
ss.ss_sp = malloc(65536);
ss.ss_size = 65536;
ss.ss_flags = 0;
if(sigaltstack(&ss, NULL) < 0) {
perror("sigaltstack");
exit(1);
}
struct sigaction sa;
sa.sa_flags = SA_SIGINFO;
sigemptyset(&sa.sa_mask);
sa.sa_sigaction = segv;
if(sigaction(SIGSEGV, &sa, NULL) < 0) {
perror("sigaction");
exit(0);
}
if(sigaction(SIGBUS, &sa, NULL) < 0) {
perror("sigaction");
exit(0);
}
recur();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment