]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
cap-list: modernize capability_set_from_string() a bit
authorLennart Poettering <lennart@poettering.net>
Mon, 20 Feb 2023 11:25:44 +0000 (12:25 +0100)
committerLennart Poettering <lennart@poettering.net>
Mon, 20 Feb 2023 15:13:49 +0000 (16:13 +0100)
Make return parameter optional. And return whether there were any caps
we didn't recognize via 0/1 return value.

src/basic/cap-list.c
src/basic/cap-list.h
src/test/test-cap-list.c

index b764d23cca1cc088e40dec785acb7a131163fdc0..0af37b94ee98e8ddba7d1949d46d1d0ab274eaf6 100644 (file)
@@ -92,29 +92,30 @@ int capability_set_to_string(uint64_t set, char **ret) {
         return 0;
 }
 
-int capability_set_from_string(const char *s, uint64_t *set) {
+int capability_set_from_string(const char *s, uint64_t *ret) {
         uint64_t val = 0;
-
-        assert(set);
+        bool good = true;
 
         for (const char *p = s;;) {
                 _cleanup_free_ char *word = NULL;
                 int r;
 
-                r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
-                if (r == -ENOMEM)
+                r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE|EXTRACT_RELAX);
+                if (r < 0)
                         return r;
-                if (r <= 0)
+                if (r == 0)
                         break;
 
                 r = capability_from_name(word);
-                if (r < 0)
-                        continue;
-
-                val |= ((uint64_t) UINT64_C(1)) << (uint64_t) r;
+                if (r < 0) {
+                        log_debug_errno(r, "Failed to parse capability '%s', ignoring: %m", word);
+                        good = false;
+                } else
+                        val |= UINT64_C(1) << r;
         }
 
-        *set = val;
+        if (ret)
+                *ret = val;
 
-        return 0;
+        return good;
 }
index 42d29c4718e4ffa3987a1ae07af6d32223d321ed..888e9223e7b41b66885091e02e19b0cb6d8c8945 100644 (file)
@@ -8,4 +8,4 @@ int capability_from_name(const char *name);
 int capability_list_length(void);
 
 int capability_set_to_string(uint64_t set, char **ret);
-int capability_set_from_string(const char *s, uint64_t *set);
+int capability_set_from_string(const char *s, uint64_t *ret);
index 31c61b8d8d13e1009c1e11932b9fd6cee73b9852..cc5f3a0c35d41db2c9290b8d0b186388780afe8b 100644 (file)
@@ -60,7 +60,7 @@ static void test_capability_set_one(uint64_t c, const char *t) {
         assert_se(capability_set_to_string(c, &t1) == 0);
         assert_se(streq(t1, t));
 
-        assert_se(capability_set_from_string(t1, &c1) == 0);
+        assert_se(capability_set_from_string(t1, &c1) > 0);
         assert_se(c1 == c_masked);
 
         free(t1);
@@ -73,19 +73,19 @@ static void test_capability_set_one(uint64_t c, const char *t) {
 TEST(capability_set_from_string) {
         uint64_t c;
 
-        assert_se(capability_set_from_string(NULL, &c) == 0);
+        assert_se(capability_set_from_string(NULL, &c) > 0);
         assert_se(c == 0);
 
-        assert_se(capability_set_from_string("", &c) == 0);
+        assert_se(capability_set_from_string("", &c) > 0);
         assert_se(c == 0);
 
-        assert_se(capability_set_from_string("0", &c) == 0);
+        assert_se(capability_set_from_string("0", &c) > 0);
         assert_se(c == UINT64_C(1));
 
-        assert_se(capability_set_from_string("1", &c) == 0);
+        assert_se(capability_set_from_string("1", &c) > 0);
         assert_se(c == UINT64_C(1) << 1);
 
-        assert_se(capability_set_from_string("0 1 2 3", &c) == 0);
+        assert_se(capability_set_from_string("0 1 2 3", &c) > 0);
         assert_se(c == (UINT64_C(1) << 4) - 1);
 }