From 0e7c2cafaa06e5b84d44f0d01d4906189bf992db Mon Sep 17 00:00:00 2001 From: Andrew Haynes Date: Wed, 14 Oct 2026 13:20:58 -0400 Subject: [PATCH] CHORE: Bugfixing --- Makefile | 16 +++++++++++++++- include/libvec/char.h | 4 ++-- include/libvec/float.h | 4 ++-- include/libvec/int.h | 4 ++-- include/libvec/string.h | 6 +++--- src/string.c | 33 ++++++++++++++++++++++++++------- src/utils.c | 2 +- 7 files changed, 51 insertions(+), 18 deletions(-) diff --git a/Makefile b/Makefile index 168fd2c..afdf81b 100644 --- a/Makefile +++ b/Makefile @@ -7,6 +7,9 @@ LLVM_PROFDATA := $(LLVM_DIR)/llvm-profdata LLVM_COV := $(LLVM_DIR)/llvm-cov CPPCHECK := cppcheck +# libstr is populated by CMake's FetchContent (run `cmake -B build` once) +LIBSTR_DIR := build/_deps/libstr-src + # ============================================================================== # Flags & Directories # ============================================================================== @@ -35,6 +38,9 @@ SRCS := src/lib/int/int.c src/lib/utils/utils.c TEST_SRC := tests/test_learn.c FUZZ_SRC := tests/fuzz_char.c +STRING_SRCS := src/string.c src/utils.c $(LIBSTR_DIR)/src/string.c +TEST_STRING_SRC := tests/test_string.c + BIN_DIR := tests/bin CACHE_DIR := tests/cache @@ -42,11 +48,12 @@ TEST_BIN := $(BIN_DIR)/test_learn_runner TSAN_BIN := $(BIN_DIR)/test_tsan_runner FUZZ_BIN := $(BIN_DIR)/fuzz_char_runner COV_BIN := $(BIN_DIR)/test_cov_runner +TEST_STRING_BIN := $(BIN_DIR)/test_string_runner # ============================================================================== # Targets & Workflow Rules # ============================================================================== -.PHONY: all test sanitize check check-sources analyze cppcheck fuzz cov-build cov clean +.PHONY: all test test-string sanitize check check-sources analyze cppcheck fuzz cov-build cov clean all: test @@ -58,6 +65,9 @@ $(BIN_DIR) $(CACHE_DIR): $(TEST_BIN): $(SRCS) $(TEST_SRC) | $(BIN_DIR) $(CC) $(CFLAGS) $(SAN_FLAGS) $(INCLUDES) $^ -o $@ +$(TEST_STRING_BIN): $(STRING_SRCS) $(TEST_STRING_SRC) | $(BIN_DIR) + $(CC) $(CFLAGS) $(SAN_FLAGS) $(INCLUDES) -I$(LIBSTR_DIR)/include $^ -o $@ + $(TSAN_BIN): $(SRCS) $(TEST_SRC) | $(BIN_DIR) $(CC) $(CFLAGS) -fsanitize=thread $(INCLUDES) $^ -o $@ @@ -70,6 +80,10 @@ test: $(TEST_BIN) @echo "=== Running Unit Tests (ASan + UBSan) ===" @$(ASAN_ENV) ./$(TEST_BIN) +test-string: $(TEST_STRING_BIN) + @echo "=== Running vec_string Unit Tests (ASan + UBSan) ===" + @$(ASAN_ENV) ./$(TEST_STRING_BIN) + sanitize: $(TEST_BIN) $(TSAN_BIN) @echo "\n=== [1/2] Running Address + UndefinedBehavior Sanitizers ===" @$(ASAN_ENV) ./$(TEST_BIN) diff --git a/include/libvec/char.h b/include/libvec/char.h index 2df6caa..b7f0cbb 100644 --- a/include/libvec/char.h +++ b/include/libvec/char.h @@ -1,5 +1,5 @@ -#ifndef CHAR_H -#define CHAR_H +#ifndef LIBVEC_CHAR_H +#define LIBVEC_CHAR_H #include diff --git a/include/libvec/float.h b/include/libvec/float.h index 68ba77e..a0d738c 100644 --- a/include/libvec/float.h +++ b/include/libvec/float.h @@ -1,5 +1,5 @@ -#ifndef CHAR_H -#define CHAR_H +#ifndef LIBVEC_FLOAT_H +#define LIBVEC_FLOAT_H #include diff --git a/include/libvec/int.h b/include/libvec/int.h index a389052..626842c 100644 --- a/include/libvec/int.h +++ b/include/libvec/int.h @@ -1,5 +1,5 @@ -#ifndef INT_H -#define INT_H +#ifndef LIBVEC_INT_H +#define LIBVEC_INT_H #include diff --git a/include/libvec/string.h b/include/libvec/string.h index c0d49e0..7ff1f58 100644 --- a/include/libvec/string.h +++ b/include/libvec/string.h @@ -1,5 +1,5 @@ -#ifndef CHAR_H -#define CHAR_H +#ifndef LIBVEC_STRING_H +#define LIBVEC_STRING_H #include #include @@ -24,7 +24,7 @@ void vec_string_print_vec(const vec_string* ptr, err_t* err); vec_string -vec_string_cpy(const char* src, const size_t src_size, err_t* err); +vec_string_cpy(const String* src, const size_t src_size, err_t* err); void vec_string_push_back(vec_string* ptr, const String val, err_t* err); diff --git a/src/string.c b/src/string.c index 82c53ce..728b885 100644 --- a/src/string.c +++ b/src/string.c @@ -27,6 +27,24 @@ __null_check(const vec_string* ptr, err_t* status) return status->flags; } +__attribute__((overloadable)) size_t +__null_check(const String* ptr, err_t* status) +{ + if (status == nullptr) + { + return ERR_ERR_T_NULL; + } + if (ptr == nullptr) + { + status->flags |= ERR_STR_NULL; + } + if (ptr->data == nullptr) + { + status->flags |= ERR_BUF_NULL; + } + return status->flags; +} + void vec_string_print_vec(const vec_string* ptr, err_t* err) { @@ -42,7 +60,7 @@ vec_string_print_vec(const vec_string* ptr, err_t* err) } vec_string -vec_string_cpy(const char* src, const size_t src_size, err_t* err) +vec_string_cpy(const String* src, const size_t src_size, err_t* err) { vec_string self = VEC_INIT; err_t status = ERR_T_INIT; @@ -328,7 +346,7 @@ vec_string_clear(vec_string* ptr, err_t* err) } if (ptr->size > 0) { - memset(ptr->array, 0, ptr->size); + memset(ptr->array, 0, ptr->size * (sizeof(String))); ptr->size = 0; } } @@ -433,9 +451,9 @@ vec_string_empty(const vec_string* ptr, err_t* err) } if (ptr->size == 0) { - return ptr->size; + return 1; } - return status.flags; + return 0; } __attribute__((overloadable)) void @@ -450,6 +468,8 @@ vec_string_erase(vec_string* ptr, const size_t iter, err_t* err) } if (iter >= ptr->size) { + __origin_override(err, "ERASE"); + err->flags |= ERR_OUT_OF_BOUNDS; return; } memmove(&ptr->array[iter], &ptr->array[iter + 1], @@ -697,8 +717,7 @@ vec_string_assign(vec_string* ptr, const vec_string* iterator, err->flags |= ERR_OUT_OF_BOUNDS; return; } - vec_string_erase(ptr, vec_string_begin(ptr, &status), - vec_string_end(ptr, &status) + 1, &status); + vec_string_erase(ptr, 0, ptr->size, &status); if (status.flags != 0) { __origin_override(err, "ASSIGN"); @@ -839,7 +858,7 @@ vec_string_insert(vec_string* ptr, const size_t iter, const size_t amount, { err_t status = ERR_T_INIT; __null_check(ptr, &status); - if ((status.flags & (ERR_VEC_NULL | ERR_BUF_NULL)) != 0) + if ((status.flags & ERR_VEC_NULL) != 0) { __origin_override(err, "INSERT"); err->flags |= status.flags; diff --git a/src/utils.c b/src/utils.c index 7765cf9..06d5cc4 100644 --- a/src/utils.c +++ b/src/utils.c @@ -78,7 +78,7 @@ __origin_override(err_t* status, char* str) } __attribute__((overloadable)) size_t -__null_check(const float* ptr, err_t* status) +__null_check(const char* ptr, err_t* status) { if (status == nullptr) {