]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
util: unify how we parse mode_t strings
authorLennart Poettering <lennart@poettering.net>
Fri, 10 Apr 2015 12:43:06 +0000 (14:43 +0200)
committerLennart Poettering <lennart@poettering.net>
Fri, 10 Apr 2015 14:23:46 +0000 (16:23 +0200)
src/shared/conf-parser.c
src/shared/util.c
src/shared/util.h
src/test/test-util.c
src/tmpfiles/tmpfiles.c

index 2148a30c66c1791e8dd233372a38d82b6d38736f..aa6a4a6395e0a1de5b00282c3e50fa5fb68affe9 100644 (file)
@@ -759,41 +759,30 @@ int config_parse_strv(const char *unit,
         return 0;
 }
 
-int config_parse_mode(const char *unit,
-                      const char *filename,
-                      unsigned line,
-                      const char *section,
+int config_parse_mode(
+                const char *unit,
+                const char *filename,
+                unsigned line,
+                const char *section,
                       unsigned section_line,
-                      const char *lvalue,
-                      int ltype,
-                      const char *rvalue,
-                      void *data,
-                      void *userdata) {
+                const char *lvalue,
+                int ltype,
+                const char *rvalue,
+                void *data,
+                void *userdata) {
 
         mode_t *m = data;
-        long l;
-        char *x = NULL;
 
         assert(filename);
         assert(lvalue);
         assert(rvalue);
         assert(data);
 
-        errno = 0;
-        l = strtol(rvalue, &x, 8);
-        if (!x || x == rvalue || *x || errno) {
-                log_syntax(unit, LOG_ERR, filename, line, errno,
-                           "Failed to parse mode value, ignoring: %s", rvalue);
-                return 0;
-        }
-
-        if (l < 0000 || l > 07777) {
-                log_syntax(unit, LOG_ERR, filename, line, ERANGE,
-                           "Mode value out of range, ignoring: %s", rvalue);
+        if (parse_mode(rvalue, m) < 0) {
+                log_syntax(unit, LOG_ERR, filename, line, errno, "Failed to parse mode value, ignoring: %s", rvalue);
                 return 0;
         }
 
-        *m = (mode_t) l;
         return 0;
 }
 
index 8372038d4d670bd915d846f869f2a01ed993bf73..df5f8d10882737423746db7be2befdd70d039ea4 100644 (file)
@@ -8159,3 +8159,24 @@ char *shell_maybe_quote(const char *s) {
 
         return r;
 }
+
+int parse_mode(const char *s, mode_t *ret) {
+        char *x;
+        long l;
+
+        assert(s);
+        assert(ret);
+
+        errno = 0;
+        l = strtol(s, &x, 8);
+        if (errno != 0)
+                return -errno;
+
+        if (!x || x == s || *x)
+                return -EINVAL;
+        if (l < 0 || l  > 07777)
+                return -ERANGE;
+
+        *ret = (mode_t) l;
+        return 0;
+}
index 527867ce8be536556c55bb13d83b79b88cb6ed7b..56e11c7961b2f556e7484db6b1fb2bc44f0085bd 100644 (file)
@@ -1089,3 +1089,5 @@ void cmsg_close_all(struct msghdr *mh);
 int rename_noreplace(int olddirfd, const char *oldpath, int newdirfd, const char *newpath);
 
 char *shell_maybe_quote(const char *s);
+
+int parse_mode(const char *s, mode_t *ret);
index 77e7400bfc37e5c95080ae13b5e16385e077b243..4d36eb26e59dedcb0018dc291965c44e9f57fab0 100644 (file)
@@ -1576,6 +1576,20 @@ static void test_shell_maybe_quote(void) {
         test_shell_maybe_quote_one("foo$bar", "\"foo\\$bar\"");
 }
 
+static void test_parse_mode(void) {
+        mode_t m;
+
+        assert_se(parse_mode("-1", &m) < 0);
+        assert_se(parse_mode("", &m) < 0);
+        assert_se(parse_mode("888", &m) < 0);
+        assert_se(parse_mode("77777", &m) < 0);
+
+        assert_se(parse_mode("544", &m) >= 0 && m == 0544);
+        assert_se(parse_mode("777", &m) >= 0 && m == 0777);
+        assert_se(parse_mode("7777", &m) >= 0 && m == 07777);
+        assert_se(parse_mode("0", &m) >= 0 && m == 0);
+}
+
 int main(int argc, char *argv[]) {
         log_parse_environment();
         log_open();
@@ -1654,6 +1668,7 @@ int main(int argc, char *argv[]) {
         test_uid_ptr();
         test_sparse_write();
         test_shell_maybe_quote();
+        test_parse_mode();
 
         return 0;
 }
index d34de70e1d343e4f5bab1cc41f88a44e60b14440..ce4a10aa865166f13ad8fa278dbb383d536a4115 100644 (file)
@@ -1879,9 +1879,9 @@ static int parse_line(const char *fname, unsigned line, const char *buffer) {
                         mm++;
                 }
 
-                if (sscanf(mm, "%o", &m) != 1) {
+                if (parse_mode(mm, &m) < 0) {
                         log_error("[%s:%u] Invalid mode '%s'.", fname, line, mode);
-                        return -ENOENT;
+                        return -EBADMSG;
                 }
 
                 i.mode = m;