daandemeyer / rpms / systemd

Forked from rpms/systemd 2 years ago
Clone
21255d
From 147a3696b45a872e0e21fb74e1497f02543ce871 Mon Sep 17 00:00:00 2001
21255d
From: Lennart Poettering <lennart@poettering.net>
21255d
Date: Mon, 1 Jun 2020 17:16:04 +0200
21255d
Subject: [PATCH] parse-util: rewrite parse_mode() on top of safe_atou_full()
21255d
21255d
Parsing is hard, hence let's use our own careful wrappers wherever
21255d
possible.
21255d
21255d
(cherry picked from commit c44702a8bd8cc8b7f2f1df21db9308d9af7dda5b)
21255d
21255d
Related: #1848373
21255d
---
21255d
 src/basic/parse-util.c | 28 +++++++++++++---------------
21255d
 1 file changed, 13 insertions(+), 15 deletions(-)
21255d
21255d
diff --git a/src/basic/parse-util.c b/src/basic/parse-util.c
21255d
index 7a7cefe6ff..68c156c543 100644
21255d
--- a/src/basic/parse-util.c
21255d
+++ b/src/basic/parse-util.c
21255d
@@ -54,26 +54,24 @@ int parse_pid(const char *s, pid_t* ret_pid) {
21255d
 }
21255d
 
21255d
 int parse_mode(const char *s, mode_t *ret) {
21255d
-        char *x;
21255d
-        long l;
21255d
+        unsigned m;
21255d
+        int r;
21255d
 
21255d
         assert(s);
21255d
-        assert(ret);
21255d
 
21255d
-        s += strspn(s, WHITESPACE);
21255d
-        if (s[0] == '-')
21255d
-                return -ERANGE;
21255d
-
21255d
-        errno = 0;
21255d
-        l = strtol(s, &x, 8);
21255d
-        if (errno > 0)
21255d
-                return -errno;
21255d
-        if (!x || x == s || *x != 0)
21255d
-                return -EINVAL;
21255d
-        if (l < 0 || l  > 07777)
21255d
+        r = safe_atou_full(s, 8 |
21255d
+                           SAFE_ATO_REFUSE_PLUS_MINUS, /* Leading '+' or even '-' char? that's just weird,
21255d
+                                                        * refuse. User might have wanted to add mode flags or
21255d
+                                                        * so, but this parser doesn't allow that, so let's
21255d
+                                                        * better be safe. */
21255d
+                           &m);
21255d
+        if (r < 0)
21255d
+                return r;
21255d
+        if (m > 07777)
21255d
                 return -ERANGE;
21255d
 
21255d
-        *ret = (mode_t) l;
21255d
+        if (ret)
21255d
+                *ret = m;
21255d
         return 0;
21255d
 }
21255d