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.
This commit is contained in:
Lennart Poettering
2022-09-05 17:59:52 +02:00
parent 6019fa1c87
commit c74101200c

View File

@@ -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;
unsigned u;
int r;
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);
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;
}