fix: logic/correctness bugs — issues #73, #68, #67, #66, #59, #58, #54, #48, #53
CI / lint (pull_request) Failing after 7s
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 19:04:35 +02:00
parent ddfdb3825a
commit 4383caa3bb
9 changed files with 159 additions and 55 deletions
+16 -2
View File
@@ -88,7 +88,14 @@ int main(int argc, char* argv[]) {
} else if (strcmp(argv[i], "-n") == 0 || strcmp(argv[i], "--dry-run") == 0) { } else if (strcmp(argv[i], "-n") == 0 || strcmp(argv[i], "--dry-run") == 0) {
config->dry_run = true; config->dry_run = true;
} else if (strcmp(argv[i], "-p") == 0 && i + 1 < argc) { } else if (strcmp(argv[i], "-p") == 0 && i + 1 < argc) {
config->ssh_port = atoi(argv[++i]); char* end;
long p = strtol(argv[++i], &end, 10);
if (*end != '\0' || p <= 0 || p > 65535) {
fprintf(stderr, "Error: invalid SSH port '%s' (must be 1-65535)\n", argv[i]);
exit_code = 1;
goto cleanup;
}
config->ssh_port = (int)p;
} else if (strcmp(argv[i], "--delete") == 0) { } else if (strcmp(argv[i], "--delete") == 0) {
config->use_delete = true; config->use_delete = true;
} else if (strcmp(argv[i], "--exclude") == 0 && i + 1 < argc) { } else if (strcmp(argv[i], "--exclude") == 0 && i + 1 < argc) {
@@ -165,7 +172,14 @@ int main(int argc, char* argv[]) {
free(config->server_host); free(config->server_host);
config->server_host = str_dup(argv[++i]); config->server_host = str_dup(argv[++i]);
} else if (strcmp(argv[i], "--server-port") == 0 && i + 1 < argc) { } else if (strcmp(argv[i], "--server-port") == 0 && i + 1 < argc) {
config->server_port = atoi(argv[++i]); char* end;
long p = strtol(argv[++i], &end, 10);
if (*end != '\0' || p <= 0 || p > 65535) {
fprintf(stderr, "Error: invalid server port '%s' (must be 1-65535)\n", argv[i]);
exit_code = 1;
goto cleanup;
}
config->server_port = (int)p;
} else if (strcmp(argv[i], "--bwlimit") == 0 && i + 1 < argc) { } else if (strcmp(argv[i], "--bwlimit") == 0 && i + 1 < argc) {
char* end; char* end;
errno = 0; errno = 0;
+8 -4
View File
@@ -113,13 +113,11 @@ void handler(int file_descriptor) {
} }
static Server* g_server = NULL; static Server* g_server = NULL;
static volatile sig_atomic_t g_server_cleanup_requested = 0;
static void cleanup(int sig) { static void cleanup(int sig) {
(void)sig; (void)sig;
if (g_server) { g_server_cleanup_requested = 1;
server_delete(&g_server);
}
_exit(0);
} }
static void print_server_usage(void) { static void print_server_usage(void) {
@@ -205,5 +203,11 @@ int main(int argc, char* argv[]) {
} else { } else {
server_listen(g_server, handler); server_listen(g_server, handler);
} }
/* Graceful shutdown: if a signal requested cleanup, delete the server */
if (g_server_cleanup_requested) {
log_message(LOG_LEVEL_INFO, "Shutdown requested, cleaning up");
server_delete(&g_server);
}
return 0; return 0;
} }
+12
View File
@@ -9,6 +9,18 @@
Data* data_compress(Data* data_to_compress, int compression_level) { Data* data_compress(Data* data_to_compress, int compression_level) {
log_message(LOG_LEVEL_DEBUG, "Starting to compress data"); log_message(LOG_LEVEL_DEBUG, "Starting to compress data");
/* Clamp compression level to valid zstd range [1, 22] */
if (compression_level < 1) {
log_message(LOG_LEVEL_WARNING, "compression_level %d out of range [1,22], using 1",
compression_level);
compression_level = 1;
} else if (compression_level > 22) {
log_message(LOG_LEVEL_WARNING, "compression_level %d out of range [1,22], using 22",
compression_level);
compression_level = 22;
}
size_t dst_size = ZSTD_compressBound(data_to_compress->size); size_t dst_size = ZSTD_compressBound(data_to_compress->size);
Data* compressed_data = data_create_empty(dst_size); Data* compressed_data = data_create_empty(dst_size);
if (compressed_data == NULL) if (compressed_data == NULL)
+41 -40
View File
@@ -155,6 +155,43 @@ static void* old_data_from_path(const char* full_path, unsigned long long old_si
return data; return data;
} }
/**
* Helper: receive data from wire, optionally decompress, and store in file.
* On success, returns the received Data* (caller owns it). On failure, returns NULL.
* If `file_data` is received via receive_data(fd), this function handles decompression
* when config->use_compression is set.
*/
static Data* receive_and_decompress(int fd, const Config* config) {
Data* file_data = receive_data(fd);
if (file_data == NULL)
return NULL;
if (config->use_compression) {
Data* uncompressed = data_decompress(file_data);
data_destroy(file_data);
if (uncompressed == NULL)
return NULL;
file_data = uncompressed;
}
return file_data;
}
/**
* Helper: receive metadata from wire and assign to file.
* Returns true on success (metadata may be NULL if absent), false on I/O error.
*/
static bool receive_and_assign_metadata(int fd, const Config* config, File* file) {
if (!config->use_metadata)
return true;
int meta_ok = 1;
file->metadata = metadata_receive(fd, &meta_ok);
if (!meta_ok) {
file_destroy(file);
send_status(fd, STATUS_ERROR);
return false;
}
return true;
}
static File* receive_delta_file(int fd, const Config* config, const char* check_path, static File* receive_delta_file(int fd, const Config* config, const char* check_path,
void* old_data, unsigned long long old_size) { void* old_data, unsigned long long old_size) {
if (!old_data) if (!old_data)
@@ -270,34 +307,16 @@ static File* receive_delta_file(int fd, const Config* config, const char* check_
return NULL; return NULL;
} }
if (config->use_metadata) { if (!receive_and_assign_metadata(fd, config, file))
int meta_ok = 1;
file->metadata = metadata_receive(fd, &meta_ok);
if (!meta_ok) {
file_destroy(file);
send_status(fd, STATUS_ERROR);
return NULL; return NULL;
}
}
Data* file_data = receive_data(fd); Data* file_data = receive_and_decompress(fd, config);
if (file_data == NULL) { if (file_data == NULL) {
file_destroy(file); file_destroy(file);
send_status(fd, STATUS_ERROR); send_status(fd, STATUS_ERROR);
return NULL; return NULL;
} }
if (config->use_compression) {
Data* uncompressed = data_decompress(file_data);
data_destroy(file_data);
if (uncompressed == NULL) {
file_destroy(file);
send_status(fd, STATUS_ERROR);
return NULL;
}
file_data = uncompressed;
}
data_destroy(file->data); data_destroy(file->data);
file->data = file_data; file->data = file_data;
return file; return file;
@@ -375,34 +394,16 @@ File* receive_incremental_check(int fd, const Config* config, bool* skipped) {
return NULL; return NULL;
} }
if (config->use_metadata) { if (!receive_and_assign_metadata(fd, config, file))
int meta_ok = 1;
file->metadata = metadata_receive(fd, &meta_ok);
if (!meta_ok) {
file_destroy(file);
send_status(fd, STATUS_ERROR);
return NULL; return NULL;
}
}
Data* file_data = receive_data(fd); Data* file_data = receive_and_decompress(fd, config);
if (file_data == NULL) { if (file_data == NULL) {
file_destroy(file); file_destroy(file);
send_status(fd, STATUS_ERROR); send_status(fd, STATUS_ERROR);
return NULL; return NULL;
} }
if (config->use_compression) {
Data* uncompressed = data_decompress(file_data);
data_destroy(file_data);
if (uncompressed == NULL) {
file_destroy(file);
send_status(fd, STATUS_ERROR);
return NULL;
}
file_data = uncompressed;
}
data_destroy(file->data); data_destroy(file->data);
file->data = file_data; file->data = file_data;
return file; return file;
+1 -1
View File
@@ -10,7 +10,7 @@ void set_log_level(LogLevel level) {
current_log_level = level; current_log_level = level;
} }
void log_message(LogLevel log_level, char* format, ...) { void log_message(LogLevel log_level, const char* format, ...) {
if (log_level < current_log_level) if (log_level < current_log_level)
return; return;
time_t now = time(NULL); time_t now = time(NULL);
+1 -1
View File
@@ -3,7 +3,7 @@
typedef enum { LOG_LEVEL_DEBUG, LOG_LEVEL_INFO, LOG_LEVEL_WARNING, LOG_LEVEL_ERROR } LogLevel; typedef enum { LOG_LEVEL_DEBUG, LOG_LEVEL_INFO, LOG_LEVEL_WARNING, LOG_LEVEL_ERROR } LogLevel;
void log_message(LogLevel log_level, char* message, ...); void log_message(LogLevel log_level, const char* message, ...);
void set_log_level(LogLevel level); void set_log_level(LogLevel level);
#endif #endif
+20 -4
View File
@@ -1,6 +1,7 @@
#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>
@@ -34,11 +35,14 @@ static void bw_throttle(size_t bytes_written) {
struct timespec now; struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now); clock_gettime(CLOCK_MONOTONIC, &now);
long long elapsed_ns = /* Use unsigned long long for elapsed_ns to avoid overflow in multiplication.
(now.tv_sec - bw_last_refill.tv_sec) * 1000000000LL + (now.tv_nsec - bw_last_refill.tv_nsec); * 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);
bw_last_refill = now; bw_last_refill = now;
long long tokens_to_add = (long long)((double)io_bwlimit * elapsed_ns / 1000000000.0); long long tokens_to_add = (long long)((double)io_bwlimit * (double)elapsed_ns / 1000000000.0);
bw_tokens += tokens_to_add; bw_tokens += tokens_to_add;
if (bw_tokens > (long long)io_bwlimit) if (bw_tokens > (long long)io_bwlimit)
bw_tokens = (long long)io_bwlimit; bw_tokens = (long long)io_bwlimit;
@@ -46,7 +50,9 @@ static void bw_throttle(size_t bytes_written) {
bw_tokens -= (long long)bytes_written; bw_tokens -= (long long)bytes_written;
if (bw_tokens < 0) { if (bw_tokens < 0) {
long long deficit_ns = (long long)((double)(-bw_tokens) / io_bwlimit * 1000000000.0); long long deficit_ns = (long long)((double)(-bw_tokens) / (double)io_bwlimit * 1000000000.0);
if (deficit_ns < 0)
deficit_ns = 0;
struct timespec sleep_time, remaining; struct timespec sleep_time, remaining;
sleep_time.tv_sec = deficit_ns / 1000000000LL; sleep_time.tv_sec = deficit_ns / 1000000000LL;
sleep_time.tv_nsec = deficit_ns % 1000000000LL; sleep_time.tv_nsec = deficit_ns % 1000000000LL;
@@ -79,6 +85,11 @@ bool send_n_data(int file_descriptor, const void* data, size_t data_size) {
else else
bytes_send = write(fd, (const char*)data + total_bytes_send, chunk); bytes_send = write(fd, (const char*)data + total_bytes_send, chunk);
if (bytes_send <= 0) { 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"); log_message(LOG_LEVEL_ERROR, "Could not send data");
return false; return false;
} }
@@ -102,6 +113,11 @@ bool receive_n_data(int file_descriptor, void* data, size_t data_size) {
bytes_received = bytes_received =
read(fd, (char*)data + total_bytes_received, data_size - total_bytes_received); read(fd, (char*)data + total_bytes_received, data_size - total_bytes_received);
if (bytes_received <= 0) { 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) if (bytes_received == 0)
log_message(LOG_LEVEL_ERROR, "Connection closed while receiving data"); log_message(LOG_LEVEL_ERROR, "Connection closed while receiving data");
else else
+27 -1
View File
@@ -70,6 +70,23 @@ char* str_dup(const char* string) {
bool glob_match(const char* pattern, const char* str) { bool glob_match(const char* pattern, const char* str) {
while (*pattern) { while (*pattern) {
if (*pattern == '*') { if (*pattern == '*') {
/* Check for double-star (globstar) pattern */
if (*(pattern + 1) == '*') {
pattern += 2;
/* Trailing double-star matches everything */
if (*pattern == '\0')
return true;
/* double-star slash: match at any depth */
if (*pattern == '/')
pattern++;
while (*str) {
if (glob_match(pattern, str))
return true;
str++;
}
return glob_match(pattern, str);
}
/* Single * — does not cross / boundaries */
pattern++; pattern++;
while (*str && *str != '/') { while (*str && *str != '/') {
if (glob_match(pattern, str)) if (glob_match(pattern, str))
@@ -83,8 +100,17 @@ bool glob_match(const char* pattern, const char* str) {
pattern++; pattern++;
str++; str++;
} else { } else {
if (*pattern != *str) if (*pattern != *str) {
/* If pattern has a '/' followed by '**', allow zero path components */
if (*pattern == '/' && *(pattern + 1) == '*' && *(pattern + 2) == '*') {
/* Skip over slash-double-star and try to match rest against current str */
const char* rest = pattern + 3;
if (*rest == '/')
rest++;
return glob_match(rest, str);
}
return false; return false;
}
pattern++; pattern++;
str++; str++;
} }
+31
View File
@@ -49,6 +49,33 @@ static void test_glob_question_star() {
EXPECT_TRUE(glob_match("?*.txt", "a.txt")); EXPECT_TRUE(glob_match("?*.txt", "a.txt"));
} }
static void test_glob_doublestar_match_all() {
EXPECT_TRUE(glob_match("**", "anything"));
EXPECT_TRUE(glob_match("**", "path/to/file"));
}
static void test_glob_doublestar_prefix() {
EXPECT_TRUE(glob_match("**/foo", "foo"));
EXPECT_TRUE(glob_match("**/foo", "bar/foo"));
EXPECT_TRUE(glob_match("**/foo", "a/b/c/foo"));
EXPECT_FALSE(glob_match("**/foo", "foobar"));
EXPECT_FALSE(glob_match("**/foo", "bar/foobar"));
}
static void test_glob_doublestar_suffix() {
EXPECT_TRUE(glob_match("foo/**", "foo"));
EXPECT_TRUE(glob_match("foo/**", "foo/bar"));
EXPECT_TRUE(glob_match("foo/**", "foo/bar/baz"));
EXPECT_FALSE(glob_match("foo/**", "foobar"));
}
static void test_glob_doublestar_mid() {
EXPECT_TRUE(glob_match("a/**/b", "a/b"));
EXPECT_TRUE(glob_match("a/**/b", "a/x/b"));
EXPECT_TRUE(glob_match("a/**/b", "a/x/y/z/b"));
EXPECT_FALSE(glob_match("a/**/b", "a/x/bad"));
}
void test_glob() { void test_glob() {
test_glob_exact_match(); test_glob_exact_match();
test_glob_question_mark(); test_glob_question_mark();
@@ -60,4 +87,8 @@ void test_glob() {
test_glob_slash_not_matched(); test_glob_slash_not_matched();
test_glob_complex(); test_glob_complex();
test_glob_question_star(); test_glob_question_star();
test_glob_doublestar_match_all();
test_glob_doublestar_prefix();
test_glob_doublestar_suffix();
test_glob_doublestar_mid();
} }