]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
src/usermod.c: $user_newhome: Remove all trailing '/'s
authorAlejandro Colomar <alx@kernel.org>
Mon, 13 Oct 2025 13:36:32 +0000 (15:36 +0200)
committerSerge Hallyn <serge@hallyn.com>
Fri, 5 Dec 2025 02:12:50 +0000 (20:12 -0600)
FTR: I'm not entirely sure if an empty string can arrive here.  It might
be that the streq() check is dead code, but I'm not sure, so I put it.
It also makes the code more robust.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
src/usermod.c

index a77344e8f99fd8947ecfea7d6df8b722ab739ae0..e85ffaf74f3b192803785f932df9234c4ee83bef 100644 (file)
@@ -28,6 +28,7 @@
 #endif                         /* ACCT_TOOLS_SETUID */
 #include <paths.h>
 #include <stdio.h>
+#include <string.h>
 #include <strings.h>
 #include <sys/stat.h>
 #include <sys/types.h>
@@ -68,6 +69,7 @@
 #include "string/strcmp/strprefix.h"
 #include "string/strdup/strdup.h"
 #include "string/strerrno.h"
+#include "string/strspn/stprspn.h"
 #include "time/day_to_str.h"
 #include "typetraits.h"
 
@@ -547,10 +549,8 @@ static void new_pwent (struct passwd *pwent, bool process_selinux)
                         "change user '%s' home from '%s' to '%s'",
                         pwent->pw_name, pwent->pw_dir, user_newhome));
 
-               if (strlen(user_newhome) > 1
-                       && '/' == user_newhome[strlen(user_newhome)-1]) {
-                       user_newhome[strlen(user_newhome)-1]='\0';
-               }
+               if (!streq(user_newhome, ""))
+                       stpcpy(stprspn(user_newhome + 1, "/"), "");
 
                pwent->pw_dir = user_newhome;
        }