Skip to content

Instantly share code, notes, and snippets.

@mjg59
Created February 11, 2018 07:24
Show Gist options
  • Save mjg59/ccbeec81dacd38eac5fd92e121faa5fc to your computer and use it in GitHub Desktop.
Save mjg59/ccbeec81dacd38eac5fd92e121faa5fc to your computer and use it in GitHub Desktop.
#include <signal.h>
#include <stdio.h>
#include <malloc.h>
#include <setjmp.h>
#include <stdbool.h>
static jmp_buf omg;
struct list {
char dummy[16384];
struct list *next;
};
static void handler(int sig, siginfo_t *si, void *unused)
{
longjmp(omg, 1);
}
bool list_cycle(struct list *list)
{
struct sigaction sa;
struct list *prev;
sa.sa_flags = SA_SIGINFO;
sigemptyset(&sa.sa_mask);
sa.sa_sigaction = handler;
if (sigaction(SIGABRT, &sa, NULL) == -1) {
perror("sigaction");
return -1;
}
if (setjmp(omg))
return true;
while (list->next) {
prev = list;
list = list->next;
free(prev);
}
free(list);
return false;
}
int main (int argc, char **argv) {
struct list *test, *test2;
test = malloc(sizeof(struct list));
test2 = malloc(sizeof(struct list));
test->next = test2;
test2->next = test;
if (list_cycle(test) == true) {
printf("loop\n");
} else {
printf("no loop\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment