From 0d57f3c8127bc7ec72296998bfd983a3c07c017c Mon Sep 17 00:00:00 2001 From: TapTap Date: Mon, 20 Jul 2026 19:52:48 +0200 Subject: [PATCH] =?UTF-8?q?fix:=20review=20fixes=20=E2=80=94=20getsockname?= =?UTF-8?q?,=20memcpy,=20clang-format?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/client/scanner.c | 16 ++++++++-------- src/client/scanner.h | 9 +++++---- src/shared/file.c | 4 ++-- src/shared/transport_tcp.c | 22 ++++++++++------------ src/shared/transport_tls.c | 2 +- tests/test_file_sendfile.c | 12 ++++++------ tests/test_log.c | 2 +- tests/test_multiprocessing.c | 20 ++++++++++---------- tests/test_scanner.c | 8 ++++---- 9 files changed, 47 insertions(+), 48 deletions(-) diff --git a/src/client/scanner.c b/src/client/scanner.c index cdad303..a6266ef 100644 --- a/src/client/scanner.c +++ b/src/client/scanner.c @@ -16,17 +16,17 @@ DirectoryScanner* directory_scanner_create(char* root_directory, bool use_metada int exclude_count, char** include_patterns, int include_count, unsigned long long max_size, unsigned long long min_size) { - return directory_scanner_create_full(root_directory, use_metadata, chunk_size, - exclude_patterns, exclude_count, - include_patterns, include_count, - max_size, min_size, true); + return directory_scanner_create_full(root_directory, use_metadata, chunk_size, exclude_patterns, + exclude_count, include_patterns, include_count, max_size, + min_size, true); } DirectoryScanner* directory_scanner_create_full(char* root_directory, bool use_metadata, - unsigned long long chunk_size, char** exclude_patterns, - int exclude_count, char** include_patterns, - int include_count, unsigned long long max_size, - unsigned long long min_size, bool follow_symlinks) { + unsigned long long chunk_size, + char** exclude_patterns, int exclude_count, + char** include_patterns, int include_count, + unsigned long long max_size, + unsigned long long min_size, bool follow_symlinks) { DirectoryScanner* scanner = malloc(sizeof(DirectoryScanner)); if (scanner == NULL) return NULL; diff --git a/src/client/scanner.h b/src/client/scanner.h index 166bc34..c163551 100644 --- a/src/client/scanner.h +++ b/src/client/scanner.h @@ -27,10 +27,11 @@ DirectoryScanner* directory_scanner_create(char* root_directory, bool use_metada int include_count, unsigned long long max_size, unsigned long long min_size); DirectoryScanner* directory_scanner_create_full(char* root_directory, bool use_metadata, - unsigned long long chunk_size, char** exclude_patterns, - int exclude_count, char** include_patterns, - int include_count, unsigned long long max_size, - unsigned long long min_size, bool follow_symlinks); + unsigned long long chunk_size, + char** exclude_patterns, int exclude_count, + char** include_patterns, int include_count, + unsigned long long max_size, + unsigned long long min_size, bool follow_symlinks); Chunk* directory_scanner_next(DirectoryScanner* scanner); void directory_scanner_destroy(DirectoryScanner* scanner); diff --git a/src/shared/file.c b/src/shared/file.c index a86b6c6..623c7f0 100644 --- a/src/shared/file.c +++ b/src/shared/file.c @@ -20,8 +20,8 @@ #include "protocol.h" #include "utils.h" -#define STREAM_THRESHOLD (64ULL * 1024 * 1024) /* 64 MB */ -#define STREAM_CHUNK_SIZE (1ULL * 1024 * 1024) /* 1 MB */ +#define STREAM_THRESHOLD (64ULL * 1024 * 1024) /* 64 MB */ +#define STREAM_CHUNK_SIZE (1ULL * 1024 * 1024) /* 1 MB */ File* file_create(const char* path) { File* file = (File*)malloc(sizeof(File)); diff --git a/src/shared/transport_tcp.c b/src/shared/transport_tcp.c index 8e4e9de..f130e38 100644 --- a/src/shared/transport_tcp.c +++ b/src/shared/transport_tcp.c @@ -44,9 +44,11 @@ Server* server_create(int port) { server->ssl_ctx = NULL; // Try IPv6 first, fall back to IPv4 - int fd = socket(AF_INET6, SOCK_STREAM, 0); + int domain = AF_INET6; + int fd = socket(domain, SOCK_STREAM, 0); if (fd < 0) { - fd = socket(AF_INET, SOCK_STREAM, 0); + domain = AF_INET; + fd = socket(domain, SOCK_STREAM, 0); } if (fd < 0) { perror("Could not create Socket!"); @@ -69,17 +71,11 @@ Server* server_create(int port) { return NULL; } - // Determine address family from the actual socket struct sockaddr_storage* addr = &server->address; - socklen_t addr_len = sizeof(*addr); - if (getsockname(fd, (struct sockaddr*)addr, &addr_len) == 0) { - // Use the family of the socket we actually created - } - struct sockaddr_in* addr4 = (struct sockaddr_in*)addr; struct sockaddr_in6* addr6 = (struct sockaddr_in6*)addr; - if (addr->ss_family == AF_INET6) { + if (domain == AF_INET6) { addr6->sin6_family = AF_INET6; addr6->sin6_addr = in6addr_any; addr6->sin6_port = htons(port); @@ -94,7 +90,7 @@ Server* server_create(int port) { if (bind(server->file_descriptor, (struct sockaddr*)&server->address, server->address_length) < 0) { // If IPv6 bind failed (maybe no IPv6), try IPv4 - if (addr->ss_family == AF_INET6) { + if (domain == AF_INET6) { close(fd); fd = socket(AF_INET, SOCK_STREAM, 0); if (fd < 0) { @@ -254,8 +250,10 @@ bool client_connect(Client* client, char* host, int port) { } // Save the connected address - memcpy(&client->address, rp->ai_addr, rp->ai_addrlen); - client->address_length = rp->ai_addrlen; + size_t copy_len = + rp->ai_addrlen < sizeof(client->address) ? rp->ai_addrlen : sizeof(client->address); + memcpy(&client->address, rp->ai_addr, copy_len); + client->address_length = (int)copy_len; freeaddrinfo(res); // Close old fd if any and set new one diff --git a/src/shared/transport_tls.c b/src/shared/transport_tls.c index ae1eddd..27cba91 100644 --- a/src/shared/transport_tls.c +++ b/src/shared/transport_tls.c @@ -34,7 +34,7 @@ static void log_ssl_errors(void) { } static SSL_CTX* create_ssl_ctx(bool is_server, const char* cert, const char* key, - const char* ca_path) { + const char* ca_path) { const SSL_METHOD* method = is_server ? TLS_server_method() : TLS_client_method(); SSL_CTX* ctx = SSL_CTX_new(method); if (!ctx) { diff --git a/tests/test_file_sendfile.c b/tests/test_file_sendfile.c index 3ef3835..24ab559 100644 --- a/tests/test_file_sendfile.c +++ b/tests/test_file_sendfile.c @@ -22,8 +22,8 @@ static void test_sendfile_basic() { /* Set the size so file_send_sendfile can report it */ file->data->size = len; - Config* cfg = config_create(str_dup(PROTOCOL_VERSION), str_dup("/tmp"), str_dup("/tmp"), - false, false, false, false, false, 0, false, 0); + Config* cfg = config_create(str_dup(PROTOCOL_VERSION), str_dup("/tmp"), str_dup("/tmp"), false, + false, false, false, false, 0, false, 0); EXPECT_NOT_NULL(cfg); int p[2]; @@ -80,8 +80,8 @@ static void test_sendfile_empty_file() { EXPECT_NOT_NULL(file); file->data->size = 0; - Config* cfg = config_create(str_dup(PROTOCOL_VERSION), str_dup("/tmp"), str_dup("/tmp"), - false, false, false, false, false, 0, false, 0); + Config* cfg = config_create(str_dup(PROTOCOL_VERSION), str_dup("/tmp"), str_dup("/tmp"), false, + false, false, false, false, 0, false, 0); EXPECT_NOT_NULL(cfg); int p[2]; @@ -161,8 +161,8 @@ static void test_sendfile_compression_fallback() { file->data->size = (size_t)st.st_size; EXPECT_TRUE(file_load_data(file)); - Config* cfg = config_create(str_dup(PROTOCOL_VERSION), str_dup("/tmp"), str_dup("/tmp"), - false, false, false, true, false, 3, false, 0); + Config* cfg = config_create(str_dup(PROTOCOL_VERSION), str_dup("/tmp"), str_dup("/tmp"), false, + false, false, true, false, 3, false, 0); EXPECT_NOT_NULL(cfg); int p[2]; diff --git a/tests/test_log.c b/tests/test_log.c index 4fc4798..dc73e77 100644 --- a/tests/test_log.c +++ b/tests/test_log.c @@ -47,7 +47,7 @@ static void test_log_set_level_info() { set_log_level(LOG_LEVEL_INFO); /* INFO level should show INFO, WARNING, ERROR but not DEBUG */ - log_message(LOG_LEVEL_DEBUG, "debug should be filtered"); /* filtered */ + log_message(LOG_LEVEL_DEBUG, "debug should be filtered"); /* filtered */ log_message(LOG_LEVEL_INFO, "info should show"); log_message(LOG_LEVEL_WARNING, "warning should show"); log_message(LOG_LEVEL_ERROR, "error should show"); diff --git a/tests/test_multiprocessing.c b/tests/test_multiprocessing.c index 95bc3ad..741bcf0 100644 --- a/tests/test_multiprocessing.c +++ b/tests/test_multiprocessing.c @@ -8,8 +8,8 @@ /* Test pipeline_context_sender_create/destroy with valid arguments */ static void test_sender_create_destroy() { - Config* cfg = config_create(str_dup("1.0"), str_dup("/src"), str_dup("/dst"), - false, false, false, false, false, 0, false, 0); + Config* cfg = config_create(str_dup("1.0"), str_dup("/src"), str_dup("/dst"), false, false, false, + false, false, 0, false, 0); EXPECT_NOT_NULL(cfg); Queue* q_scanner = queue_create(5, NULL); @@ -32,8 +32,8 @@ static void test_sender_create_destroy() { /* Test pipeline_context_receiver_create/destroy with valid arguments */ static void test_receiver_create_destroy() { - Config* cfg = config_create(str_dup("2.0"), str_dup("/src"), str_dup("/dst"), - true, true, false, false, false, 0, false, 0); + Config* cfg = config_create(str_dup("2.0"), str_dup("/src"), str_dup("/dst"), true, true, false, + false, false, 0, false, 0); EXPECT_NOT_NULL(cfg); Queue* q = queue_create(20, NULL); @@ -51,8 +51,8 @@ static void test_receiver_create_destroy() { /* Test that create handles various queue capacities */ static void test_sender_queue_capacities() { - Config* cfg = config_create(str_dup("3.0"), str_dup("/src"), str_dup("/dst"), - false, false, false, false, false, 0, false, 0); + Config* cfg = config_create(str_dup("3.0"), str_dup("/src"), str_dup("/dst"), false, false, false, + false, false, 0, false, 0); EXPECT_NOT_NULL(cfg); /* Single-element queues */ @@ -67,8 +67,8 @@ static void test_sender_queue_capacities() { /* Test that create handles zero-capacity queues */ static void test_sender_zero_capacity() { - Config* cfg = config_create(str_dup("4.0"), str_dup("/src"), str_dup("/dst"), - false, false, false, false, false, 0, false, 0); + Config* cfg = config_create(str_dup("4.0"), str_dup("/src"), str_dup("/dst"), false, false, false, + false, false, 0, false, 0); EXPECT_NOT_NULL(cfg); Queue* q1 = queue_create(0, NULL); @@ -82,8 +82,8 @@ static void test_sender_zero_capacity() { /* Test receiver with zero file_descriptor */ static void test_receiver_fd_zero() { - Config* cfg = config_create(str_dup("5.0"), str_dup("/src"), str_dup("/dst"), - false, false, false, false, false, 0, false, 0); + Config* cfg = config_create(str_dup("5.0"), str_dup("/src"), str_dup("/dst"), false, false, false, + false, false, 0, false, 0); Queue* q = queue_create(5, NULL); PipelineContextReceiver* ctx = pipeline_context_receiver_create(cfg, q, 0); EXPECT_NOT_NULL(ctx); diff --git a/tests/test_scanner.c b/tests/test_scanner.c index aaa8fcf..7ec4a9e 100644 --- a/tests/test_scanner.c +++ b/tests/test_scanner.c @@ -336,10 +336,10 @@ static void test_scanner_size_range() { static void test_scanner_mixed_patterns() { /* Combine exclude, include, and size filters together */ const char* dir = "test_scan_mixed"; - const char* a_txt = "test_scan_mixed/a.txt"; /* size ~= 5 */ - const char* b_bin = "test_scan_mixed/b.bin"; /* size ~= 13 */ - const char* c_txt = "test_scan_mixed/c.txt"; /* size ~= 5 */ - const char* d_bak = "test_scan_mixed/d.bak"; /* size ~= 42 */ + const char* a_txt = "test_scan_mixed/a.txt"; /* size ~= 5 */ + const char* b_bin = "test_scan_mixed/b.bin"; /* size ~= 13 */ + const char* c_txt = "test_scan_mixed/c.txt"; /* size ~= 5 */ + const char* d_bak = "test_scan_mixed/d.bak"; /* size ~= 42 */ mkdir(dir, 0755); create_test_file(a_txt, "aaaaa");