From fe1de151ffe5538fa7678487983aff9bf5700f52 Mon Sep 17 00:00:00 2001 From: biglyderv Date: Thu, 27 Mar 2025 15:56:47 -0400 Subject: [PATCH] optimization on memory --- src/command.c | 5 +++++ src/loader.c | 11 +++++++---- src/main.c | 1 + src/main.h | 2 +- 4 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/command.c b/src/command.c index bdd3e1f..b3b52f5 100644 --- a/src/command.c +++ b/src/command.c @@ -111,6 +111,11 @@ int suggest_command(char *command, char *command_re, hashmap *polls, } page[strlen(page) - 1] = '\0'; + if (strstr(page,"\n") || strstr(page,";") || strstr(page,",") || strstr(page,"+")) { + printf("This element contains illegal characters.\n"); + return 1; + } + char *val = calloc(MAX_BUF_LENGTH, sizeof(char)); sprintf(val, "%s;%s", page, command_re); diff --git a/src/loader.c b/src/loader.c index 1a3eba4..ab1a071 100644 --- a/src/loader.c +++ b/src/loader.c @@ -32,12 +32,15 @@ int load_elements(hashmap *m, char *table, int use_inv) { continue; } - char *combo = strstr(str, ";"); - combo[0] = '\0'; - combo++; + char *combo_o = strstr(str, ";"); + combo_o[0] = '\0'; + combo_o++; + + char *combo = calloc(strlen(combo_o)+1,sizeof(char)); + strcpy(combo,combo_o); if (use_inv == 2) { - hashmap_set(m, str, strlen(str) - 1, (uintptr_t)combo, 0); + hashmap_set(m, str, strlen(str) - 1, (uintptr_t)combo, 1); continue; } diff --git a/src/main.c b/src/main.c index 561a50e..4ca5f1f 100644 --- a/src/main.c +++ b/src/main.c @@ -25,6 +25,7 @@ void init_tables(hashmap *elements, hashmap *inv, hashmap *polls, int do_inv) { load_elements(inv, inv_file, 1); } +// todo: sanitize, valgrind, look through todos int main(int argc, char *argv[]) { char *name; if (argc < 2) { diff --git a/src/main.h b/src/main.h index 02691b9..199f669 100644 --- a/src/main.h +++ b/src/main.h @@ -7,5 +7,5 @@ #define MAX_BUF_LENGTH 1024 #define MAX_COMBO_LENGTH 1024 #define UPVOTE_IN 2 -#define MAX_FILE_SIZE 1024 * 16 +#define MAX_FILE_SIZE 1024 long stoi(const char *s); \ No newline at end of file