]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
parse-util: make safe_atou8() just a wrapper around safe_atou8_full() 24568/head
authorLennart Poettering <lennart@poettering.net>
Mon, 5 Sep 2022 16:08:16 +0000 (18:08 +0200)
committerLennart Poettering <lennart@poettering.net>
Mon, 5 Sep 2022 16:17:18 +0000 (18:17 +0200)
As in the previous commit: it's just a wrapper around the same
strtoul(), hence let's just share some more code.

src/basic/parse-util.c
src/basic/parse-util.h

index 247c84e61862d0e0f2893fa6a47405d83580ead1..3b3efb0ab8c561fe0772711f4af540878b0f4bd3 100644 (file)
@@ -476,29 +476,17 @@ int safe_atolli(const char *s, long long int *ret_lli) {
         return 0;
 }
 
-int safe_atou8(const char *s, uint8_t *ret) {
-        unsigned base = 0;
-        unsigned long l;
-        char *x = NULL;
-
-        assert(s);
-
-        s += strspn(s, WHITESPACE);
-        s = mangle_base(s, &base);
+int safe_atou8_full(const char *s, unsigned base, uint8_t *ret) {
+        unsigned u;
+        int r;
 
-        errno = 0;
-        l = strtoul(s, &x, base);
-        if (errno > 0)
-                return -errno;
-        if (!x || x == s || *x != 0)
-                return -EINVAL;
-        if (l != 0 && s[0] == '-')
-                return -ERANGE;
-        if ((unsigned long) (uint8_t) l != l)
+        r = safe_atou_full(s, base, &u);
+        if (r < 0)
+                return r;
+        if (u > UINT8_MAX)
                 return -ERANGE;
 
-        if (ret)
-                *ret = (uint8_t) l;
+        *ret = (uint8_t) u;
         return 0;
 }
 
index f2222dcffb09d86356345e772ae54fbfb90be26d..8d8d52327b751bf3fc5fc5f91c63d5fe8a506e60 100644 (file)
@@ -36,7 +36,11 @@ static inline int safe_atou(const char *s, unsigned *ret_u) {
 int safe_atoi(const char *s, int *ret_i);
 int safe_atolli(const char *s, long long int *ret_i);
 
-int safe_atou8(const char *s, uint8_t *ret);
+int safe_atou8_full(const char *s, unsigned base, uint8_t *ret);
+
+static inline int safe_atou8(const char *s, uint8_t *ret) {
+        return safe_atou8_full(s, 0, ret);
+}
 
 int safe_atou16_full(const char *s, unsigned base, uint16_t *ret);