@ -0,0 +1,29 @@ | |||||
CFLAGS = -g | |||||
CPPFLAGS = -I/opt/local/include | |||||
LDFLAGS = -L/opt/local/lib | |||||
LDLIBS = -lgc | |||||
all : main | |||||
%.c : %.leg | |||||
leg -o $@ $< | |||||
% : %.c | |||||
cc $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(LDLIBS) -o $@ $< | |||||
test : main | |||||
./main -xvv test.txt | |||||
demo : main | |||||
for i in demofiles/*.c; do echo $$i; ./main -x < $$i; done | |||||
demov : main | |||||
for i in demofiles/*.c; do echo $$i; ./main -x -vv < $$i; done | |||||
spotless : clean | |||||
rm -rf *~ *.dSYM | |||||
clean : .FORCE | |||||
rm -f main | |||||
.FORCE : |
@ -0,0 +1,14 @@ | |||||
SRC = $(wildcard *.c) | |||||
EXE = $(SRC:.c=) | |||||
CFLAGS = -Wno-return-stack-address | |||||
all : $(EXE) | |||||
spotless : clean | |||||
rm -f *~ | |||||
clean : .FORCE | |||||
rm -rf $(EXE) *.dSYM | |||||
.FORCE : |
@ -0,0 +1,18 @@ | |||||
// dangling-pointer | |||||
#include <stdlib.h> | |||||
#include <stdio.h> | |||||
#include <assert.h> | |||||
int *alloc() { | |||||
int i; | |||||
return &i; | |||||
} | |||||
int main() { | |||||
int *ptr = alloc(); | |||||
assert(ptr); | |||||
*ptr = 42; | |||||
printf("%d\n", *ptr); | |||||
return 0; | |||||
} |
@ -0,0 +1,11 @@ | |||||
// invalid-pointer | |||||
#include <stdio.h> | |||||
int main() { | |||||
int *ptr = (int *)(intptr_t)0xDeadD0d0; | |||||
printf("%p\n",ptr); | |||||
*ptr = 42; // illegal memory access | |||||
printf("%d\n", *ptr); | |||||
return 0; | |||||
} |
@ -0,0 +1,14 @@ | |||||
// memory-leak | |||||
#include <stdlib.h> | |||||
#include <stdio.h> | |||||
#include <assert.h> | |||||
int main() { | |||||
for (int i = 0; i < 10; ++i) { | |||||
int *ptr = malloc(sizeof(*ptr)); | |||||
assert(ptr); | |||||
*ptr = i; | |||||
} | |||||
return 0; | |||||
} |
@ -0,0 +1,13 @@ | |||||
// multiple-free | |||||
#include <stdlib.h> | |||||
#include <stdio.h> | |||||
#include <assert.h> | |||||
int main() { | |||||
int *ptr = malloc(sizeof(*ptr)); | |||||
assert(ptr); | |||||
free(ptr); | |||||
free(ptr); | |||||
return 0; | |||||
} |
@ -0,0 +1,9 @@ | |||||
// null-pointer | |||||
#include <stdio.h> | |||||
int main() { | |||||
char *ptr = NULL; | |||||
printf("%s\n", ptr); | |||||
return 0; | |||||
} |
@ -0,0 +1,10 @@ | |||||
// out-of-bounds-access | |||||
#include <stdio.h> | |||||
int main() { | |||||
int array[5] = { 0, 1, 2, 3, 4 }, i = 4; | |||||
printf("%d\n", array[i++]); | |||||
printf("%d\n", array[i++]); // out of bounds | |||||
return 0; | |||||
} |
@ -0,0 +1,15 @@ | |||||
// pointer-compare | |||||
#include <stdlib.h> | |||||
#include <stdio.h> | |||||
int main() { | |||||
int array[5] = {0, 1, 2, 3, 4}; | |||||
int brray[5] = {0, 1, 2, 3, 4}; | |||||
int *p = array + 2; | |||||
int *q = array + 4; | |||||
int *r = brray + 4; | |||||
if (p > q) abort(); | |||||
if (p > r) abort(); // illegal comparison | |||||
return 0; | |||||
} |
@ -0,0 +1,11 @@ | |||||
// pointer-increment | |||||
#include <stdio.h> | |||||
int main() { | |||||
int array[5] = {0, 1, 2, 3, 4}; | |||||
int *ptr = array + 4; | |||||
++ptr; | |||||
++ptr; // out of bounds | |||||
return 0; | |||||
} |
@ -0,0 +1,10 @@ | |||||
// pointer-out-of-bounds | |||||
#include <stdio.h> | |||||
int main() { | |||||
int array[5] = {0, 1, 2, 3, 4}; | |||||
int *ptr = &array[5]; | |||||
*ptr = 42; | |||||
return 0; | |||||
} |
@ -0,0 +1,9 @@ | |||||
// segmentation-fault | |||||
#include <stdio.h> | |||||
int main() { | |||||
int *ptr = NULL; | |||||
*ptr = 42; | |||||
return 0; | |||||
} |
@ -0,0 +1,15 @@ | |||||
// use-after-free | |||||
#include <stdlib.h> | |||||
#include <stdio.h> | |||||
#include <assert.h> | |||||
int main() { | |||||
int *ptr = malloc(sizeof(*ptr)); | |||||
assert(ptr); | |||||
*ptr = 42; | |||||
free(ptr); | |||||
printf("%d\n", *ptr); // use after free | |||||
*ptr = 43; // use after free | |||||
return 0; | |||||
} |
@ -0,0 +1 @@ | |||||
// here is stdio |
@ -0,0 +1,11 @@ | |||||
// -*- C -*- | |||||
#include <stdio.h> | |||||
#include "myfile.c" | |||||
int main() | |||||
{ | |||||
printf("hello, world\n"); | |||||
return 0; | |||||
} |