From a51ba7c169d5b200d9e3038fab6b2ea2cffd4ac0 Mon Sep 17 00:00:00 2001 From: TapTap Date: Thu, 30 Jul 2026 18:49:31 +0200 Subject: [PATCH] fix: replace strcpy with bounded memory operations (#195) Replace all uses of strcpy() with memcpy() + explicit NUL termination or direct assignment for safety and consistency. No behavioral changes. src/shared/file.c: - file_create(): strcpy -> memcpy + explicit NUL (buffer size known) src/shared/utils.c: - mkdir_r(): strcpy -> memcpy for path_duplicate - mkdir_r(): strcpy(path_current, "/") -> direct assignment - mkdir_r(): strcpy loop -> memcpy + direct assignment - str_dup(): strcpy -> memcpy (buffer size known) PR #196 (dry-run manifest refactoring) was already applied in a previous commit - send_dry_run_manifest() and send_delete_manifest() helpers already exist and are used by both send_files() and send_files_multithreaded(). --- src/shared/file.c | 3 ++- src/shared/utils.c | 20 ++++++++++++-------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/shared/file.c b/src/shared/file.c index 62082ff..874c603 100644 --- a/src/shared/file.c +++ b/src/shared/file.c @@ -35,7 +35,8 @@ File* file_create(const char* path) { return NULL; } - strcpy(file->path, path); + memcpy(file->path, path, path_len); + file->path[path_len] = '\0'; file->data = data_create_reserve(0); if (file->data == NULL) { free(file->path); diff --git a/src/shared/utils.c b/src/shared/utils.c index 755d695..3840c3c 100644 --- a/src/shared/utils.c +++ b/src/shared/utils.c @@ -13,7 +13,7 @@ bool mkdir_r(const char* path) { char* path_duplicate = malloc(strlen(path) + 1); if (!path_duplicate) return false; - strcpy(path_duplicate, path); + memcpy(path_duplicate, path, strlen(path) + 1); char* path_current = (char*)malloc((strlen(path) + 2) * sizeof(char)); if (!path_current) { free(path_duplicate); @@ -21,7 +21,8 @@ bool mkdir_r(const char* path) { } char* path_current_position = path_current; if (path[0] == '/') { - strcpy(path_current, "/"); + path_current[0] = '/'; + path_current[1] = '\0'; path_current_position += 1; } else { path_current[0] = '\0'; @@ -31,10 +32,12 @@ bool mkdir_r(const char* path) { const char* part = strtok_r(path_duplicate, delimiter, &saveptr); bool ok = true; while (part != NULL) { - strcpy(path_current_position, part); - path_current_position += strlen(part) * sizeof(char); - strcpy(path_current_position, "/"); - path_current_position += sizeof(char); + size_t part_len = strlen(part); + memcpy(path_current_position, part, part_len); + path_current_position += part_len; + path_current_position[0] = '/'; + path_current_position[1] = '\0'; + path_current_position++; struct stat st; if (stat(path_current, &st) != 0) { if (mkdir(path_current, 0755) != 0) { @@ -53,8 +56,9 @@ bool mkdir_r(const char* path) { char* str_dup(const char* string) { if (string == NULL) return NULL; - char* new_string = (char*)malloc(strlen(string) + 1); - strcpy(new_string, string); + size_t str_len = strlen(string); + char* new_string = (char*)malloc(str_len + 1); + memcpy(new_string, string, str_len + 1); return new_string; }