From 512110854280479a236e756ee3f1331ee6e06d92 Mon Sep 17 00:00:00 2001 From: Allan McRae Date: Sun, 25 Feb 2024 12:48:30 +1000 Subject: [PATCH] Improve robustness of parsing the --debug argument Signed-off-by: Allan McRae --- src/pacman/pacman.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/pacman/pacman.c b/src/pacman/pacman.c index 0fc289a3..d74bfce8 100644 --- a/src/pacman/pacman.c +++ b/src/pacman/pacman.c @@ -409,7 +409,18 @@ static int parsearg_global(int opt) * here, error and warning are set in config_new, though perhaps a * --quiet option will remove these later */ if(optarg) { - unsigned short debug = (unsigned short)atoi(optarg); + char *endptr; + long debug; + + errno = 0; + debug = strtol(optarg, &endptr, 10); + + if(errno == ERANGE || endptr == optarg || *endptr != '\0') { + pm_printf(ALPM_LOG_ERROR, _("'%s' is not a valid debug level\n"), + optarg); + return 1; + } + switch(debug) { case 2: config->logmask |= ALPM_LOG_FUNCTION;