Skip to content

Commit 6a2e15c

Browse files
alejandro-colomarhallyn
authored andcommitted
lib/tcbfuncs.c: rmdir_leading(): Create string just once
Signed-off-by: Alejandro Colomar <alx@kernel.org>
1 parent 4cf430b commit 6a2e15c

1 file changed

Lines changed: 7 additions & 12 deletions

File tree

lib/tcbfuncs.c

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "shadowlog_internal.h"
2727
#include "string/sprintf/aprintf.h"
2828
#include "string/strcmp/streq.h"
29+
#include "string/strcmp/strprefix.h"
2930
#include "string/strerrno.h"
3031

3132

@@ -257,38 +258,32 @@ static shadowtcb_status unlink_suffs (const char *user)
257258
static shadowtcb_status
258259
rmdir_leading(const char *relpath)
259260
{
260-
char *ind, *dir, *path;
261+
char *ind, *path, *p;
261262
shadowtcb_status ret = SHADOWTCB_SUCCESS;
262263

263-
path = strdup(relpath);
264+
path = aprintf(TCB_DIR "/%s", relpath);
264265
if (path == NULL)
265266
goto oom;
266267

268+
p = strprefix(path, TCB_DIR "/");
267269

268-
while ((ind = strrchr (path, '/'))) {
270+
while ((ind = strrchr(p, '/'))) {
269271
stpcpy(ind, "");
270-
dir = aprintf(TCB_DIR "/%s", path);
271-
if (dir == NULL)
272-
goto free_path;
273272

274-
if (rmdir (dir) != 0) {
273+
if (rmdir(path) != 0) {
275274
if (errno != ENOTEMPTY) {
276275
fprintf (shadow_logfd,
277276
_("%s: Cannot remove directory %s: %s\n"),
278-
shadow_progname, dir, strerrno());
277+
shadow_progname, path, strerrno());
279278
ret = SHADOWTCB_FAILURE;
280279
}
281-
free (dir);
282280
break;
283281
}
284-
free (dir);
285282
}
286283

287284
free(path);
288285
return ret;
289286

290-
free_path:
291-
free(path);
292287
oom:
293288
OUT_OF_MEMORY;
294289
return SHADOWTCB_FAILURE;

0 commit comments

Comments
 (0)