]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
core: catch some special cases in cg_slice_to_path()
authorLennart Poettering <lennart@poettering.net>
Thu, 30 Apr 2015 10:33:35 +0000 (12:33 +0200)
committerLennart Poettering <lennart@poettering.net>
Thu, 30 Apr 2015 10:33:35 +0000 (12:33 +0200)
src/shared/cgroup-util.c
src/test/test-cgroup-util.c

index dbf7942024e0fbea2c45a66ccaa0e456024758da..1306cc197a21b5bee4989462506997ee4f831ccc 100644 (file)
@@ -1642,6 +1642,16 @@ int cg_slice_to_path(const char *unit, char **ret) {
         assert(unit);
         assert(ret);
 
+        if (streq(unit, "-.slice")) {
+                char *x;
+
+                x = strdup("");
+                if (!x)
+                        return -ENOMEM;
+                *ret = x;
+                return 0;
+        }
+
         if (!unit_name_is_valid(unit, TEMPLATE_INVALID))
                 return -EINVAL;
 
@@ -1657,8 +1667,10 @@ int cg_slice_to_path(const char *unit, char **ret) {
                 _cleanup_free_ char *escaped = NULL;
                 char n[dash - p + sizeof(".slice")];
 
-                strcpy(stpncpy(n, p, dash - p), ".slice");
+                if (isempty(dash + 1))
+                        return -EINVAL;
 
+                strcpy(stpncpy(n, p, dash - p), ".slice");
                 if (!unit_name_is_valid(n, TEMPLATE_INVALID))
                         return -EINVAL;
 
index 79c11e297e4a7dc7ba0e77bc8a3030fd2c4521ee..efe99cb34b55fc03fb5e5a4159c0171c9fb4f8f4 100644 (file)
@@ -268,9 +268,14 @@ static void test_slice_to_path(void) {
         test_slice_to_path_one("foobar.slice", "foobar.slice", 0);
         test_slice_to_path_one("foobar-waldo.slice", "foobar.slice/foobar-waldo.slice", 0);
         test_slice_to_path_one("foobar-waldo.service", NULL, -EINVAL);
-        test_slice_to_path_one("-.slice", NULL, -EINVAL);
+        test_slice_to_path_one("-.slice", "", 0);
+        test_slice_to_path_one("--.slice", NULL, -EINVAL);
+        test_slice_to_path_one("-", NULL, -EINVAL);
         test_slice_to_path_one("-foo-.slice", NULL, -EINVAL);
         test_slice_to_path_one("-foo.slice", NULL, -EINVAL);
+        test_slice_to_path_one("foo-.slice", NULL, -EINVAL);
+        test_slice_to_path_one("foo--bar.slice", "foo.slice/foo-.slice/foo--bar.slice", 0);
+        test_slice_to_path_one("foo.slice/foo--bar.slice", NULL, -EINVAL);
         test_slice_to_path_one("a-b.slice", "a.slice/a-b.slice", 0);
         test_slice_to_path_one("a-b-c-d-e.slice", "a.slice/a-b.slice/a-b-c.slice/a-b-c-d.slice/a-b-c-d-e.slice", 0);
 }