Last active
August 29, 2020 16:38
-
-
Save sjkillen/da51a0253131459aa5c9a46fa0375fc4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <setjmp.h> | |
#include <signal.h> | |
#include <stdio.h> | |
#include <stdlib.h> // EXIT_SUCCESS | |
#include <unistd.h> // sbrk | |
struct datum { | |
int v1; | |
int v2; | |
int v3; | |
}; | |
static jmp_buf env; | |
struct datum *data; | |
void on_segfault(int signum) { | |
puts("signal rec'd"); | |
longjmp(env, 1); | |
} | |
int main(void) { | |
struct sigaction action; | |
action.sa_handler = on_segfault; | |
sigemptyset(&action.sa_mask); | |
action.sa_flags = 0; | |
sigaction(SIGSEGV, &action, NULL); | |
if (!setjmp(env)) { | |
data = sbrk(sizeof(struct datum) * 1000); | |
if (data == -1) { | |
puts("sbrk failed"); | |
exit(EXIT_FAILURE); | |
} | |
struct datum *it = data; | |
// Write data to array | |
for (;; it++) { | |
it->v1 = 1; | |
it->v2 = 2; | |
it->v3 = 3; | |
} | |
} | |
puts("Success, printing data:"); | |
for (struct datum *it = data; it < (data + 1000); it++) { | |
printf("%d %d %d", it->v1, it->v2, it->v3); | |
} | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fills data buffer with integers 1, 2, 3, 1, 2, 3 ...
Uses segfault signal instead of loop termination condition.