diff options
| -rw-r--r-- | 0001-chore-add-debug-symbols-and-memory-leak-detection.patch | 29 | ||||
| -rw-r--r-- | include/banned.h | 47 | ||||
| -rw-r--r-- | include/vars.h | 3 | ||||
| -rw-r--r-- | src/add_repo.c | 52 | ||||
| -rw-r--r-- | src/cmd_out.c | 66 |
5 files changed, 112 insertions, 85 deletions
diff --git a/0001-chore-add-debug-symbols-and-memory-leak-detection.patch b/0001-chore-add-debug-symbols-and-memory-leak-detection.patch deleted file mode 100644 index 787e29a..0000000 --- a/0001-chore-add-debug-symbols-and-memory-leak-detection.patch +++ /dev/null @@ -1,29 +0,0 @@ -From 7969c9218ccb947f564122582c5bbd65558c32c1 Mon Sep 17 00:00:00 2001 -From: aintea <aintea@protonmail.com> -Date: Mon, 29 Jun 2026 01:16:05 +0300 -Subject: [PATCH] chore: add debug symbols and memory leak detection - ---- - Makefile | 6 +++++- - 1 file changed, 5 insertions(+), 1 deletion(-) - -diff --git a/Makefile b/Makefile -index 021491e..51f5ef0 100644 ---- a/Makefile -+++ b/Makefile -@@ -27,7 +27,11 @@ CFLAGS += $(shell pkg-config --cflags luajit) -I./include -Wno-format-truncatio - default: pkgit - - pkgit: $(OBJS) -- ${CC} -o $@ $^ $(shell pkg-config --libs luajit) -+ ${CC} -o $@ $^ $(shell pkg-config --libs luajit) $(LDFLAGS) -+ -+debug: CFLAGS += -fsanitize=address -g3 -+debug: LDFLAGS += -fsanitize=address -+debug: pkgit - - $(OBJDIR): - @mkdir -p $(OBJDIR) --- -2.54.0 - diff --git a/include/banned.h b/include/banned.h new file mode 100644 index 0000000..2b934c8 --- /dev/null +++ b/include/banned.h @@ -0,0 +1,47 @@ +#ifndef BANNED_H +#define BANNED_H + +/* + * This header lists functions that have been banned from our code base, + * because they're too easy to misuse (and even if used correctly, + * complicate audits). Including this header turns them into compile-time + * errors. + */ + +#define BANNED(func) sorry_##func##_is_a_banned_function + +#undef strcpy +#define strcpy(x,y) BANNED(strcpy) +#undef strcat +#define strcat(x,y) BANNED(strcat) +#undef strncpy +#define strncpy(x,y,n) BANNED(strncpy) +#undef strncat +#define strncat(x,y,n) BANNED(strncat) +#undef strtok +#define strtok(x,y) BANNED(strtok) +#undef strtok_r +#define strtok_r(x,y,z) BANNED(strtok_r) + +#undef sprintf +#undef vsprintf +#define sprintf(...) BANNED(sprintf) +#define vsprintf(...) BANNED(vsprintf) + +#undef gmtime +#define gmtime(t) BANNED(gmtime) +#undef localtime +#define localtime(t) BANNED(localtime) +#undef ctime +#define ctime(t) BANNED(ctime) +#undef ctime_r +#define ctime_r(t, buf) BANNED(ctime_r) +#undef asctime +#define asctime(t) BANNED(asctime) +#undef asctime_r +#define asctime_r(t, buf) BANNED(asctime_r) + +#undef mktemp +#define mktemp(x) BANNED(mktemp) + +#endif /* BANNED_H */ diff --git a/include/vars.h b/include/vars.h index f9e5b37..c76b742 100644 --- a/include/vars.h +++ b/include/vars.h @@ -23,6 +23,7 @@ #include <stdbool.h> #include <stddef.h> +#include "banned.h" #define MAX_REPOS 1000 #define MAX_DIRS 100 @@ -142,4 +143,4 @@ void map_init(Map *map); void map_put(Map *map, char *key, char *value); char* map_get(Map *map, const char *key); -#endif
\ No newline at end of file +#endif diff --git a/src/add_repo.c b/src/add_repo.c index 52afc80..949c992 100644 --- a/src/add_repo.c +++ b/src/add_repo.c @@ -26,26 +26,32 @@ #include "vars.h" void add_repo(const char *repo, const char *repo_name) { - bool is_previous_repos = false; - char rfile_line[1024]; - char rfile_contents[8192] = {0}; - - if (file_exists(repo_file)) { - FILE *rfile = fopen(repo_file, "r"); - if (rfile) { - while (fgets(rfile_line, sizeof(rfile_line), rfile)) { - strcat(rfile_contents, rfile_line); - } - fclose(rfile); - is_previous_repos = true; - } - } - - char *previous_repos = is_previous_repos ? rfile_contents : ""; - - FILE *wfile = fopen(repo_file, "w"); - if (wfile) { - fprintf(wfile, "%srepositories[\"%s\"] = { url = \"%s\" }\n", previous_repos, repo_name, repo); - fclose(wfile); - } -}
\ No newline at end of file + bool is_previous_repos = false; + char rfile_line[1024]; + char rfile_contents[8192] = {0}; + + if (file_exists(repo_file)) { + FILE *rfile = fopen(repo_file, "r"); + if (rfile) { + while (fgets(rfile_line, sizeof(rfile_line), rfile)) { + size_t current_len = strlen(rfile_contents); + size_t remaining = sizeof(rfile_contents) - current_len; + if (remaining > 1) { + snprintf(rfile_contents + current_len, remaining, "%s", rfile_line); + } else { + break; + } + } + fclose(rfile); + is_previous_repos = true; + } + } + + char *previous_repos = is_previous_repos ? rfile_contents : ""; + + FILE *wfile = fopen(repo_file, "w"); + if (wfile) { + fprintf(wfile, "%srepositories[\"%s\"] = { url = \"%s\" }\n", previous_repos, repo_name, repo); + fclose(wfile); + } +} diff --git a/src/cmd_out.c b/src/cmd_out.c index bc371e0..f2d6bf4 100644 --- a/src/cmd_out.c +++ b/src/cmd_out.c @@ -26,35 +26,37 @@ #include "cmd_out.h" char* cmd_out(const char *cmd) { - FILE *pipe = popen(cmd, "r"); - if (!pipe) return strdup(""); - - char buffer[128]; - size_t total_size = 0; - size_t capacity = 256; - char *result = malloc(capacity); - if (!result) { - pclose(pipe); - return strdup(""); - } - result[0] = '\0'; - - while (fgets(buffer, sizeof(buffer), pipe) != NULL) { - size_t len = strlen(buffer); - if (total_size + len + 1 > capacity) { - capacity *= 2; - char *new_result = realloc(result, capacity); - if (!new_result) { - free(result); - pclose(pipe); - return strdup(""); - } - result = new_result; - } - strcat(result, buffer); - total_size += len; - } - - pclose(pipe); - return result; -}
\ No newline at end of file + FILE *pipe = popen(cmd, "r"); + if (!pipe) return strdup(""); + + char buffer[128]; + size_t total_size = 0; + size_t capacity = 256; + char *result = malloc(capacity); + if (!result) { + pclose(pipe); + return strdup(""); + } + result[0] = '\0'; + + while (fgets(buffer, sizeof(buffer), pipe) != NULL) { + size_t len = strlen(buffer); + // Ensure enough space for the new chunk + 1 byte for '\0' + if (total_size + len + 1 > capacity) { + capacity *= 2; + char *new_result = realloc(result, capacity); + if (!new_result) { + free(result); + pclose(pipe); + return strdup(""); + } + result = new_result; + } + + snprintf(result + total_size, capacity - total_size, "%s", buffer); + total_size += len; + } + + pclose(pipe); + return result; +} |
