diff --git a/src/shared/data.c b/src/shared/data.c index e0e3156..82e6198 100644 --- a/src/shared/data.c +++ b/src/shared/data.c @@ -1,6 +1,6 @@ #include "data.h" #include "log.h" -#include "stdlib.h" +#include Data* data_create_empty(size_t data_size) { /* malloc(0) is UB; allocate at least 1 byte but preserve requested size */ diff --git a/src/shared/protocol.c b/src/shared/protocol.c index f07b8a1..1e5cb7f 100644 --- a/src/shared/protocol.c +++ b/src/shared/protocol.c @@ -34,14 +34,11 @@ static void bw_throttle(size_t bytes_written) { struct timespec now; clock_gettime(CLOCK_MONOTONIC, &now); - /* Use unsigned long long for elapsed_ns to avoid overflow in multiplication. - * time_t differences fit comfortably in 64-bit for any practical runtime. */ - unsigned long long elapsed_ns = - (unsigned long long)(now.tv_sec - bw_last_refill.tv_sec) * 1000000000ULL + - (unsigned long long)(now.tv_nsec - bw_last_refill.tv_nsec); + long long elapsed_ns = + (now.tv_sec - bw_last_refill.tv_sec) * 1000000000LL + (now.tv_nsec - bw_last_refill.tv_nsec); bw_last_refill = now; - long long tokens_to_add = (long long)((double)io_bwlimit * (double)elapsed_ns / 1000000000.0); + long long tokens_to_add = (long long)((double)io_bwlimit * elapsed_ns / 1000000000.0); bw_tokens += tokens_to_add; if (bw_tokens > (long long)io_bwlimit) bw_tokens = (long long)io_bwlimit; @@ -49,9 +46,7 @@ static void bw_throttle(size_t bytes_written) { bw_tokens -= (long long)bytes_written; if (bw_tokens < 0) { - long long deficit_ns = (long long)((double)(-bw_tokens) / (double)io_bwlimit * 1000000000.0); - if (deficit_ns < 0) - deficit_ns = 0; + long long deficit_ns = (long long)((double)(-bw_tokens) / io_bwlimit * 1000000000.0); struct timespec sleep_time, remaining; sleep_time.tv_sec = deficit_ns / 1000000000LL; sleep_time.tv_nsec = deficit_ns % 1000000000LL; @@ -84,11 +79,6 @@ bool send_n_data(int file_descriptor, const void* data, size_t data_size) { else bytes_send = write(fd, (const char*)data + total_bytes_send, chunk); if (bytes_send <= 0) { - if (io_ssl) { - int ssl_err = SSL_get_error(io_ssl, (int)bytes_send); - if (ssl_err == SSL_ERROR_WANT_WRITE || ssl_err == SSL_ERROR_WANT_READ) - continue; - } log_message(LOG_LEVEL_ERROR, "Could not send data"); return false; } @@ -112,11 +102,6 @@ bool receive_n_data(int file_descriptor, void* data, size_t data_size) { bytes_received = read(fd, (char*)data + total_bytes_received, data_size - total_bytes_received); if (bytes_received <= 0) { - if (io_ssl) { - int ssl_err = SSL_get_error(io_ssl, (int)bytes_received); - if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) - continue; - } if (bytes_received == 0) log_message(LOG_LEVEL_ERROR, "Connection closed while receiving data"); else @@ -193,18 +178,18 @@ bool send_data(int file_descriptor, const Data* data) { return false; if (!send_n_data(file_descriptor, data->data, data_size)) return false; - log_message(LOG_LEVEL_DEBUG, "Send %lld data", data_size); + log_message(LOG_LEVEL_DEBUG, "Send %llu data", data_size); return true; } -#define MAX_DATA_SIZE (1024ULL * 1024 * 1024) // 1 GB +#define MAX_DATA_SIZE (1024ULL * 1024 * 1024) Data* receive_data(int file_descriptor) { unsigned long long size = 0; if (!receive_n_data(file_descriptor, &size, sizeof(unsigned long long))) return NULL; - if (size > MAX_DATA_SIZE) { - log_message(LOG_LEVEL_ERROR, "receive_data: size %llu exceeds maximum", size); + if ((size_t)size != size || size > MAX_DATA_SIZE) { + log_message(LOG_LEVEL_ERROR, "receive_data size %llu exceeds limits", size); return NULL; } void* data = malloc((size_t)size); @@ -214,7 +199,7 @@ Data* receive_data(int file_descriptor) { free(data); return NULL; } - log_message(LOG_LEVEL_DEBUG, "Received %lld data", size); + log_message(LOG_LEVEL_DEBUG, "Received %llu data", size); return data_create(data, (size_t)size); } diff --git a/src/shared/utils.c b/src/shared/utils.c index 680a6b2..a5b67cd 100644 --- a/src/shared/utils.c +++ b/src/shared/utils.c @@ -134,7 +134,7 @@ static void delete_extras_walk(const char* abs_path, const char* rel_path, Array if (!dir) return; bool all_removed = true; - struct dirent* entry; + const struct dirent* entry; while ((entry = readdir(dir)) != NULL) { if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) continue; @@ -184,18 +184,17 @@ void delete_extras(const char* dest_root, ArrayList* manifest) { delete_extras_walk(dest_root, "", manifest); } -char* path_cat(const char* path1, char* path2) { +char* path_cat(const char* path1, const char* path2) { if (path1 == NULL || *path1 == '\0') return str_dup(path2); if (path2 == NULL || *path2 == '\0') return str_dup(path1); int path1_len = strlen(path1); int path2_len = strlen(path2); - char* path2_pointer = path2; if (path1[path1_len - 1] == '/') path1_len -= 1; if (path2[0] == '/') { - path2_pointer += 1; + path2++; path2_len -= 1; } char* new_path = malloc(path1_len + path2_len + 2); @@ -203,7 +202,7 @@ char* path_cat(const char* path1, char* path2) { return NULL; memcpy(new_path, path1, path1_len); new_path[path1_len] = '/'; - memcpy(new_path + path1_len + 1, path2_pointer, path2_len); + memcpy(new_path + path1_len + 1, path2, path2_len); new_path[path1_len + path2_len + 1] = '\0'; return new_path; } diff --git a/src/shared/utils.h b/src/shared/utils.h index 13cd999..1cc8a6c 100644 --- a/src/shared/utils.h +++ b/src/shared/utils.h @@ -6,7 +6,7 @@ bool mkdir_r(const char* path); char* str_dup(const char* string); -char* path_cat(const char* path1, char* path2); +char* path_cat(const char* path1, const char* path2); bool glob_match(const char* pattern, const char* str); void delete_extras(const char* dest_root, ArrayList* manifest); diff --git a/tests/test_protocol.c b/tests/test_protocol.c index f644032..bece803 100644 --- a/tests/test_protocol.c +++ b/tests/test_protocol.c @@ -179,8 +179,7 @@ static void test_receive_str_oversized() { size_t huge = MAX_STRING_SIZE + 1; EXPECT_TRUE(send_n_data(0, &huge, sizeof(size_t))); - /* cppcheck-suppress constVariablePointer */ - char* received = receive_str(0); + const char* received = receive_str(0); EXPECT_NULL(received); close(p[0]);