Skip to content

Commit

Permalink
lib/tcbfuncs.c: rmdir_leading(): Create string just once
Browse files Browse the repository at this point in the history
Signed-off-by: Alejandro Colomar <[email protected]>
  • Loading branch information
alejandro-colomar committed Jan 24, 2025
1 parent ecc493a commit ee0f6c2
Showing 1 changed file with 7 additions and 13 deletions.
20 changes: 7 additions & 13 deletions lib/tcbfuncs.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "shadowio.h"
#include "shadowlog_internal.h"
#include "string/strcmp/streq.h"
#include "string/strcmp/strprefix.h"


#define SHADOWTCB_HASH_BY 1000
Expand Down Expand Up @@ -256,38 +257,31 @@ 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);
if (path == NULL)
if (asprintf(&path, TCB_DIR "/%s", relpath) == -1)
goto oom;

p = strprefix(path, TCB_DIR "/");

while ((ind = strrchr (path, '/'))) {
while ((ind = strrchr(p, '/'))) {
stpcpy(ind, "");
if (asprintf (&dir, TCB_DIR "/%s", path) == -1) {
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, strerror (errno));
shadow_progname, path, strerror(errno));
ret = SHADOWTCB_FAILURE;
}
free (dir);
break;
}
free (dir);
}

free(path);
return ret;

free_path:
free(path);
oom:
OUT_OF_MEMORY;
return SHADOWTCB_FAILURE;
Expand Down

0 comments on commit ee0f6c2

Please sign in to comment.