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().
This commit is contained in:
2026-07-30 18:49:31 +02:00
parent e876055167
commit 88746c3396
2 changed files with 14 additions and 9 deletions
+2 -1
View File
@@ -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);
+12 -8
View File
@@ -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;
}