~jkaivo/mapalloc

543c1d44711c4a4cd1ee52c3717abfe2c79b344a — Jakob Kaivo 2 years ago aacd5c7
add tests for attempting to free() or realloc() on an invalid address
3 files changed, 30 insertions(+), 2 deletions(-)

M Makefile
A test/invalid-free.c
A test/invalid-realloc.c
M Makefile => Makefile +6 -2
@@ 16,13 16,15 @@ TESTS=$(BINDIR)/overflow \
	$(BINDIR)/realloc \
	$(BINDIR)/use-after-free \
	$(BINDIR)/double-free \
	$(BINDIR)/macros
	$(BINDIR)/macros \
	$(BINDIR)/invalid-free \
	$(BINDIR)/invalid-realloc

all: $(LIBDIR)/libmapalloc.a $(LIBDIR)/libwrapalloc.so

#$(LIBDIR)/libmapalloc.so

tests: $(TESTS) $(BINDIR)/wrapper
tests: all $(TESTS) $(BINDIR)/wrapper

$(OBJDIR)/mapalloc.o: $(SRCDIR)/mapalloc.c $(INCDIR)/mapalloc.h
	@mkdir -p $(@D)


@@ 52,6 54,8 @@ $(BINDIR)/realloc: $(TESTDIR)/realloc.c
$(BINDIR)/use-after-free: $(TESTDIR)/use-after-free.c
$(BINDIR)/double-free: $(TESTDIR)/double-free.c
$(BINDIR)/macros: $(TESTDIR)/macros.c
$(BINDIR)/invalid-free: $(TESTDIR)/invalid-free.c
$(BINDIR)/invalid-realloc: $(TESTDIR)/invalid-realloc.c

$(BINDIR)/wrapper: $(TESTDIR)/wrapper.c
	@mkdir -p $(@D)

A test/invalid-free.c => test/invalid-free.c +12 -0
@@ 0,0 1,12 @@
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "mapalloc.h"

int main(void)
{
	int foo = 42;
	MA_free(&foo);
	printf("freed\n");
}

A test/invalid-realloc.c => test/invalid-realloc.c +12 -0
@@ 0,0 1,12 @@
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "mapalloc.h"

int main(void)
{
	int foo = 42;
	int *ptr = MA_realloc(&foo, sizeof(int));
	printf("%d\n", *ptr);
}