summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2022-09-05 17:59:52 +0200
committerLennart Poettering <lennart@poettering.net>2022-09-05 18:17:18 +0200
commitc74101200c4f055dc376fda0b42f2c858c4431fa (patch)
tree242d078a752e2ae04bfbe118768a61ad91082612
parent6019fa1c8793adc54a720105a4a7124663311e77 (diff)
downloadsystemd-c74101200c4f055dc376fda0b42f2c858c4431fa.tar.gz
parse-util: make safe_atou16_full() just a wrapper around safe_atou_full()
Both are fancy wrappers around strtoul() anyway, not more, hence let's just make them a wrapper around each other, too, to simplify things a lot.
-rw-r--r--src/basic/parse-util.c40
1 files changed, 7 insertions, 33 deletions
diff --git a/src/basic/parse-util.c b/src/basic/parse-util.c
index 787a681870..247c84e618 100644
--- a/src/basic/parse-util.c
+++ b/src/basic/parse-util.c
@@ -503,42 +503,16 @@ int safe_atou8(const char *s, uint8_t *ret) {
}
int safe_atou16_full(const char *s, unsigned base, uint16_t *ret) {
- char *x = NULL;
- unsigned long l;
-
- assert(s);
- assert(SAFE_ATO_MASK_FLAGS(base) <= 16);
-
- if (FLAGS_SET(base, SAFE_ATO_REFUSE_LEADING_WHITESPACE) &&
- strchr(WHITESPACE, s[0]))
- return -EINVAL;
-
- s += strspn(s, WHITESPACE);
-
- if (FLAGS_SET(base, SAFE_ATO_REFUSE_PLUS_MINUS) &&
- IN_SET(s[0], '+', '-'))
- return -EINVAL;
-
- if (FLAGS_SET(base, SAFE_ATO_REFUSE_LEADING_ZERO) &&
- s[0] == '0' && s[1] != 0)
- return -EINVAL;
-
- s = mangle_base(s, &base);
+ unsigned u;
+ int r;
- errno = 0;
- l = strtoul(s, &x, SAFE_ATO_MASK_FLAGS(base));
- if (errno > 0)
- return -errno;
- if (!x || x == s || *x != 0)
- return -EINVAL;
- if (l != 0 && s[0] == '-')
- return -ERANGE;
- if ((unsigned long) (uint16_t) l != l)
+ r = safe_atou_full(s, base, &u);
+ if (r < 0)
+ return r;
+ if (u > UINT16_MAX)
return -ERANGE;
- if (ret)
- *ret = (uint16_t) l;
-
+ *ret = (uint16_t) u;
return 0;
}