From b7565b3f2d3b13c4ae5734407a2c3f27658c7b4b Mon Sep 17 00:00:00 2001 From: Brad Bishop Date: Thu, 12 Jan 2017 08:52:42 -0500 Subject: [PATCH 2/2] basic: Use path escaping when mangling path instances Allow path instances with dashes in them to be unescaped properly. Fixes systemd/systemd#5072 Signed-off-by: Brad Bishop --- src/basic/unit-name.c | 18 ++++++++++++++++++ src/test/test-unit-name.c | 3 +++ 2 files changed, 21 insertions(+) diff --git a/src/basic/unit-name.c b/src/basic/unit-name.c index 93c4838..c91b0e7 100644 --- a/src/basic/unit-name.c +++ b/src/basic/unit-name.c @@ -684,6 +684,8 @@ static char *do_escape_mangle(const char *f, UnitNameMangle allow_globs, char *t * If @allow_globs, globs characters are preserved. Otherwise, they are escaped. */ int unit_name_mangle_with_suffix(const char *name, UnitNameMangle allow_globs, const char *suffix, char **ret) { + _cleanup_free_ char *instance = NULL; + _cleanup_free_ char *prefix = NULL; char *s, *t; int r; @@ -723,6 +725,22 @@ int unit_name_mangle_with_suffix(const char *name, UnitNameMangle allow_globs, c return r; } + r = string_to_instance(name, &instance); + if(r < 0 && r != -EINVAL) + return r; + + if(instance && path_is_absolute(instance)) { + r = string_to_prefix(name, &prefix); + if(r < 0 && r != -EINVAL) + return r; + + r = unit_name_from_path_instance(prefix, instance, suffix, ret); + if (r >= 0) + return 1; + if (r != -EINVAL) + return r; + } + s = new(char, strlen(name) * 4 + strlen(suffix) + 1); if (!s) return -ENOMEM; diff --git a/src/test/test-unit-name.c b/src/test/test-unit-name.c index 2fd83f3..f2cb047 100644 --- a/src/test/test-unit-name.c +++ b/src/test/test-unit-name.c @@ -192,6 +192,9 @@ static void test_unit_name_mangle(void) { test_unit_name_mangle_one(UNIT_NAME_GLOB, "foo", "foo.service", 1); test_unit_name_mangle_one(UNIT_NAME_GLOB, "foo*", "foo*", 0); test_unit_name_mangle_one(UNIT_NAME_GLOB, "ΓΌ*", "\\xc3\\xbc*", 1); + test_unit_name_mangle_one(UNIT_NAME_GLOB, "foo@/bar.service", "foo@bar.service", 1); + test_unit_name_mangle_one(UNIT_NAME_GLOB, "foo@/bar/baz-boo.service", "foo@bar-baz\\x2dboo.service", 1); + test_unit_name_mangle_one(UNIT_NAME_GLOB, "foo@bar/baz-boo.service", "foo@bar-baz-boo.service", 1); } static int test_unit_printf(void) { -- 1.8.3.1