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
This commit is contained in:
2026-08-01 18:12:41 +02:00
parent e876055167
commit 5eaea9d92e
+81 -110
View File
@@ -49,6 +49,37 @@ static bool parse_nonneg_int(const char* s, int* out_val) {
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 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;
} 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) {
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) {
config->use_incremental = true;
} 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) {
char* dup = str_dup(argv[++i]);
if (!dup) {
fprintf(stderr, "Error: memory allocation failed for --source-dir\n");
if (set_string_option(&config->send_directory, argv[++i], "--source-dir") != 0)
return -1;
}
free(config->send_directory);
config->send_directory = dup;
} else if (strcmp(argv[i], "--dest-dir") == 0 && i + 1 < argc) {
char* dup = str_dup(argv[++i]);
if (!dup) {
fprintf(stderr, "Error: memory allocation failed for --dest-dir\n");
if (set_string_option(&config->receive_root_directory, argv[++i], "--dest-dir") != 0)
return -1;
}
free(config->receive_root_directory);
config->receive_root_directory = dup;
} else if (strcmp(argv[i], "--save-to-disk") == 0) {
config->save_to_disk = true;
} 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;
log_message(LOG_LEVEL_INFO, "Enabled Chunk Serialization");
} else if (strcmp(argv[i], "--server-host") == 0 && i + 1 < argc) {
char* dup = str_dup(argv[++i]);
if (!dup) {
fprintf(stderr, "Error: memory allocation failed for --server-host\n");
if (set_string_option(&config->server_host, argv[++i], "--server-host") != 0)
return -1;
}
free(config->server_host);
config->server_host = dup;
} else if (strcmp(argv[i], "--server-port") == 0 && i + 1 < argc) {
if (!parse_positive_int(argv[++i], &config->server_port)) {
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) {
config->show_progress = true;
} else if (strcmp(argv[i], "--chunk-size") == 0 && i + 1 < argc) {
unsigned long long val = strtoull(argv[++i], NULL, 10);
if (val > 0)
config->chunk_size = val;
char* end;
errno = 0;
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) {
config->use_tls = true;
} else if (strcmp(argv[i], "--cert") == 0 && i + 1 < argc) {
char* dup = str_dup(argv[++i]);
if (!dup) {
fprintf(stderr, "Error: memory allocation failed for --cert\n");
if (set_string_option(&config->tls_cert, argv[++i], "--cert") != 0)
return -1;
}
free(config->tls_cert);
config->tls_cert = dup;
} else if (strcmp(argv[i], "--key") == 0 && i + 1 < argc) {
char* dup = str_dup(argv[++i]);
if (!dup) {
fprintf(stderr, "Error: memory allocation failed for --key\n");
if (set_string_option(&config->tls_key, argv[++i], "--key") != 0)
return -1;
}
free(config->tls_key);
config->tls_key = dup;
} else if (strcmp(argv[i], "--ca") == 0 && i + 1 < argc) {
char* dup = str_dup(argv[++i]);
if (!dup) {
fprintf(stderr, "Error: memory allocation failed for --ca\n");
if (set_string_option(&config->tls_ca, argv[++i], "--ca") != 0)
return -1;
}
free(config->tls_ca);
config->tls_ca = dup;
} else if (strcmp(argv[i], "--timeout") == 0 && i + 1 < argc) {
int val;
if (!parse_positive_int(argv[++i], &val)) {
fprintf(stderr, "Error: --timeout must be a positive integer\n");
if (set_positive_int_option(&config->timeout, argv[++i], "--timeout") != 0)
return -1;
}
config->timeout = val;
} else if (strcmp(argv[i], "--contimeout") == 0 && i + 1 < argc) {
int val;
if (!parse_positive_int(argv[++i], &val)) {
fprintf(stderr, "Error: --contimeout must be a positive integer\n");
if (set_positive_int_option(&config->contimeout, argv[++i], "--contimeout") != 0)
return -1;
}
config->contimeout = val;
} else if (strcmp(argv[i], "-q") == 0 || strcmp(argv[i], "--quiet") == 0 ||
strcmp(argv[i], "--silent") == 0) {
config->quiet = true;
} else if (strcmp(argv[i], "--backup") == 0) {
config->backup = true;
} else if (strcmp(argv[i], "--backup-dir") == 0 && i + 1 < argc) {
char* dup = str_dup(argv[++i]);
if (!dup) {
fprintf(stderr, "Error: memory allocation failed for --backup-dir\n");
if (set_string_option(&config->backup_dir, argv[++i], "--backup-dir") != 0)
return -1;
}
free(config->backup_dir);
config->backup_dir = dup;
} else if (strcmp(argv[i], "--stats") == 0) {
config->stats = true;
} else if (strcmp(argv[i], "--max-depth") == 0 && i + 1 < argc) {
if (!parse_nonneg_int(argv[++i], &config->max_depth)) {
fprintf(stderr, "Error: --max-depth must be a non-negative integer\n");
if (set_nonneg_int_option(&config->max_depth, argv[++i], "--max-depth") != 0)
return -1;
}
} else if (strcmp(argv[i], "--log-file") == 0 && i + 1 < argc) {
FILE* lf = fopen(argv[++i], "a");
if (!lf) {
@@ -263,12 +268,8 @@ static int parse_args(Config* config, int argc, char* argv[], int* positional_ar
config->log_file = lf;
log_set_file(lf);
} else if (strcmp(argv[i], "--queue-size") == 0 && i + 1 < argc) {
int val;
if (!parse_positive_int(argv[++i], &val)) {
fprintf(stderr, "Error: --queue-size must be a positive integer\n");
if (set_positive_int_option(&config->queue_size, argv[++i], "--queue-size") != 0)
return -1;
}
config->queue_size = val;
} else if (strcmp(argv[i], "--exclude-from") == 0 && i + 1 < argc) {
if (read_patterns_from_file(argv[++i], &config->exclude_patterns, &config->exclude_count) !=
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) {
config->partial = true;
} else if (strcmp(argv[i], "--fastsync-server-path") == 0 && i + 1 < argc) {
char* dup = str_dup(argv[++i]);
if (!dup) {
fprintf(stderr, "Error: memory allocation failed for --fastsync-server-path\n");
if (set_string_option(&config->fastsync_server_path, argv[++i], "--fastsync-server-path") !=
0)
return -1;
}
free(config->fastsync_server_path);
config->fastsync_server_path = dup;
} else if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--verbose") == 0) {
set_log_level(LOG_LEVEL_DEBUG);
} 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) {
config->itemize_changes = true;
} else if (strcmp(argv[i], "--out-format") == 0 && i + 1 < argc) {
char* dup = str_dup(argv[++i]);
if (!dup)
if (set_string_option(&config->out_format, argv[++i], "--out-format") != 0)
return -1;
free(config->out_format);
config->out_format = dup;
} 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) {
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) {
config->list_only = true;
} 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) {
config->delete_after = true;
} else if (strcmp(argv[i], "--max-delete") == 0 && i + 1 < argc) {
int val;
if (!parse_nonneg_int(argv[++i], &val)) {
fprintf(stderr, "Error: --max-delete must be a non-negative integer\n");
if (set_nonneg_int_option(&config->max_delete, argv[++i], "--max-delete") != 0)
return -1;
}
config->max_delete = val;
} else if (strcmp(argv[i], "--filter") == 0 && i + 1 < argc) {
if (!config->filters)
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;
array_list_add(config->filters, dup);
} else if (strcmp(argv[i], "--files-from") == 0 && i + 1 < argc) {
char* dup = str_dup(argv[++i]);
if (!dup)
if (set_string_option(&config->files_from, argv[++i], "--files-from") != 0)
return -1;
free(config->files_from);
config->files_from = dup;
} else if (strcmp(argv[i], "--cvs-exclude") == 0) {
config->cvs_exclude = true;
} 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;
} else if (strcmp(argv[i], "-e") == 0 || strcmp(argv[i], "--rsh") == 0) {
if (i + 1 < argc) {
char* dup = str_dup(argv[++i]);
if (!dup)
if (set_string_option(&config->rsh_command, argv[++i], "-e/--rsh") != 0)
return -1;
free(config->rsh_command);
config->rsh_command = dup;
} else {
fprintf(stderr, "Error: -e/--rsh requires a command argument\n");
return -1;
}
} else if (strcmp(argv[i], "--rsync-path") == 0 && i + 1 < argc) {
char* dup = str_dup(argv[++i]);
if (!dup)
if (set_string_option(&config->rsync_path, argv[++i], "--rsync-path") != 0)
return -1;
free(config->rsync_path);
config->rsync_path = dup;
} else if (strcmp(argv[i], "--temp-dir") == 0 && i + 1 < argc) {
char* dup = str_dup(argv[++i]);
if (!dup)
if (set_string_option(&config->temp_dir, argv[++i], "--temp-dir") != 0)
return -1;
free(config->temp_dir);
config->temp_dir = dup;
} else if (strcmp(argv[i], "--compare-dest") == 0 && i + 1 < argc) {
char* dup = str_dup(argv[++i]);
if (!dup)
if (set_string_option(&config->compare_dest, argv[++i], "--compare-dest") != 0)
return -1;
free(config->compare_dest);
config->compare_dest = dup;
} else if (strcmp(argv[i], "--copy-dest") == 0 && i + 1 < argc) {
char* dup = str_dup(argv[++i]);
if (!dup)
if (set_string_option(&config->copy_dest, argv[++i], "--copy-dest") != 0)
return -1;
free(config->copy_dest);
config->copy_dest = dup;
} else if (strcmp(argv[i], "--link-dest") == 0 && i + 1 < argc) {
char* dup = str_dup(argv[++i]);
if (!dup)
if (set_string_option(&config->link_dest, argv[++i], "--link-dest") != 0)
return -1;
free(config->link_dest);
config->link_dest = dup;
} else if (argv[i][0] == '-') {
fprintf(stderr, "Unknown option: %s\n", argv[i]);
print_usage();