mirror of
https://github.com/Dasharo/systemd.git
synced 2026-03-06 15:02:31 -08:00
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:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user