From 3e65bd58c5e4e01ae80eacd17ceb01398316dc7a Mon Sep 17 00:00:00 2001 From: Evan Burkey Date: Tue, 13 Aug 2024 13:19:28 -0700 Subject: [PATCH] fix compiler warnings --- Makefile | 15 +++++++++++++++ include/advent_utility.h | 2 -- lib/libflint | 2 +- src/2015/04.c | 5 +++-- src/2015/06.c | 2 +- src/2015/07.c | 9 +++++---- src/2023/02.c | 2 +- src/advent_utility.c | 15 ++------------- 8 files changed, 28 insertions(+), 24 deletions(-) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..cd7732d --- /dev/null +++ b/Makefile @@ -0,0 +1,15 @@ +CMAKE_OPTS=-DCMAKE_EXPORT_COMPILE_COMMANDS=1 + +.PHONY: all clean run + +all: + mkdir -p build + cd build && \ + cmake ${CMAKE_OPTS} .. && \ + $(MAKE) && \ + cp compile_commands.json .. + +clean: + rm -rf build + rm -f compile_commands.json + diff --git a/include/advent_utility.h b/include/advent_utility.h index a84ed04..a9ba6c5 100644 --- a/include/advent_utility.h +++ b/include/advent_utility.h @@ -10,8 +10,6 @@ char *md5_str(const char *); -char *capture_system(const char *); - Vector *string_to_int_vector(const char *input_string, const char *delim); #endif diff --git a/lib/libflint b/lib/libflint index 34b21c4..4b59b47 160000 --- a/lib/libflint +++ b/lib/libflint @@ -1 +1 @@ -Subproject commit 34b21c494b6b9fcc9e3c9e7d98bfea6114eefa41 +Subproject commit 4b59b4789cd743dc874399784a1918e9f11086d1 diff --git a/src/2015/04.c b/src/2015/04.c index 9e6364b..4c9d50c 100644 --- a/src/2015/04.c +++ b/src/2015/04.c @@ -30,8 +30,9 @@ void advent2015day04(void) { int c = 0, five = 0, six = 0; while (!five || !six) { - char test[strlen(input) + 8]; - sprintf(test, "%s%d", input, c); + int sz = strlen(input) + 8; + char test[sz]; + snprintf(test, sz, "%s%d", input, c); char *md5 = md5_str(test); if (!five && is_five(md5)) { diff --git a/src/2015/06.c b/src/2015/06.c index 2207174..57dd9c1 100644 --- a/src/2015/06.c +++ b/src/2015/06.c @@ -91,7 +91,7 @@ void advent2015day06(void) { s1503 ins[sz]; for (size_t i = 0; i < sz; ++i) { char *s = malloc(sizeof(char) * 64); - strcpy(s, input[i]); + strncpy(s, input[i], 64); size_t sp_sz = 0; char **sp = split(s, &sp_sz, " "); diff --git a/src/2015/07.c b/src/2015/07.c index c1b8735..8242cbc 100644 --- a/src/2015/07.c +++ b/src/2015/07.c @@ -26,7 +26,7 @@ static void set_wire(const char *name, int v) { HASH_FIND_STR(wires, name, w); if (w == NULL) { w = malloc(sizeof(struct Wire)); - strcpy(w->name, name); + strlcpy(w->name, name, 8); HASH_ADD_STR(wires, name, w); } w->val = v; @@ -68,11 +68,11 @@ void advent2015day07(void) { int a_set = LFFALSE; while (!a_set) { - char *buf = malloc(sizeof(char) * strlen(input[i]) + 1); - strcpy(buf, input[i]); + int sz = strlen(input[i]) + 1; + char *buf = malloc(sizeof(char) * sz); + strlcpy(buf, input[i], sz); size_t sp_sz = 0; char **sp = split(buf, &sp_sz, " "); - int s = 0; if (sp_sz == 3) { uint16_t a = get_val(sp[0]); @@ -114,4 +114,5 @@ void advent2015day07(void) { printf("%u\n", get_wire("a")->val); del_lines(input); + clear_wires(); } diff --git a/src/2023/02.c b/src/2023/02.c index eab5eac..c7d1466 100644 --- a/src/2023/02.c +++ b/src/2023/02.c @@ -17,7 +17,7 @@ static void parse(char *input, struct Game *g) { sscanf(input, "Game %d:", &g->id); const char *c = strchr(input, ':') + 1; char cpy[1024]; - strcpy(cpy, c); + strncpy(cpy, c, 1024); char *token = strtok(cpy, ",;"); while (token != NULL) { diff --git a/src/advent_utility.c b/src/advent_utility.c index ae92d4f..88d5f15 100644 --- a/src/advent_utility.c +++ b/src/advent_utility.c @@ -29,22 +29,11 @@ char *md5_str(const char *input) { char *md5string = malloc(33); for (int i = 0; i < 16; ++i) { - sprintf(&md5string[i * 2], "%02x", (unsigned int) digest[i]); + snprintf(&md5string[i * 2], 33, "%02x", (unsigned int) digest[i]); } return md5string; } -char *capture_system(const char *cmd) { - char *buf = malloc(256); - FILE *tmp = popen(cmd, "r"); - if (tmp == NULL) { - printf("failed to open FILE *tmp\n"); - exit(1); - } - fgets(buf, 256, tmp); - return buf; -} - Vector *string_to_int_vector(const char *input_string, const char* delim) { char *copy = strdup(input_string); char* token = strtok(copy, delim); @@ -54,7 +43,7 @@ Vector *string_to_int_vector(const char *input_string, const char* delim) { while (token != NULL) { int *i = malloc(sizeof(int)); *i = atoi(token); - vec_push(v, i); + vec_push(v, i, NULL); token = strtok(NULL, delim); }