From ea8284be823f480c7d748de939147686e0a133b6 Mon Sep 17 00:00:00 2001 From: Aleksandr Lebedev Date: Tue, 21 Apr 2026 16:59:35 +0200 Subject: [PATCH] Removed some code duplication --- main.c | 75 +++++++++++++++++++++++++++++----------------------------- 1 file changed, 38 insertions(+), 37 deletions(-) diff --git a/main.c b/main.c index 2ee9348..9865ec1 100644 --- a/main.c +++ b/main.c @@ -26,6 +26,33 @@ size_t min_size_t(size_t a, size_t b) return a > b ? b : a; } +/*Works simular to memcpy, but checks the sizes to avoid buffer overflows. If dest or src are NULL, returns 0, otherwise returns number of copied BYTES*/ +size_t copy_to_buf(void* dest, size_t dest_len, const void* src, size_t src_len) +{ + if(!dest || !src) + { + return 0; + } + size_t len = min_size_t(dest_len, src_len); + memcpy(dest, src, len); + return len; +} + +/*Works simular to strcpy, but checks the sizes to avoid buffer overflows. If dest or src are NULL, returns 0, otherwise returns number of copied BYTES*/ +size_t copy_str_to_buf(void* dest, size_t dest_len, const char* src) +{ + if(!dest || !src) + { + return 0; + } + size_t len = copy_to_buf(dest, dest_len, src, strlen(src) + 1); + if(len != 0) + { + ((char*)dest)[len] = '\0'; + } + return len; +} + void sigint_handler(int signo) { if(!jump_active) @@ -910,39 +937,33 @@ int execute_pipeline(struct pipeline* pl, int capture, char** out) char* get_user() { static char buf[max_env_var_length] = {0}; - char* result = getenv("USER"); - if(!result) + size_t len = copy_str_to_buf(buf, sizeof(buf), getenv("USER")); + if(!len) { return "UNKNOWN"; } - size_t length = min_size_t(sizeof(buf), strlen(result) + 1); - memcpy(buf, result, length); return buf; } char* get_home() { static char buf[max_length] = {0}; - char* result = getenv("HOME"); - if(!result) + size_t len = copy_str_to_buf(buf, sizeof(buf), getenv("HOME")); + if(!len) { return "UNKNOWN_HOME_PATH"; } - size_t length = min_size_t(sizeof(buf), strlen(result) + 1); - memcpy(buf, result, length); return buf; } char* get_pwd() { static char buf[max_length] = {0}; - char* result = getenv("PWD"); - if(!result) + size_t len = copy_str_to_buf(buf, sizeof(buf), getenv("PWD")); + if(!len) { return "UNKNOWN"; } - size_t length = min_size_t(sizeof(buf), strlen(result) + 1); - memcpy(buf, result, length); return buf; } @@ -976,21 +997,14 @@ char* generate_ps1_prompt() { static char prompt_buf[max_length] = {0}; char env_buf[max_env_var_length] = {0}; - char* ps1 = getenv("PS1"); - if(!ps1) - { - return default_prompt; - } - size_t ps1_len = min_size_t(sizeof(env_buf), strlen(ps1) + 1) - 1; + size_t ps1_len = copy_str_to_buf(env_buf, sizeof(env_buf), getenv("PS1")); if(ps1_len == 0) { return default_prompt; } - memcpy(env_buf, ps1, ps1_len); - env_buf[ps1_len + 1] = '\0'; if(ps1_len < 2) { - memcpy(prompt_buf, env_buf, ps1_len + 1); + copy_str_to_buf(prompt_buf, sizeof(prompt_buf), getenv("PS1")); return prompt_buf; } size_t i = 1, j = 0; @@ -1027,25 +1041,12 @@ char* generate_ps1_prompt() i++; continue; } - size_t len = strlen(data); - if(j + i - 1 - start + len >= sizeof(prompt_buf)) - { - fprintf(stderr, "\nOut of memory(1) for prompt: %d >= %d\n", j + i - start + len, sizeof(prompt_buf)); - break; - } - size_t start_len = min_size_t(sizeof(prompt_buf) - j, i - start - 1); - memcpy(prompt_buf + j, env_buf + start, start_len); + size_t start_len = copy_to_buf(prompt_buf + j, sizeof(prompt_buf) - j, env_buf + start, i - start - 1); j += start_len; - if(j + len >= sizeof(prompt_buf)) - { - fprintf(stderr, "\nOut of memory(2) for prompt: %d >= %d\n", j + len, sizeof(prompt_buf)); - break; - } - memcpy(prompt_buf + j, data, len); + size_t len = copy_to_buf(prompt_buf + j, sizeof(prompt_buf) - j, data, strlen(data)); i += 1; start = i; j += len; } - size_t start_len = min_size_t(sizeof(prompt_buf) - j, i - start); - memcpy(prompt_buf + j, env_buf + start, start_len); + size_t start_len = copy_to_buf(prompt_buf + j, sizeof(prompt_buf) - j, env_buf + start, i - start); j += start_len; prompt_buf[j] = '\0'; return prompt_buf;