richardphibel / rpms / systemd

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