From 3923421224204c395b8cbcf19d88f186a740cab0 Mon Sep 17 00:00:00 2001 From: TapTap Date: Mon, 20 Jul 2026 19:53:15 +0200 Subject: [PATCH] =?UTF-8?q?fix:=20review=20fixes=20=E2=80=94=20getsockname?= =?UTF-8?q?,=20test=20assertion,=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 | 15 ++++++++------- tests/test_log.c | 2 +- tests/test_multiprocessing.c | 20 ++++++++++---------- tests/test_scanner.c | 8 ++++---- 9 files changed, 51 insertions(+), 47 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 934292b..545ebe9 100644 --- a/src/shared/transport_tcp.c +++ b/src/shared/transport_tcp.c @@ -44,8 +44,10 @@ Server* server_create(int port) { // Try IPv6 first, fall back to IPv4 int fd = socket(AF_INET6, SOCK_STREAM, 0); + sa_family_t domain = AF_INET6; if (fd < 0) { fd = socket(AF_INET, SOCK_STREAM, 0); + domain = AF_INET; } if (fd < 0) { perror("Could not create Socket!"); @@ -69,32 +71,29 @@ Server* server_create(int port) { return NULL; } - // Determine address family from the actual socket + // Use the domain from the socket we actually created 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); + addr->ss_family = AF_INET6; server->address_length = sizeof(struct sockaddr_in6); } else { addr4->sin_family = AF_INET; addr4->sin_addr.s_addr = INADDR_ANY; addr4->sin_port = htons(port); + addr->ss_family = AF_INET; server->address_length = sizeof(struct sockaddr_in); } 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 +253,11 @@ 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; + socklen_t addr_len = rp->ai_addrlen; + if (addr_len > sizeof(client->address)) + addr_len = sizeof(client->address); + memcpy(&client->address, rp->ai_addr, addr_len); + client->address_length = addr_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 2b40bab..1126daf 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]; @@ -226,7 +226,8 @@ static void test_sendfile_no_path() { close(p[1]); /* When send_path is false, the sender still sends file_type + data */ int file_type; - receive_int(p[0], &file_type); + EXPECT_TRUE(receive_int(p[0], &file_type)); + EXPECT_EQ_INT(file_type, (int)FILE_TYPE_REGULAR); Data* received = receive_data(p[0]); close(p[0]); 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");