]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sd-bus: add comment and test in sd_bus_path_decode() for empty string
authorLuca Boccassi <bluca@debian.org>
Fri, 20 May 2022 13:00:39 +0000 (14:00 +0100)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 20 May 2022 18:44:09 +0000 (03:44 +0900)
3970         e = object_path_startswith(path, prefix);
(gdb) p path
$1 = 0x55c5a166f768 "/org/freedesktop/portable1/image"
(gdb) p prefix
$2 = 0x55c59ffc2928 "/org/freedesktop/portable1/image"
(gdb) p e
$1 = 0x5581a1675788 ""

This can be a bit confusing in certain cases, so add a comment and a
test to make the behaviour clearer and explicit.

src/libsystemd/sd-bus/sd-bus.c
src/libsystemd/sd-bus/test-bus-marshal.c

index f96149473be2dfd41042bcb0cb27ab6d3630d2cc..c7a58e95cc45113ee636e423e488519640dbd89c 100644 (file)
@@ -3973,6 +3973,10 @@ _public_ int sd_bus_path_decode(const char *path, const char *prefix, char **ext
                 return 0;
         }
 
+        /* Note that 'e' might be an empty string here. That's expected. E.g. a case where the subtree
+         * corresponds to a subtree on a disk, and we want to return something that represents the root
+         * of the filesystem. */
+
         ret = bus_label_unescape(e);
         if (!ret)
                 return -ENOMEM;
index 9feeaf48fd3cbf641ae75d0a745621cac30628d2..e1afbc2f1be5b1bf7d7eccf97fd6e3945c4e5ede 100644 (file)
@@ -36,7 +36,7 @@ static void test_bus_path_encode_unique(void) {
 }
 
 static void test_bus_path_encode(void) {
-        _cleanup_free_ char *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL, *f = NULL;
+        _cleanup_free_ char *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL, *f = NULL, *g = NULL;
 
         assert_se(sd_bus_path_encode("/foo/bar", "waldo", &a) >= 0 && streq(a, "/foo/bar/waldo"));
         assert_se(sd_bus_path_decode(a, "/waldo", &b) == 0 && b == NULL);
@@ -50,6 +50,8 @@ static void test_bus_path_encode(void) {
 
         assert_se(sd_bus_path_encode("/foo/bar", "foo.bar", &e) >= 0 && streq(e, "/foo/bar/foo_2ebar"));
         assert_se(sd_bus_path_decode(e, "/foo/bar", &f) > 0 && streq(f, "foo.bar"));
+
+        assert_se(sd_bus_path_decode("/waldo", "/waldo", &g) > 0 && streq(g, ""));
 }
 
 static void test_bus_path_encode_many(void) {