diff options
author | Lennart Poettering <lennart@poettering.net> | 2020-06-01 17:16:04 +0200 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2020-06-05 15:56:31 +0200 |
commit | c44702a8bd8cc8b7f2f1df21db9308d9af7dda5b (patch) | |
tree | 52aecae5fb16df5e7dbb41af228f2f3c27f423b2 /src/basic/parse-util.c | |
parent | parse-util: make return parameter optional in safe_atou16_full() (diff) | |
download | systemd-c44702a8bd8cc8b7f2f1df21db9308d9af7dda5b.tar.xz systemd-c44702a8bd8cc8b7f2f1df21db9308d9af7dda5b.zip |
parse-util: rewrite parse_mode() on top of safe_atou_full()
Parsing is hard, hence let's use our own careful wrappers wherever
possible.
Diffstat (limited to 'src/basic/parse-util.c')
-rw-r--r-- | src/basic/parse-util.c | 28 |
1 files changed, 13 insertions, 15 deletions
diff --git a/src/basic/parse-util.c b/src/basic/parse-util.c index c58f2cdda1..0f6d24f590 100644 --- a/src/basic/parse-util.c +++ b/src/basic/parse-util.c @@ -70,26 +70,24 @@ int parse_pid(const char *s, pid_t* ret_pid) { } int parse_mode(const char *s, mode_t *ret) { - char *x; - long l; + unsigned m; + int r; assert(s); - assert(ret); - s += strspn(s, WHITESPACE); - if (s[0] == '-') - return -ERANGE; - - errno = 0; - l = strtol(s, &x, 8); - if (errno > 0) - return -errno; - if (!x || x == s || *x != 0) - return -EINVAL; - if (l < 0 || l > 07777) + r = safe_atou_full(s, 8 | + SAFE_ATO_REFUSE_PLUS_MINUS, /* Leading '+' or even '-' char? that's just weird, + * refuse. User might have wanted to add mode flags or + * so, but this parser doesn't allow that, so let's + * better be safe. */ + &m); + if (r < 0) + return r; + if (m > 07777) return -ERANGE; - *ret = (mode_t) l; + if (ret) + *ret = m; return 0; } |