From: Vincent Lefevre Date: Tue, 19 Jun 2018 07:37:56 +0000 (+0200) Subject: mutt_str_atol: better error handling X-Git-Tag: neomutt-20180622~5^2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=ee9c657b1ac942499ae356add472f903de85ecaa;p=neomutt mutt_str_atol: better error handling Reset errno to 0 before calling strtol and testing it in mutt_atol. Otherwise providing LONG_MAX+1 then LONG_MAX gave an error for LONG_MAX too. * Detect overflow on negative numbers too (< LONG_MIN). * Distinguish between format error and number overflow as already expected (e.g. for pgp_timeout and smime_timeout). --- diff --git a/mutt/string.c b/mutt/string.c index 8c86e4330..09c1c0322 100644 --- a/mutt/string.c +++ b/mutt/string.c @@ -123,6 +123,7 @@ const char *mutt_str_sysexit(int e) * @param[out] dst Store the result * @retval 0 Success * @retval -1 Error + * @retval -2 Overflow * * This is a strtol() wrapper with range checking. * errno may be set on error, e.g. ERANGE @@ -140,9 +141,12 @@ int mutt_str_atol(const char *str, long *dst) return 0; } + errno = 0; *res = strtol(str, &e, 10); - if (((*res == LONG_MAX) && (errno == ERANGE)) || (e && (*e != '\0'))) + if (e && (*e != '\0')) return -1; + if (errno == ERANGE) + return -2; return 0; }