From: Luca Boccassi Date: Fri, 20 May 2022 13:00:39 +0000 (+0100) Subject: sd-bus: add comment and test in sd_bus_path_decode() for empty string X-Git-Tag: v251~5 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=54cd2d6869d20f0df3d8264168e17c31893dc0ca;p=thirdparty%2Fsystemd.git sd-bus: add comment and test in sd_bus_path_decode() for empty string 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. --- diff --git a/src/libsystemd/sd-bus/sd-bus.c b/src/libsystemd/sd-bus/sd-bus.c index f96149473be..c7a58e95cc4 100644 --- a/src/libsystemd/sd-bus/sd-bus.c +++ b/src/libsystemd/sd-bus/sd-bus.c @@ -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; diff --git a/src/libsystemd/sd-bus/test-bus-marshal.c b/src/libsystemd/sd-bus/test-bus-marshal.c index 9feeaf48fd3..e1afbc2f1be 100644 --- a/src/libsystemd/sd-bus/test-bus-marshal.c +++ b/src/libsystemd/sd-bus/test-bus-marshal.c @@ -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) {