diff options
author | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2024-03-19 23:35:32 +0100 |
---|---|---|
committer | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2024-03-20 15:18:21 +0100 |
commit | 196166dbb1e65a4d942170cb3856cb7e6d28c6d0 (patch) | |
tree | ae28d5f52887e27fb23cacb4095993d2e684b157 /src/basic/unit-name.c | |
parent | logind: use strdup_to() (diff) | |
download | systemd-196166dbb1e65a4d942170cb3856cb7e6d28c6d0.tar.xz systemd-196166dbb1e65a4d942170cb3856cb7e6d28c6d0.zip |
basic/unit-name: use strdup_to() in slice_build_parent_slice()
The handling of the buffer is not obvious, so add a comment.
Diffstat (limited to 'src/basic/unit-name.c')
-rw-r--r-- | src/basic/unit-name.c | 20 |
1 files changed, 7 insertions, 13 deletions
diff --git a/src/basic/unit-name.c b/src/basic/unit-name.c index a84b14568d..4e2f77c03d 100644 --- a/src/basic/unit-name.c +++ b/src/basic/unit-name.c @@ -797,10 +797,6 @@ good: } int slice_build_parent_slice(const char *slice, char **ret) { - _cleanup_free_ char *s = NULL; - char *dash; - int r; - assert(slice); assert(ret); @@ -812,18 +808,16 @@ int slice_build_parent_slice(const char *slice, char **ret) { return 0; } - s = strdup(slice); + _cleanup_free_ char *s = strdup(slice); if (!s) return -ENOMEM; - dash = strrchr(s, '-'); - if (dash) - strcpy(dash, ".slice"); - else { - r = free_and_strdup(&s, SPECIAL_ROOT_SLICE); - if (r < 0) - return r; - } + char *dash = strrchr(s, '-'); + if (!dash) + return strdup_to_full(ret, SPECIAL_ROOT_SLICE); + + /* We know that s ended with .slice before truncation, so we have enough space. */ + strcpy(dash, ".slice"); *ret = TAKE_PTR(s); return 1; |