From 3c55d2c9a122091db77b735120c3bf5aa29722ad Mon Sep 17 00:00:00 2001 From: Jakob Kaivo Date: Tue, 30 Jun 2020 13:38:00 -0400 Subject: add tests for double free() and use after free() --- Makefile | 9 ++++++++- test/double-free.c | 19 +++++++++++++++++++ test/use-after-free.c | 18 ++++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 test/double-free.c create mode 100644 test/use-after-free.c diff --git a/Makefile b/Makefile index accf611..3f7db7f 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,12 @@ TESTDIR=test CFLAGS=-I$(INCDIR) -Wall -Wextra -Wpedantic -g OBJECTS=$(OBJDIR)/mapalloc.o -TESTS=$(BINDIR)/overflow $(BINDIR)/underflow $(BINDIR)/zero $(BINDIR)/realloc +TESTS=$(BINDIR)/overflow \ + $(BINDIR)/underflow \ + $(BINDIR)/zero \ + $(BINDIR)/realloc \ + $(BINDIR)/use-after-free \ + $(BINDIR)/double-free all: $(LIBDIR)/libmapalloc.a @@ -34,6 +39,8 @@ $(BINDIR)/overflow: $(TESTDIR)/overflow.c $(BINDIR)/underflow: $(TESTDIR)/underflow.c $(BINDIR)/zero: $(TESTDIR)/zero.c $(BINDIR)/realloc: $(TESTDIR)/realloc.c +$(BINDIR)/use-after-free: $(TESTDIR)/use-after-free.c +$(BINDIR)/double-free: $(TESTDIR)/double-free.c $(TESTS): @mkdir -p $(@D) diff --git a/test/double-free.c b/test/double-free.c new file mode 100644 index 0000000..73ceff5 --- /dev/null +++ b/test/double-free.c @@ -0,0 +1,19 @@ +#define _POSIX_C_SOURCE 200809L +#include +#include +#include +#include "mapalloc.h" + +int main(void) +{ + const char buf[] = "THIS IS A CONSTANT STRING"; + + char *ptr = map_malloc(sizeof(buf)); + memcpy(ptr, buf, sizeof(buf)); + printf("%p: %s\n", ptr, ptr); + + map_free(ptr); + printf("freed\n"); + map_free(ptr); + printf("freed again\n"); +} diff --git a/test/use-after-free.c b/test/use-after-free.c new file mode 100644 index 0000000..4338ed5 --- /dev/null +++ b/test/use-after-free.c @@ -0,0 +1,18 @@ +#define _POSIX_C_SOURCE 200809L +#include +#include +#include +#include "mapalloc.h" + +int main(void) +{ + const char buf[] = "THIS IS A CONSTANT STRING"; + + char *ptr = map_malloc(sizeof(buf)); + memcpy(ptr, buf, sizeof(buf)); + printf("%p: %s\n", ptr, ptr); + + map_free(ptr); + printf("freed\n"); + printf("%p: %s\n", ptr, ptr); +} -- cgit v1.2.1