]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
lib/tcbfuncs.c: rmdir_leading(): Create string just once
authorAlejandro Colomar <alx@kernel.org>
Sat, 16 Nov 2024 21:20:16 +0000 (22:20 +0100)
committerSerge Hallyn <serge@hallyn.com>
Sun, 7 Dec 2025 04:38:06 +0000 (22:38 -0600)
Signed-off-by: Alejandro Colomar <alx@kernel.org>
lib/tcbfuncs.c

index 499a1a9fc49857e385adaaea5caf36010952eada..269ae0d0443dc94dd51f432d176df1211730ca79 100644 (file)
@@ -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;