]> granicus.if.org Git - neomutt/commitdiff
mutt_str_atol: better error handling
authorVincent Lefevre <vincent@vinc17.net>
Tue, 19 Jun 2018 07:37:56 +0000 (09:37 +0200)
committerRichard Russon <rich@flatcap.org>
Wed, 20 Jun 2018 15:19:20 +0000 (16:19 +0100)
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).

mutt/string.c

index 8c86e4330a10879de343933c41ebe51794e0687c..09c1c0322c3d2cdd8d217c9a8a9d8e757229a55c 100644 (file)
@@ -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;
 }