From: Lennart Poettering Date: Mon, 5 Sep 2022 15:59:52 +0000 (+0200) Subject: parse-util: make safe_atou16_full() just a wrapper around safe_atou_full() X-Git-Tag: v252-rc1~252^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c74101200c4f055dc376fda0b42f2c858c4431fa;p=thirdparty%2Fsystemd.git 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. --- diff --git a/src/basic/parse-util.c b/src/basic/parse-util.c index 787a681870e..247c84e6186 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; }