Skip to content

Commit e3cf230

Browse files
peffgitster
authored andcommitted
prefer mkpathdup to mkpath in assignments
As with the previous commit to git_path, assigning the result of mkpath is suspicious, since it is not clear whether we will still depend on the value after it may have been overwritten by subsequent calls. This patch converts low-hanging fruit to use mkpathdup instead of mkpath (with the downside that we must remember to free the result). Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent fcd12db commit e3cf230

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

builtin/repack.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -285,27 +285,28 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
285285
failed = 0;
286286
for_each_string_list_item(item, &names) {
287287
for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {
288-
const char *fname_old;
289-
char *fname;
288+
char *fname, *fname_old;
290289
fname = mkpathdup("%s/pack-%s%s", packdir,
291290
item->string, exts[ext].name);
292291
if (!file_exists(fname)) {
293292
free(fname);
294293
continue;
295294
}
296295

297-
fname_old = mkpath("%s/old-%s%s", packdir,
296+
fname_old = mkpathdup("%s/old-%s%s", packdir,
298297
item->string, exts[ext].name);
299298
if (file_exists(fname_old))
300299
if (unlink(fname_old))
301300
failed = 1;
302301

303302
if (!failed && rename(fname, fname_old)) {
304303
free(fname);
304+
free(fname_old);
305305
failed = 1;
306306
break;
307307
} else {
308308
string_list_append(&rollback, fname);
309+
free(fname_old);
309310
}
310311
}
311312
if (failed)
@@ -314,13 +315,13 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
314315
if (failed) {
315316
struct string_list rollback_failure = STRING_LIST_INIT_DUP;
316317
for_each_string_list_item(item, &rollback) {
317-
const char *fname_old;
318-
char *fname;
318+
char *fname, *fname_old;
319319
fname = mkpathdup("%s/%s", packdir, item->string);
320-
fname_old = mkpath("%s/old-%s", packdir, item->string);
320+
fname_old = mkpathdup("%s/old-%s", packdir, item->string);
321321
if (rename(fname_old, fname))
322322
string_list_append(&rollback_failure, fname);
323323
free(fname);
324+
free(fname_old);
324325
}
325326

326327
if (rollback_failure.nr) {
@@ -368,13 +369,14 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
368369
/* Remove the "old-" files */
369370
for_each_string_list_item(item, &names) {
370371
for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {
371-
const char *fname;
372-
fname = mkpath("%s/old-%s%s",
373-
packdir,
374-
item->string,
375-
exts[ext].name);
372+
char *fname;
373+
fname = mkpathdup("%s/old-%s%s",
374+
packdir,
375+
item->string,
376+
exts[ext].name);
376377
if (remove_path(fname))
377378
warning(_("removing '%s' failed"), fname);
379+
free(fname);
378380
}
379381
}
380382

refs.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3380,7 +3380,7 @@ static int commit_ref_update(struct ref_lock *lock,
33803380
int create_symref(const char *ref_target, const char *refs_heads_master,
33813381
const char *logmsg)
33823382
{
3383-
const char *lockpath;
3383+
char *lockpath = NULL;
33843384
char ref[1000];
33853385
int fd, len, written;
33863386
char *git_HEAD = git_pathdup("%s", ref_target);
@@ -3407,7 +3407,7 @@ int create_symref(const char *ref_target, const char *refs_heads_master,
34073407
error("refname too long: %s", refs_heads_master);
34083408
goto error_free_return;
34093409
}
3410-
lockpath = mkpath("%s.lock", git_HEAD);
3410+
lockpath = mkpathdup("%s.lock", git_HEAD);
34113411
fd = open(lockpath, O_CREAT | O_EXCL | O_WRONLY, 0666);
34123412
if (fd < 0) {
34133413
error("Unable to open %s for writing", lockpath);
@@ -3427,9 +3427,11 @@ int create_symref(const char *ref_target, const char *refs_heads_master,
34273427
error_unlink_return:
34283428
unlink_or_warn(lockpath);
34293429
error_free_return:
3430+
free(lockpath);
34303431
free(git_HEAD);
34313432
return -1;
34323433
}
3434+
free(lockpath);
34333435

34343436
#ifndef NO_SYMLINK_HEAD
34353437
done:

0 commit comments

Comments
 (0)