From: Karel Zak Date: Thu, 21 Apr 2022 09:40:43 +0000 (+0200) Subject: lib/strutils: make sure ul_strtoXX functions always set errno X-Git-Tag: v2.39-rc1~698 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=47f851e15eefc34c03620c367e585a198d12b279;p=thirdparty%2Futil-linux.git lib/strutils: make sure ul_strtoXX functions always set errno Signed-off-by: Karel Zak --- diff --git a/lib/strutils.c b/lib/strutils.c index a63f71036e..2dcc03d149 100644 --- a/lib/strutils.c +++ b/lib/strutils.c @@ -326,13 +326,16 @@ int ul_strtos64(const char *str, int64_t *num, int base) { char *end = NULL; - errno = 0; if (str == NULL || *str == '\0') - return -EINVAL; + return -(errno = EINVAL); + + errno = 0; *num = (int64_t) strtoimax(str, &end, base); - if (errno || str == end || (end && *end)) - return -EINVAL; + if (errno != 0) + return -errno; + if (str == end || (end && *end)) + return -(errno = EINVAL); return 0; } @@ -341,13 +344,13 @@ int ul_strtou64(const char *str, uint64_t *num, int base) char *end = NULL; int64_t tmp; - errno = 0; if (str == NULL || *str == '\0') - return -EINVAL; + return -(errno = EINVAL); /* we need to ignore negative numbers, note that for invalid negative * number strtoimax() returns negative number too, so we do not * need to check errno here */ + errno = 0; tmp = (int64_t) strtoimax(str, &end, base); if (tmp < 0) errno = ERANGE; @@ -356,8 +359,10 @@ int ul_strtou64(const char *str, uint64_t *num, int base) *num = strtoumax(str, &end, base); } - if (errno || str == end || (end && *end)) - return -EINVAL; + if (errno != 0) + return -errno; + if (str == end || (end && *end)) + return -(errno = EINVAL); return 0; }