Improve robustness of parsing the --ask argument
Signed-off-by: Allan McRae <allan@archlinux.org>
This commit is contained in:
parent
5121108542
commit
b30dac6a5b
1 changed files with 31 additions and 12 deletions
|
@ -371,7 +371,7 @@ static int parsearg_op(int opt, int dryrun)
|
||||||
|
|
||||||
/** Helper functions for parsing command-line arguments.
|
/** Helper functions for parsing command-line arguments.
|
||||||
* @param opt Keycode returned by getopt_long
|
* @param opt Keycode returned by getopt_long
|
||||||
* @return 0 on success, 1 on failure
|
* @return 0 on success, 1 on unkown option, 2 on invalid argument
|
||||||
*/
|
*/
|
||||||
static int parsearg_global(int opt)
|
static int parsearg_global(int opt)
|
||||||
{
|
{
|
||||||
|
@ -380,8 +380,25 @@ static int parsearg_global(int opt)
|
||||||
config_add_architecture(strdup(optarg));
|
config_add_architecture(strdup(optarg));
|
||||||
break;
|
break;
|
||||||
case OP_ASK:
|
case OP_ASK:
|
||||||
|
if(optarg) {
|
||||||
|
char *endptr;
|
||||||
|
long ask;
|
||||||
|
|
||||||
|
errno = 0;
|
||||||
|
ask = strtol(optarg, &endptr, 10);
|
||||||
|
|
||||||
|
if(errno == ERANGE || endptr == optarg || *endptr != '\0' || ask > UINT_MAX) {
|
||||||
|
pm_printf(ALPM_LOG_ERROR, "'%s' is not a valid ask level\n",
|
||||||
|
optarg);
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
|
||||||
config->noask = 1;
|
config->noask = 1;
|
||||||
config->ask = (unsigned int)atoi(optarg);
|
config->ask = (unsigned int)ask;
|
||||||
|
} else {
|
||||||
|
pm_printf(ALPM_LOG_ERROR, "no value provided for ask level\n");
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case OP_CACHEDIR:
|
case OP_CACHEDIR:
|
||||||
config->cachedirs = alpm_list_add(config->cachedirs, strdup(optarg));
|
config->cachedirs = alpm_list_add(config->cachedirs, strdup(optarg));
|
||||||
|
@ -396,7 +413,7 @@ static int parsearg_global(int opt)
|
||||||
} else {
|
} else {
|
||||||
pm_printf(ALPM_LOG_ERROR, _("invalid argument '%s' for %s\n"),
|
pm_printf(ALPM_LOG_ERROR, _("invalid argument '%s' for %s\n"),
|
||||||
optarg, "--color");
|
optarg, "--color");
|
||||||
return 1;
|
return 2;
|
||||||
}
|
}
|
||||||
enable_colors(config->color);
|
enable_colors(config->color);
|
||||||
break;
|
break;
|
||||||
|
@ -418,7 +435,7 @@ static int parsearg_global(int opt)
|
||||||
if(errno == ERANGE || endptr == optarg || *endptr != '\0') {
|
if(errno == ERANGE || endptr == optarg || *endptr != '\0') {
|
||||||
pm_printf(ALPM_LOG_ERROR, _("'%s' is not a valid debug level\n"),
|
pm_printf(ALPM_LOG_ERROR, _("'%s' is not a valid debug level\n"),
|
||||||
optarg);
|
optarg);
|
||||||
return 1;
|
return 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch(debug) {
|
switch(debug) {
|
||||||
|
@ -431,7 +448,7 @@ static int parsearg_global(int opt)
|
||||||
default:
|
default:
|
||||||
pm_printf(ALPM_LOG_ERROR, _("'%s' is not a valid debug level\n"),
|
pm_printf(ALPM_LOG_ERROR, _("'%s' is not a valid debug level\n"),
|
||||||
optarg);
|
optarg);
|
||||||
return 1;
|
return 2;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
config->logmask |= ALPM_LOG_DEBUG;
|
config->logmask |= ALPM_LOG_DEBUG;
|
||||||
|
@ -1030,6 +1047,7 @@ static int parseargs(int argc, char *argv[])
|
||||||
/* fall back to global options */
|
/* fall back to global options */
|
||||||
result = parsearg_global(opt);
|
result = parsearg_global(opt);
|
||||||
if(result != 0) {
|
if(result != 0) {
|
||||||
|
if(result == 1) {
|
||||||
/* global option parsing failed, abort */
|
/* global option parsing failed, abort */
|
||||||
if(opt < OP_LONG_FLAG_MIN) {
|
if(opt < OP_LONG_FLAG_MIN) {
|
||||||
pm_printf(ALPM_LOG_ERROR, _("invalid option '-%c'\n"), opt);
|
pm_printf(ALPM_LOG_ERROR, _("invalid option '-%c'\n"), opt);
|
||||||
|
@ -1037,7 +1055,8 @@ static int parseargs(int argc, char *argv[])
|
||||||
pm_printf(ALPM_LOG_ERROR, _("invalid option '--%s'\n"),
|
pm_printf(ALPM_LOG_ERROR, _("invalid option '--%s'\n"),
|
||||||
opts[option_index].name);
|
opts[option_index].name);
|
||||||
}
|
}
|
||||||
return result;
|
}
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue