From 3fc51ee0c8135d95ec422dadd6562bf97261a757 Mon Sep 17 00:00:00 2001 From: jvech Date: Thu, 19 Sep 2024 16:22:19 -0500 Subject: fix: wrong memory allocation on csv_read() fixed --- Makefile | 12 +++++++----- src/parse.c | 8 ++++---- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index b829f3d..b8efec3 100644 --- a/Makefile +++ b/Makefile @@ -3,8 +3,10 @@ include config.mk # you can `export DEV_MODE=true` to compile the binaries with more warnings and debugging support ifdef DEV_MODE CFLAGS = -std=gnu11 -Wall -Wextra -g +LDFLAGS = #-fsanitize=address else CFLAGS = -std=gnu11 -Wall -O2 +LDFLAGS = endif CC = clang @@ -27,7 +29,7 @@ $(OBJDIR)/%.o: src/%.c $(HEADERS) ${CC} -c -o $@ $< ${CFLAGS} build: $(OBJS) - ${CC} ${DLIBS} -o ${BIN} ${OBJS} + ${CC} ${DLIBS} -o ${BIN} ${OBJS} ${LDFLAGS} install: all @# binary @@ -58,15 +60,15 @@ test_%: src/%.c $(OBJDIR) $(shell sed -n 's/.*compile: clang/clang/;/clang/p' $<) debug: build - gdb -x utils/commands.gdb --tui --args ${BIN} train data/xor.json -e 100 + gdb --tui --args ./${BIN} train -c utils/settings.cfg data/xor.csv @#gdb -x utils/commands.gdb --tui --args ${BIN} predict data/sample_data.json check_leaks: build - valgrind --leak-check=yes \ - --log-file=leaks.log \ + setarch x86_64 -R valgrind --log-file=leaks.log \ --leak-check=full \ --show-leak-kinds=all \ - ./${BIN} train -c utils/settings.cfg data/xor.json + --track-origins=yes \ + ./${BIN} train -c utils/settings.cfg data/xor.csv clean: @rm $(OBJDIR) -rv diff --git a/src/parse.c b/src/parse.c index 6aa5563..d11d006 100644 --- a/src/parse.c +++ b/src/parse.c @@ -429,8 +429,8 @@ void csv_read( n_values_buffer = n_in_keys + n_out_keys; values_buffer = ecalloc(n_values_buffer, sizeof(char *)); - in_indexes = ecalloc(n_in_keys, sizeof(char)); - out_indexes = ecalloc(n_out_keys, sizeof(char)); + in_indexes = ecalloc(n_in_keys, sizeof(size_t)); + out_indexes = ecalloc(n_out_keys, sizeof(size_t)); if (fp == NULL) die("csv_read() Error:"); @@ -486,12 +486,12 @@ void csv_read( for (i = 0; i < n_in_keys; i++) { key_index = util_get_key_index(in_keys[i], values_buffer, n_values_buffer); - in_indexes[i] = has_header ? key_index : i; + in_indexes[i] = has_header ? (size_t)key_index : i; } for (i = 0; i < n_out_keys && read_output; i++) { key_index = util_get_key_index(out_keys[i], values_buffer, n_values_buffer); - out_indexes[i] = has_header ? key_index : i + n_in_keys; + out_indexes[i] = has_header ? (size_t)key_index : i + n_in_keys; } } -- cgit v1.2.3-70-g09d2