From: Zbigniew Jędrzejewski-Szmek Date: Mon, 25 Sep 2017 09:09:57 +0000 (+0200) Subject: basic/cap-list: report empty capability set as "" X-Git-Tag: v235~59^2~1 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=6088cefb217184a0a80d80a42b2ab98bc4e07ab7;p=thirdparty%2Fsystemd.git basic/cap-list: report empty capability set as "" $ systemctl show systemd-journald -p CapabilityBoundingSet,AmbientCapabilities CapabilityBoundingSet=cap_chown cap_dac_override cap_dac_read_search cap_fowner cap_setgid ... AmbientCapabilities=(null) ↓ $ systemctl show systemd-journald -p CapabilityBoundingSet,AmbientCapabilities CapabilityBoundingSet=cap_chown cap_dac_override cap_dac_read_search cap_fowner cap_setgid ... AmbientCapabilities= Partially fixes #6511. Add some basic tests for the printing function. --- diff --git a/src/basic/cap-list.c b/src/basic/cap-list.c index 124641f940b..2e9b2d9a550 100644 --- a/src/basic/cap-list.c +++ b/src/basic/cap-list.c @@ -86,15 +86,17 @@ int capability_set_to_string_alloc(uint64_t set, char **s) { add = strlen(p); - if (!GREEDY_REALLOC0(str, allocated, n + add + 2)) + if (!GREEDY_REALLOC(str, allocated, n + add + 2)) return -ENOMEM; strcpy(mempcpy(str + n, p, add), " "); n += add + 1; } - if (n != 0) - str[n - 1] = '\0'; + if (!GREEDY_REALLOC(str, allocated, n + 1)) + return -ENOMEM; + + str[n > 0 ? n - 1 : 0] = '\0'; /* truncate the last space, if it's there */ *s = str; str = NULL; diff --git a/src/test/test-cap-list.c b/src/test/test-cap-list.c index 4132ec56fd0..c1af277f34e 100644 --- a/src/test/test-cap-list.c +++ b/src/test/test-cap-list.c @@ -24,6 +24,7 @@ #include "capability-util.h" #include "fileio.h" #include "parse-util.h" +#include "string-util.h" #include "util.h" /* verify the capability parser */ @@ -102,10 +103,24 @@ static void test_last_cap_probe(void) { assert_se(p == cap_last_cap()); } +static void test_capability_set_to_string_alloc(void) { + _cleanup_free_ char *t1 = NULL, *t2 = NULL, *t3 = NULL; + + assert_se(capability_set_to_string_alloc(0u, &t1) == 0); + assert_se(streq(t1, "")); + + assert_se(capability_set_to_string_alloc(1u<