]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib/strutils: make sure ul_strtoXX functions always set errno
authorKarel Zak <kzak@redhat.com>
Thu, 21 Apr 2022 09:40:43 +0000 (11:40 +0200)
committerKarel Zak <kzak@redhat.com>
Thu, 21 Apr 2022 09:57:56 +0000 (11:57 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
lib/strutils.c

index a63f71036e61443c92d1138cb32180819d2da4c3..2dcc03d149d6b1c0f1f825d350542f25b8eb2294 100644 (file)
@@ -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;
 }