From: Alejandro Colomar Date: Sat, 16 Nov 2024 21:20:16 +0000 (+0100) Subject: lib/tcbfuncs.c: rmdir_leading(): Create string just once X-Git-Tag: 4.19.0-rc1~1 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=6a2e15c73b8785598c4b7074693dabed73620102;p=thirdparty%2Fshadow.git lib/tcbfuncs.c: rmdir_leading(): Create string just once Signed-off-by: Alejandro Colomar --- diff --git a/lib/tcbfuncs.c b/lib/tcbfuncs.c index 499a1a9f..269ae0d0 100644 --- a/lib/tcbfuncs.c +++ b/lib/tcbfuncs.c @@ -26,6 +26,7 @@ #include "shadowlog_internal.h" #include "string/sprintf/aprintf.h" #include "string/strcmp/streq.h" +#include "string/strcmp/strprefix.h" #include "string/strerrno.h" @@ -257,38 +258,32 @@ static shadowtcb_status unlink_suffs (const char *user) static shadowtcb_status rmdir_leading(const char *relpath) { - char *ind, *dir, *path; + char *ind, *path, *p; shadowtcb_status ret = SHADOWTCB_SUCCESS; - path = strdup(relpath); + path = aprintf(TCB_DIR "/%s", relpath); if (path == NULL) goto oom; + p = strprefix(path, TCB_DIR "/"); - while ((ind = strrchr (path, '/'))) { + while ((ind = strrchr(p, '/'))) { stpcpy(ind, ""); - dir = aprintf(TCB_DIR "/%s", path); - if (dir == NULL) - goto free_path; - if (rmdir (dir) != 0) { + if (rmdir(path) != 0) { if (errno != ENOTEMPTY) { fprintf (shadow_logfd, _("%s: Cannot remove directory %s: %s\n"), - shadow_progname, dir, strerrno()); + shadow_progname, path, strerrno()); ret = SHADOWTCB_FAILURE; } - free (dir); break; } - free (dir); } free(path); return ret; -free_path: - free(path); oom: OUT_OF_MEMORY; return SHADOWTCB_FAILURE;