Skip to content

Commit 362b7ea

Browse files
committed
bin: copy: Fix various resource leaks introduced by refactor
Thanks coverity! <3 Fixes: d26e311 ("bin: copy: Refactor")
1 parent 4b3ee0f commit 362b7ea

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

src/bin/copy.c

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ static int curl(char *op, const char *path, const char *uri)
338338
char *argv[] = {
339339
"curl", "-L", op, NULL, NULL, NULL, NULL, NULL,
340340
};
341-
int err;
341+
int err = 1;
342342

343343
argv[3] = strdup(path);
344344
argv[4] = strdup(uri);
@@ -390,7 +390,7 @@ static int cp(const char *srcpath, const char *dstpath)
390390
char *argv[] = {
391391
"cp", NULL, NULL, NULL,
392392
};
393-
int err;
393+
int err = 1;
394394

395395
argv[1] = strdup(srcpath);
396396
argv[2] = strdup(dstpath);
@@ -442,13 +442,20 @@ static int get(const char *src, const struct infix_ds *ds, const char *path)
442442

443443
static int resolve_src(const char **src, const struct infix_ds **ds, char **path, bool *rm)
444444
{
445+
int tmpfd;
446+
445447
*src = infix_ds(*src, ds);
446448

447449
if (*ds || is_uri(*src)) {
448-
*path = tempnam(NULL, NULL);
450+
*path = strdup("/tmp/copy-XXXXXX");
449451
if (!*path)
450452
return 1;
451453

454+
tmpfd = mkstemp(*path);
455+
if (tmpfd < 0)
456+
return 1;
457+
458+
close(tmpfd);
452459
*rm = true;
453460
return 0;
454461
} else {
@@ -477,7 +484,7 @@ static int resolve_dst(const char **dst, const struct infix_ds **ds, char **path
477484
if (!(*ds)->path)
478485
return 0;
479486

480-
*path = (*ds)->path;
487+
*path = strdup((*ds)->path);
481488
} else if (is_uri(*dst)) {
482489
return 0;
483490
} else {
@@ -499,8 +506,8 @@ static int resolve_dst(const char **dst, const struct infix_ds **ds, char **path
499506

500507
static int copy(const char *src, const char *dst)
501508
{
509+
char *srcpath = NULL, *dstpath = NULL;
502510
const struct infix_ds *srcds, *dstds;
503-
char *srcpath, *dstpath;
504511
bool rmsrc = false;
505512
mode_t oldmask;
506513
int err = 1;
@@ -534,6 +541,9 @@ static int copy(const char *src, const char *dst)
534541
if (rmsrc)
535542
rmtmp(srcpath);
536543

544+
free(dstpath);
545+
free(srcpath);
546+
537547
sync();
538548
umask(oldmask);
539549
return err;

src/bin/erase.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ static int sanitize;
1616
static int do_erase(const char *name)
1717
{
1818
char *path;
19-
int rc;
19+
int rc = 0;
2020

2121
path = cfg_adjust(name, NULL, sanitize);
2222
if (!path) {
@@ -25,10 +25,8 @@ static int do_erase(const char *name)
2525
goto out;
2626
}
2727

28-
if (!yorn("Remove %s, are you sure?", path)) {
29-
rc = 0;
28+
if (!yorn("Remove %s, are you sure?", path))
3029
goto out;
31-
}
3230

3331
if (remove(path)) {
3432
fprintf(stderr, ERRMSG "failed removing %s: %s\n", path, strerror(errno));

0 commit comments

Comments
 (0)