From: Karel Zak Date: Tue, 19 Apr 2022 09:38:57 +0000 (+0200) Subject: lib/strutils: improve strtoul_or_err() for negative numbers X-Git-Tag: v2.39-rc1~706 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=3b888573661d43ea069e98a083bd10e33a2ece69;p=thirdparty%2Futil-linux.git lib/strutils: improve strtoul_or_err() for negative numbers Let's use the same code for strtoul_or_err() and strtol_or_err() as we already use for strtoxXX_or_err() functions. It resolves issue with negative numbers. This problem has been discovered by "./eject -x -1 -v" where -x is based on strtoul_or_err(), but accepts negative number (-1). Reported-by: Enze Li Signed-off-by: Karel Zak --- diff --git a/include/strutils.h b/include/strutils.h index 62129f6f30..ca2b0d9a25 100644 --- a/include/strutils.h +++ b/include/strutils.h @@ -42,8 +42,8 @@ extern uint64_t str2unum_or_err(const char *str, int base, const char *errmesg, extern double strtod_or_err(const char *str, const char *errmesg); extern long double strtold_or_err(const char *str, const char *errmesg); -extern long strtol_or_err(const char *str, const char *errmesg); -extern unsigned long strtoul_or_err(const char *str, const char *errmesg); +#define strtol_or_err(_s, _e) (long) str2num_or_err(_s, 10, _e, LONG_MIN, LONG_MAX) +#define strtoul_or_err(_s, _e) (unsigned long) str2unum_or_err(_s, 10, _e, ULONG_MAX) extern void strtotimeval_or_err(const char *str, struct timeval *tv, const char *errmesg); diff --git a/lib/strutils.c b/lib/strutils.c index cfbd35e677..a63f71036e 100644 --- a/lib/strutils.c +++ b/lib/strutils.c @@ -471,48 +471,6 @@ err: errx(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str); } -long strtol_or_err(const char *str, const char *errmesg) -{ - long num; - char *end = NULL; - - errno = 0; - if (str == NULL || *str == '\0') - goto err; - num = strtol(str, &end, 10); - - if (errno || str == end || (end && *end)) - goto err; - - return num; -err: - if (errno == ERANGE) - err(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str); - - errx(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str); -} - -unsigned long strtoul_or_err(const char *str, const char *errmesg) -{ - unsigned long num; - char *end = NULL; - - errno = 0; - if (str == NULL || *str == '\0') - goto err; - num = strtoul(str, &end, 10); - - if (errno || str == end || (end && *end)) - goto err; - - return num; -err: - if (errno == ERANGE) - err(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str); - - errx(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str); -} - uintmax_t strtosize_or_err(const char *str, const char *errmesg) { uintmax_t num;