teknoraver / rpms / systemd

Forked from rpms/systemd 2 months ago
Clone

Blame SOURCES/0464-parse-util-rewrite-parse_mode-on-top-of-safe_atou_fu.patch

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