Compare commits

..

1 Commits

Author SHA1 Message Date
TapTap 5eaea9d92e refactor: extract CLI parser helpers, fix validation bugs
CI / lint (pull_request) Successful in 16s
CI / sanitizers (undefined) (pull_request) Successful in 37s
CI / sanitizers (address) (pull_request) Successful in 39s
CI / fuzz-build (pull_request) Successful in 14s
CI / coverage (pull_request) Successful in 32s
CI / build-and-test (pull_request) Successful in 1m15s
CI / valgrind (pull_request) Successful in 33s
- Add set_string_option(), set_positive_int_option(), set_nonneg_int_option()
  helpers to eliminate duplicated alloc/free/assign patterns
- Replace 14 string-option branches with set_string_option() calls
- Replace 7 integer-option branches with set_positive/nonneg_int_option()
- Fix --info/--debug: replace atoi() with validated parse
- Fix --max-size/--min-size: add strtoull() error checking
- Fix --chunk-size: validate input with error message on failure
2026-08-01 18:12:41 +02:00
3 changed files with 90 additions and 124 deletions
+81 -110
View File
@@ -49,6 +49,37 @@ static bool parse_nonneg_int(const char* s, int* out_val) {
return true; return true;
} }
/* Duplicate a string argument into *dest, freeing the old value. Returns 0 on success, -1 on
* failure. */
static int set_string_option(char** dest, const char* value, const char* option_name) {
char* dup = str_dup(value);
if (!dup) {
fprintf(stderr, "Error: memory allocation failed for %s\n", option_name);
return -1;
}
free(*dest);
*dest = dup;
return 0;
}
/* Parse a string as a positive integer into *dest. Returns 0 on success, -1 on error. */
static int set_positive_int_option(int* dest, const char* value, const char* option_name) {
if (!parse_positive_int(value, dest)) {
fprintf(stderr, "Error: %s must be a positive integer\n", option_name);
return -1;
}
return 0;
}
/* Parse a string as a non-negative integer into *dest. Returns 0 on success, -1 on error. */
static int set_nonneg_int_option(int* dest, const char* value, const char* option_name) {
if (!parse_nonneg_int(value, dest)) {
fprintf(stderr, "Error: %s must be a non-negative integer\n", option_name);
return -1;
}
return 0;
}
static void print_usage(void); static void print_usage(void);
static int read_patterns_from_file(const char* filepath, char*** patterns, int* count); static int read_patterns_from_file(const char* filepath, char*** patterns, int* count);
@@ -100,9 +131,23 @@ static int parse_args(Config* config, int argc, char* argv[], int* positional_ar
} }
config->include_patterns[config->include_count++] = dup; config->include_patterns[config->include_count++] = dup;
} else if (strcmp(argv[i], "--max-size") == 0 && i + 1 < argc) { } else if (strcmp(argv[i], "--max-size") == 0 && i + 1 < argc) {
config->max_size = strtoull(argv[++i], NULL, 10); char* end;
errno = 0;
unsigned long long val = strtoull(argv[++i], &end, 10);
if (errno != 0 || *end != '\0') {
fprintf(stderr, "Error: --max-size must be a non-negative integer\n");
return -1;
}
config->max_size = val;
} else if (strcmp(argv[i], "--min-size") == 0 && i + 1 < argc) { } else if (strcmp(argv[i], "--min-size") == 0 && i + 1 < argc) {
config->min_size = strtoull(argv[++i], NULL, 10); char* end;
errno = 0;
unsigned long long val = strtoull(argv[++i], &end, 10);
if (errno != 0 || *end != '\0') {
fprintf(stderr, "Error: --min-size must be a non-negative integer\n");
return -1;
}
config->min_size = val;
} else if (strcmp(argv[i], "--incremental") == 0) { } else if (strcmp(argv[i], "--incremental") == 0) {
config->use_incremental = true; config->use_incremental = true;
} else if (strcmp(argv[i], "--delta") == 0) { } else if (strcmp(argv[i], "--delta") == 0) {
@@ -132,21 +177,11 @@ static int parse_args(Config* config, int argc, char* argv[], int* positional_ar
} }
} }
} else if (strcmp(argv[i], "--source-dir") == 0 && i + 1 < argc) { } else if (strcmp(argv[i], "--source-dir") == 0 && i + 1 < argc) {
char* dup = str_dup(argv[++i]); if (set_string_option(&config->send_directory, argv[++i], "--source-dir") != 0)
if (!dup) {
fprintf(stderr, "Error: memory allocation failed for --source-dir\n");
return -1; return -1;
}
free(config->send_directory);
config->send_directory = dup;
} else if (strcmp(argv[i], "--dest-dir") == 0 && i + 1 < argc) { } else if (strcmp(argv[i], "--dest-dir") == 0 && i + 1 < argc) {
char* dup = str_dup(argv[++i]); if (set_string_option(&config->receive_root_directory, argv[++i], "--dest-dir") != 0)
if (!dup) {
fprintf(stderr, "Error: memory allocation failed for --dest-dir\n");
return -1; return -1;
}
free(config->receive_root_directory);
config->receive_root_directory = dup;
} else if (strcmp(argv[i], "--save-to-disk") == 0) { } else if (strcmp(argv[i], "--save-to-disk") == 0) {
config->save_to_disk = true; config->save_to_disk = true;
} else if (strcmp(argv[i], "-M") == 0 || strcmp(argv[i], "--preserve") == 0) { } else if (strcmp(argv[i], "-M") == 0 || strcmp(argv[i], "--preserve") == 0) {
@@ -162,13 +197,8 @@ static int parse_args(Config* config, int argc, char* argv[], int* positional_ar
config->use_chunk_serialization = true; config->use_chunk_serialization = true;
log_message(LOG_LEVEL_INFO, "Enabled Chunk Serialization"); log_message(LOG_LEVEL_INFO, "Enabled Chunk Serialization");
} else if (strcmp(argv[i], "--server-host") == 0 && i + 1 < argc) { } else if (strcmp(argv[i], "--server-host") == 0 && i + 1 < argc) {
char* dup = str_dup(argv[++i]); if (set_string_option(&config->server_host, argv[++i], "--server-host") != 0)
if (!dup) {
fprintf(stderr, "Error: memory allocation failed for --server-host\n");
return -1; return -1;
}
free(config->server_host);
config->server_host = dup;
} else if (strcmp(argv[i], "--server-port") == 0 && i + 1 < argc) { } else if (strcmp(argv[i], "--server-port") == 0 && i + 1 < argc) {
if (!parse_positive_int(argv[++i], &config->server_port)) { if (!parse_positive_int(argv[++i], &config->server_port)) {
fprintf(stderr, "Error: invalid --server-port value: %s\n", argv[i]); fprintf(stderr, "Error: invalid --server-port value: %s\n", argv[i]);
@@ -191,69 +221,44 @@ static int parse_args(Config* config, int argc, char* argv[], int* positional_ar
} else if (strcmp(argv[i], "--progress") == 0) { } else if (strcmp(argv[i], "--progress") == 0) {
config->show_progress = true; config->show_progress = true;
} else if (strcmp(argv[i], "--chunk-size") == 0 && i + 1 < argc) { } else if (strcmp(argv[i], "--chunk-size") == 0 && i + 1 < argc) {
unsigned long long val = strtoull(argv[++i], NULL, 10); char* end;
if (val > 0) errno = 0;
config->chunk_size = val; unsigned long long val = strtoull(argv[++i], &end, 10);
if (errno != 0 || *end != '\0' || val == 0) {
fprintf(stderr, "Error: --chunk-size must be a positive integer\n");
return -1;
}
config->chunk_size = val;
} else if (strcmp(argv[i], "--tls") == 0) { } else if (strcmp(argv[i], "--tls") == 0) {
config->use_tls = true; config->use_tls = true;
} else if (strcmp(argv[i], "--cert") == 0 && i + 1 < argc) { } else if (strcmp(argv[i], "--cert") == 0 && i + 1 < argc) {
char* dup = str_dup(argv[++i]); if (set_string_option(&config->tls_cert, argv[++i], "--cert") != 0)
if (!dup) {
fprintf(stderr, "Error: memory allocation failed for --cert\n");
return -1; return -1;
}
free(config->tls_cert);
config->tls_cert = dup;
} else if (strcmp(argv[i], "--key") == 0 && i + 1 < argc) { } else if (strcmp(argv[i], "--key") == 0 && i + 1 < argc) {
char* dup = str_dup(argv[++i]); if (set_string_option(&config->tls_key, argv[++i], "--key") != 0)
if (!dup) {
fprintf(stderr, "Error: memory allocation failed for --key\n");
return -1; return -1;
}
free(config->tls_key);
config->tls_key = dup;
} else if (strcmp(argv[i], "--ca") == 0 && i + 1 < argc) { } else if (strcmp(argv[i], "--ca") == 0 && i + 1 < argc) {
char* dup = str_dup(argv[++i]); if (set_string_option(&config->tls_ca, argv[++i], "--ca") != 0)
if (!dup) {
fprintf(stderr, "Error: memory allocation failed for --ca\n");
return -1; return -1;
}
free(config->tls_ca);
config->tls_ca = dup;
} else if (strcmp(argv[i], "--timeout") == 0 && i + 1 < argc) { } else if (strcmp(argv[i], "--timeout") == 0 && i + 1 < argc) {
int val; if (set_positive_int_option(&config->timeout, argv[++i], "--timeout") != 0)
if (!parse_positive_int(argv[++i], &val)) {
fprintf(stderr, "Error: --timeout must be a positive integer\n");
return -1; return -1;
}
config->timeout = val;
} else if (strcmp(argv[i], "--contimeout") == 0 && i + 1 < argc) { } else if (strcmp(argv[i], "--contimeout") == 0 && i + 1 < argc) {
int val; if (set_positive_int_option(&config->contimeout, argv[++i], "--contimeout") != 0)
if (!parse_positive_int(argv[++i], &val)) {
fprintf(stderr, "Error: --contimeout must be a positive integer\n");
return -1; return -1;
}
config->contimeout = val;
} else if (strcmp(argv[i], "-q") == 0 || strcmp(argv[i], "--quiet") == 0 || } else if (strcmp(argv[i], "-q") == 0 || strcmp(argv[i], "--quiet") == 0 ||
strcmp(argv[i], "--silent") == 0) { strcmp(argv[i], "--silent") == 0) {
config->quiet = true; config->quiet = true;
} else if (strcmp(argv[i], "--backup") == 0) { } else if (strcmp(argv[i], "--backup") == 0) {
config->backup = true; config->backup = true;
} else if (strcmp(argv[i], "--backup-dir") == 0 && i + 1 < argc) { } else if (strcmp(argv[i], "--backup-dir") == 0 && i + 1 < argc) {
char* dup = str_dup(argv[++i]); if (set_string_option(&config->backup_dir, argv[++i], "--backup-dir") != 0)
if (!dup) {
fprintf(stderr, "Error: memory allocation failed for --backup-dir\n");
return -1; return -1;
}
free(config->backup_dir);
config->backup_dir = dup;
} else if (strcmp(argv[i], "--stats") == 0) { } else if (strcmp(argv[i], "--stats") == 0) {
config->stats = true; config->stats = true;
} else if (strcmp(argv[i], "--max-depth") == 0 && i + 1 < argc) { } else if (strcmp(argv[i], "--max-depth") == 0 && i + 1 < argc) {
if (!parse_nonneg_int(argv[++i], &config->max_depth)) { if (set_nonneg_int_option(&config->max_depth, argv[++i], "--max-depth") != 0)
fprintf(stderr, "Error: --max-depth must be a non-negative integer\n");
return -1; return -1;
}
} else if (strcmp(argv[i], "--log-file") == 0 && i + 1 < argc) { } else if (strcmp(argv[i], "--log-file") == 0 && i + 1 < argc) {
FILE* lf = fopen(argv[++i], "a"); FILE* lf = fopen(argv[++i], "a");
if (!lf) { if (!lf) {
@@ -263,12 +268,8 @@ static int parse_args(Config* config, int argc, char* argv[], int* positional_ar
config->log_file = lf; config->log_file = lf;
log_set_file(lf); log_set_file(lf);
} else if (strcmp(argv[i], "--queue-size") == 0 && i + 1 < argc) { } else if (strcmp(argv[i], "--queue-size") == 0 && i + 1 < argc) {
int val; if (set_positive_int_option(&config->queue_size, argv[++i], "--queue-size") != 0)
if (!parse_positive_int(argv[++i], &val)) {
fprintf(stderr, "Error: --queue-size must be a positive integer\n");
return -1; return -1;
}
config->queue_size = val;
} else if (strcmp(argv[i], "--exclude-from") == 0 && i + 1 < argc) { } else if (strcmp(argv[i], "--exclude-from") == 0 && i + 1 < argc) {
if (read_patterns_from_file(argv[++i], &config->exclude_patterns, &config->exclude_count) != if (read_patterns_from_file(argv[++i], &config->exclude_patterns, &config->exclude_count) !=
0) 0)
@@ -280,13 +281,9 @@ static int parse_args(Config* config, int argc, char* argv[], int* positional_ar
} else if (strcmp(argv[i], "--partial") == 0) { } else if (strcmp(argv[i], "--partial") == 0) {
config->partial = true; config->partial = true;
} else if (strcmp(argv[i], "--fastsync-server-path") == 0 && i + 1 < argc) { } else if (strcmp(argv[i], "--fastsync-server-path") == 0 && i + 1 < argc) {
char* dup = str_dup(argv[++i]); if (set_string_option(&config->fastsync_server_path, argv[++i], "--fastsync-server-path") !=
if (!dup) { 0)
fprintf(stderr, "Error: memory allocation failed for --fastsync-server-path\n");
return -1; return -1;
}
free(config->fastsync_server_path);
config->fastsync_server_path = dup;
} else if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--verbose") == 0) { } else if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--verbose") == 0) {
set_log_level(LOG_LEVEL_DEBUG); set_log_level(LOG_LEVEL_DEBUG);
} else if (strcmp(argv[i], "-l") == 0 || strcmp(argv[i], "--links") == 0) { } else if (strcmp(argv[i], "-l") == 0 || strcmp(argv[i], "--links") == 0) {
@@ -310,15 +307,14 @@ static int parse_args(Config* config, int argc, char* argv[], int* positional_ar
} else if (strcmp(argv[i], "-i") == 0 || strcmp(argv[i], "--itemize-changes") == 0) { } else if (strcmp(argv[i], "-i") == 0 || strcmp(argv[i], "--itemize-changes") == 0) {
config->itemize_changes = true; config->itemize_changes = true;
} else if (strcmp(argv[i], "--out-format") == 0 && i + 1 < argc) { } else if (strcmp(argv[i], "--out-format") == 0 && i + 1 < argc) {
char* dup = str_dup(argv[++i]); if (set_string_option(&config->out_format, argv[++i], "--out-format") != 0)
if (!dup)
return -1; return -1;
free(config->out_format);
config->out_format = dup;
} else if (strcmp(argv[i], "--info") == 0 && i + 1 < argc) { } else if (strcmp(argv[i], "--info") == 0 && i + 1 < argc) {
config->info_level = atoi(argv[++i]); if (set_nonneg_int_option(&config->info_level, argv[++i], "--info") != 0)
return -1;
} else if (strcmp(argv[i], "--debug") == 0 && i + 1 < argc) { } else if (strcmp(argv[i], "--debug") == 0 && i + 1 < argc) {
config->debug_level = atoi(argv[++i]); if (set_nonneg_int_option(&config->debug_level, argv[++i], "--debug") != 0)
return -1;
} else if (strcmp(argv[i], "--list-only") == 0) { } else if (strcmp(argv[i], "--list-only") == 0) {
config->list_only = true; config->list_only = true;
} else if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--human-readable") == 0) { } else if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--human-readable") == 0) {
@@ -336,12 +332,8 @@ static int parse_args(Config* config, int argc, char* argv[], int* positional_ar
} else if (strcmp(argv[i], "--delete-after") == 0) { } else if (strcmp(argv[i], "--delete-after") == 0) {
config->delete_after = true; config->delete_after = true;
} else if (strcmp(argv[i], "--max-delete") == 0 && i + 1 < argc) { } else if (strcmp(argv[i], "--max-delete") == 0 && i + 1 < argc) {
int val; if (set_nonneg_int_option(&config->max_delete, argv[++i], "--max-delete") != 0)
if (!parse_nonneg_int(argv[++i], &val)) {
fprintf(stderr, "Error: --max-delete must be a non-negative integer\n");
return -1; return -1;
}
config->max_delete = val;
} else if (strcmp(argv[i], "--filter") == 0 && i + 1 < argc) { } else if (strcmp(argv[i], "--filter") == 0 && i + 1 < argc) {
if (!config->filters) if (!config->filters)
config->filters = array_list_create(free); config->filters = array_list_create(free);
@@ -350,11 +342,8 @@ static int parse_args(Config* config, int argc, char* argv[], int* positional_ar
return -1; return -1;
array_list_add(config->filters, dup); array_list_add(config->filters, dup);
} else if (strcmp(argv[i], "--files-from") == 0 && i + 1 < argc) { } else if (strcmp(argv[i], "--files-from") == 0 && i + 1 < argc) {
char* dup = str_dup(argv[++i]); if (set_string_option(&config->files_from, argv[++i], "--files-from") != 0)
if (!dup)
return -1; return -1;
free(config->files_from);
config->files_from = dup;
} else if (strcmp(argv[i], "--cvs-exclude") == 0) { } else if (strcmp(argv[i], "--cvs-exclude") == 0) {
config->cvs_exclude = true; config->cvs_exclude = true;
} else if (strcmp(argv[i], "--prune-empty-dirs") == 0) { } else if (strcmp(argv[i], "--prune-empty-dirs") == 0) {
@@ -363,45 +352,27 @@ static int parse_args(Config* config, int argc, char* argv[], int* positional_ar
config->relative = true; config->relative = true;
} else if (strcmp(argv[i], "-e") == 0 || strcmp(argv[i], "--rsh") == 0) { } else if (strcmp(argv[i], "-e") == 0 || strcmp(argv[i], "--rsh") == 0) {
if (i + 1 < argc) { if (i + 1 < argc) {
char* dup = str_dup(argv[++i]); if (set_string_option(&config->rsh_command, argv[++i], "-e/--rsh") != 0)
if (!dup)
return -1; return -1;
free(config->rsh_command);
config->rsh_command = dup;
} else { } else {
fprintf(stderr, "Error: -e/--rsh requires a command argument\n"); fprintf(stderr, "Error: -e/--rsh requires a command argument\n");
return -1; return -1;
} }
} else if (strcmp(argv[i], "--rsync-path") == 0 && i + 1 < argc) { } else if (strcmp(argv[i], "--rsync-path") == 0 && i + 1 < argc) {
char* dup = str_dup(argv[++i]); if (set_string_option(&config->rsync_path, argv[++i], "--rsync-path") != 0)
if (!dup)
return -1; return -1;
free(config->rsync_path);
config->rsync_path = dup;
} else if (strcmp(argv[i], "--temp-dir") == 0 && i + 1 < argc) { } else if (strcmp(argv[i], "--temp-dir") == 0 && i + 1 < argc) {
char* dup = str_dup(argv[++i]); if (set_string_option(&config->temp_dir, argv[++i], "--temp-dir") != 0)
if (!dup)
return -1; return -1;
free(config->temp_dir);
config->temp_dir = dup;
} else if (strcmp(argv[i], "--compare-dest") == 0 && i + 1 < argc) { } else if (strcmp(argv[i], "--compare-dest") == 0 && i + 1 < argc) {
char* dup = str_dup(argv[++i]); if (set_string_option(&config->compare_dest, argv[++i], "--compare-dest") != 0)
if (!dup)
return -1; return -1;
free(config->compare_dest);
config->compare_dest = dup;
} else if (strcmp(argv[i], "--copy-dest") == 0 && i + 1 < argc) { } else if (strcmp(argv[i], "--copy-dest") == 0 && i + 1 < argc) {
char* dup = str_dup(argv[++i]); if (set_string_option(&config->copy_dest, argv[++i], "--copy-dest") != 0)
if (!dup)
return -1; return -1;
free(config->copy_dest);
config->copy_dest = dup;
} else if (strcmp(argv[i], "--link-dest") == 0 && i + 1 < argc) { } else if (strcmp(argv[i], "--link-dest") == 0 && i + 1 < argc) {
char* dup = str_dup(argv[++i]); if (set_string_option(&config->link_dest, argv[++i], "--link-dest") != 0)
if (!dup)
return -1; return -1;
free(config->link_dest);
config->link_dest = dup;
} else if (argv[i][0] == '-') { } else if (argv[i][0] == '-') {
fprintf(stderr, "Unknown option: %s\n", argv[i]); fprintf(stderr, "Unknown option: %s\n", argv[i]);
print_usage(); print_usage();
+1 -2
View File
@@ -35,8 +35,7 @@ File* file_create(const char* path) {
return NULL; return NULL;
} }
memcpy(file->path, path, path_len); strcpy(file->path, path);
file->path[path_len] = '\0';
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);
+8 -12
View File
@@ -13,7 +13,7 @@ bool mkdir_r(const char* path) {
char* path_duplicate = malloc(strlen(path) + 1); char* path_duplicate = malloc(strlen(path) + 1);
if (!path_duplicate) if (!path_duplicate)
return false; return false;
memcpy(path_duplicate, path, strlen(path) + 1); strcpy(path_duplicate, path);
char* path_current = (char*)malloc((strlen(path) + 2) * sizeof(char)); char* path_current = (char*)malloc((strlen(path) + 2) * sizeof(char));
if (!path_current) { if (!path_current) {
free(path_duplicate); free(path_duplicate);
@@ -21,8 +21,7 @@ bool mkdir_r(const char* path) {
} }
char* path_current_position = path_current; char* path_current_position = path_current;
if (path[0] == '/') { if (path[0] == '/') {
path_current[0] = '/'; strcpy(path_current, "/");
path_current[1] = '\0';
path_current_position += 1; path_current_position += 1;
} else { } else {
path_current[0] = '\0'; path_current[0] = '\0';
@@ -32,12 +31,10 @@ bool mkdir_r(const char* path) {
const char* part = strtok_r(path_duplicate, delimiter, &saveptr); const char* part = strtok_r(path_duplicate, delimiter, &saveptr);
bool ok = true; bool ok = true;
while (part != NULL) { while (part != NULL) {
size_t part_len = strlen(part); strcpy(path_current_position, part);
memcpy(path_current_position, part, part_len); path_current_position += strlen(part) * sizeof(char);
path_current_position += part_len; strcpy(path_current_position, "/");
path_current_position[0] = '/'; path_current_position += sizeof(char);
path_current_position[1] = '\0';
path_current_position++;
struct stat st; struct stat st;
if (stat(path_current, &st) != 0) { if (stat(path_current, &st) != 0) {
if (mkdir(path_current, 0755) != 0) { if (mkdir(path_current, 0755) != 0) {
@@ -56,9 +53,8 @@ bool mkdir_r(const char* path) {
char* str_dup(const char* string) { char* str_dup(const char* string) {
if (string == NULL) if (string == NULL)
return NULL; return NULL;
size_t str_len = strlen(string); char* new_string = (char*)malloc(strlen(string) + 1);
char* new_string = (char*)malloc(str_len + 1); strcpy(new_string, string);
memcpy(new_string, string, str_len + 1);
return new_string; return new_string;
} }