From 9f544dc37212f4e89d53ef4abc55db5ef7c8df3d Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Tue, 9 Feb 2021 15:54:54 +1100 Subject: [PATCH] Fail on unparsed characters after numeric arguments. --- main.c | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/main.c b/main.c index 859fdf2..7e6c131 100644 --- a/main.c +++ b/main.c @@ -316,6 +316,7 @@ int main(int argc, char *argv[]) int hours,minutes; extern int optind; char *eptr, *av; /* for environment */ + char *endptr = NULL; control = &base_control; @@ -435,17 +436,23 @@ int main(int argc, char *argv[]) license(); exit(0); } - control->compression_level = atoi(optarg); + control->compression_level = strtol(optarg, &endptr, 10); if (control->compression_level < 1 || control->compression_level > 9) failure("Invalid compression level (must be 1-9)\n"); + if (*endptr) + failure("Extra characters after compression level: \'%s\'\n", endptr); break; case 'm': - control->ramsize = atol(optarg) * 1024 * 1024 * 100; + control->ramsize = strtol(optarg, &endptr, 10) * 1024 * 1024 * 100; + if (*endptr) + failure("Extra characters after ramsize: \'%s\'\n", endptr); break; case 'N': - control->nice_val = atoi(optarg); + control->nice_val = strtol(optarg, &endptr, 10); if (control->nice_val < PRIO_MIN || control->nice_val > PRIO_MAX) failure("Invalid nice value (must be %d...%d)\n", PRIO_MIN, PRIO_MAX); + if (*endptr) + failure("Extra characters after nice level: \'%s\'\n", endptr); break; case 'o': if (control->outdir) @@ -468,9 +475,11 @@ int main(int argc, char *argv[]) strcat(control->outdir, "/"); break; case 'p': - control->threads = atoi(optarg); + control->threads = strtol(optarg, &endptr, 10); if (control->threads < 1) failure("Must have at least one thread\n"); + if (*endptr) + failure("Extra characters after number of threads: \'%s\'\n", endptr); break; case 'P': control->flags |= FLAG_SHOW_PROGRESS; @@ -519,9 +528,11 @@ int main(int argc, char *argv[]) exit(0); break; case 'w': - control->window = atol(optarg); + control->window = strtol(optarg, &endptr, 10); if (control->window < 1) failure("Window must be positive\n"); + if (*endptr) + failure("Extra characters after window size: \'%s\'\n", endptr); break; case '1': case '2':