diff --git a/.github/workflows/c-cpp.yml b/.github/workflows/c-cpp.yml index ad88d18..5784592 100644 --- a/.github/workflows/c-cpp.yml +++ b/.github/workflows/c-cpp.yml @@ -15,5 +15,7 @@ jobs: - uses: actions/checkout@v3 - name: install valgrind run: sudo apt install valgrind - - name: make test + - name: make test (valgrind) run: make test + - name: make test (sanitized) + run: make test-sanitized diff --git a/makefile b/makefile index 56ecbd9..8cbe545 100644 --- a/makefile +++ b/makefile @@ -1,5 +1,6 @@ # Optimisation Options # export CFLAGS+=-O2 -mtune=native -march=native +# export CFLAGS+=-fsanitize=address,undefined export TOY_OUTDIR = out @@ -35,6 +36,12 @@ static-release: $(TOY_OUTDIR) test: clean $(TOY_OUTDIR) $(MAKE) -C test +test-sanitized: export CFLAGS+=-fsanitize=address,undefined +test-sanitized: export LIBS+=-static-libasan +test-sanitized: export DISABLE_VALGRIND=true +test-sanitized: clean $(TOY_OUTDIR) + $(MAKE) -C test + $(TOY_OUTDIR): mkdir $(TOY_OUTDIR) diff --git a/repl/repl_main.c b/repl/repl_main.c index cafed98..5aae83c 100644 --- a/repl/repl_main.c +++ b/repl/repl_main.c @@ -30,7 +30,11 @@ void repl() { for(;;) { printf("> "); - fgets(input, size, stdin); + + //handle EOF for exits + if (!fgets(input, size, stdin)) { + break; + } //escape the repl (length of 5 to accomodate the newline) if (strlen(input) == 5 && (!strncmp(input, "exit", 4) || !strncmp(input, "quit", 4))) { diff --git a/source/interpreter.c b/source/interpreter.c index ad65ca4..26e701c 100644 --- a/source/interpreter.c +++ b/source/interpreter.c @@ -180,19 +180,22 @@ static unsigned char readByte(unsigned char* tb, int* count) { } static unsigned short readShort(unsigned char* tb, int* count) { - unsigned short ret = *(unsigned short*)(tb + *count); + unsigned short ret = 0; + memcpy(&ret, tb + *count, 2); *count += 2; return ret; } static int readInt(unsigned char* tb, int* count) { - int ret = *(int*)(tb + *count); + int ret = 0; + memcpy(&ret, tb + *count, 4); *count += 4; return ret; } static float readFloat(unsigned char* tb, int* count) { - float ret = *(float*)(tb + *count); + float ret = 0; + memcpy(&ret, tb + *count, 4); *count += 4; return ret; } diff --git a/test/makefile b/test/makefile index afcd46c..acde163 100644 --- a/test/makefile +++ b/test/makefile @@ -14,7 +14,7 @@ all: $(OBJ) $(TESTS:%.c=../$(TOY_OUTDIR)/%.exe) ../$(TOY_OUTDIR)/%.exe: $(ODIR)/%.o @$(CC) -o $@ $< $(TARGETS:../source/%.c=$(ODIR)/%.o) $(CFLAGS) $(LIBS) -ifeq ($(shell uname),Linux) +ifeq ($(shell uname)$(DISABLE_VALGRIND),Linux) valgrind --leak-check=full --track-origins=yes $@ else $@