~jkaivo/mapalloc

3c55d2c9a122091db77b735120c3bf5aa29722ad — Jakob Kaivo 2 years ago 624ac93
add tests for double free() and use after free()
3 files changed, 45 insertions(+), 1 deletions(-)

M Makefile
A test/double-free.c
A test/use-after-free.c
M Makefile => Makefile +8 -1
@@ 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)

A test/double-free.c => test/double-free.c +19 -0
@@ 0,0 1,19 @@
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#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");
}

A test/use-after-free.c => test/use-after-free.c +18 -0
@@ 0,0 1,18 @@
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#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);
}