Skip to content

Commit 5290b78

Browse files
src/usermod.c: $user_newhome: Remove all trailing '/'s
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>
1 parent 3ea6df8 commit 5290b78

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

src/usermod.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#endif /* ACCT_TOOLS_SETUID */
2929
#include <paths.h>
3030
#include <stdio.h>
31+
#include <string.h>
3132
#include <strings.h>
3233
#include <sys/stat.h>
3334
#include <sys/types.h>
@@ -69,6 +70,7 @@
6970
#include "string/strcmp/streq.h"
7071
#include "string/strcmp/strprefix.h"
7172
#include "string/strdup/xstrdup.h"
73+
#include "string/strspn/stprspn.h"
7274
#include "time/day_to_str.h"
7375
#include "typetraits.h"
7476

@@ -540,10 +542,8 @@ static void new_pwent (struct passwd *pwent)
540542
"change user '%s' home from '%s' to '%s'",
541543
pwent->pw_name, pwent->pw_dir, user_newhome));
542544

543-
if (strlen(user_newhome) > 1
544-
&& '/' == user_newhome[strlen(user_newhome)-1]) {
545-
user_newhome[strlen(user_newhome)-1]='\0';
546-
}
545+
if (!streq(user_newhome, ""))
546+
stpcpy(stprspn(user_newhome + 1, "/"), "");
547547

548548
pwent->pw_dir = user_newhome;
549549
}

0 commit comments

Comments
 (0)