fix: memory safety — malloc NULL checks, strcpy→memcpy, str_dup NULL check, remove unused include
CI / lint (pull_request) Failing after 3s
CI / build-and-test (pull_request) Has been skipped
CI / sanitizers (address) (pull_request) Has been skipped
CI / sanitizers (undefined) (pull_request) Has been skipped
CI / fuzz-build (pull_request) Has been skipped
CI / coverage (pull_request) Has been skipped
CI / valgrind (pull_request) Has been skipped

This commit is contained in:
2026-07-20 21:26:01 +02:00
parent eb6034044b
commit 88ad77e397
6 changed files with 10 additions and 4 deletions
+5
View File
@@ -230,6 +230,11 @@ Chunk* directory_scanner_next(DirectoryScanner* scanner) {
if (link_len >= 0) { if (link_len >= 0) {
link_buf[link_len] = '\0'; link_buf[link_len] = '\0';
file->link_target = str_dup(link_buf); file->link_target = str_dup(link_buf);
if (file->link_target == NULL) {
file_destroy(file);
free(cur_path);
continue;
}
} }
file->data->size = 0; file->data->size = 0;
if (scanner->use_metadata) if (scanner->use_metadata)
+1
View File
@@ -14,6 +14,7 @@ Config* config_create(char* version, char* send_directory, char* receive_directo
bool use_sendfile, unsigned long long chunk_size) { bool use_sendfile, unsigned long long chunk_size) {
Config* config = malloc(sizeof(Config)); Config* config = malloc(sizeof(Config));
if (config == NULL) return NULL;
config->version = version; config->version = version;
config->send_directory = send_directory; config->send_directory = send_directory;
config->receive_root_directory = receive_directory; config->receive_root_directory = receive_directory;
+2 -2
View File
@@ -37,7 +37,7 @@ File* file_create(const char* path) {
return NULL; return NULL;
} }
strcpy(file->path, path); memcpy(file->path, path, path_len + 1);
file->data = data_create_reserve(0); file->data = data_create_reserve(0);
if (file->data == NULL) { if (file->data == NULL) {
free(file->path); free(file->path);
@@ -431,7 +431,7 @@ File* receive_incremental_check(int fd, const Config* config, bool* skipped) {
if (config->partial && !has_old_file && full_path) { if (config->partial && !has_old_file && full_path) {
char* partial_path = malloc(strlen(full_path) + 20); char* partial_path = malloc(strlen(full_path) + 20);
if (partial_path) { if (partial_path) {
sprintf(partial_path, "%s.fastsync-partial", full_path); snprintf(partial_path, strlen(full_path) + 20, "%s.fastsync-partial", full_path);
has_old_file = (stat(partial_path, &st) == 0); has_old_file = (stat(partial_path, &st) == 0);
if (has_old_file) if (has_old_file)
old_size = (unsigned long long)st.st_size; old_size = (unsigned long long)st.st_size;
+1
View File
@@ -41,6 +41,7 @@ FileMetadata* metadata_from_buf(char** buf) {
if (!present) if (!present)
return NULL; return NULL;
FileMetadata* m = malloc(sizeof(FileMetadata)); FileMetadata* m = malloc(sizeof(FileMetadata));
if (m == NULL) return NULL;
int32_t mode; int32_t mode;
memcpy(&mode, *buf, sizeof(mode)); memcpy(&mode, *buf, sizeof(mode));
*buf += sizeof(mode); *buf += sizeof(mode);
-1
View File
@@ -1,7 +1,6 @@
#include "protocol.h" #include "protocol.h"
#include "log.h" #include "log.h"
#include <errno.h> #include <errno.h>
#include <openssl/err.h>
#include <openssl/ssl.h> #include <openssl/ssl.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
+1 -1
View File
@@ -63,7 +63,7 @@ char* str_dup(const char* string) {
if (string == NULL) if (string == NULL)
return NULL; return NULL;
char* new_string = (char*)malloc(strlen(string) + 1); char* new_string = (char*)malloc(strlen(string) + 1);
strcpy(new_string, string); memcpy(new_string, string, strlen(string) + 1);
return new_string; return new_string;
} }