Skip to content

Commit 5f8ef5b

Browse files
peffgitster
authored andcommitted
refs.c: avoid git_path assignment in lock_ref_sha1_basic
Assigning the result of git_path is a bad pattern, because it's not immediately obvious how long you expect the content to stay valid (and it may be overwritten by subsequent calls). Let's use a function-local strbuf here instead, which we know is safe (we just have to remember to free it in all code paths). As a bonus, we get rid of a confusing variable-reuse ("ref_file" is used for two distinct purposes). Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d6549f3 commit 5f8ef5b

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

refs.c

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2408,7 +2408,8 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
24082408
unsigned int flags, int *type_p,
24092409
struct strbuf *err)
24102410
{
2411-
const char *ref_file;
2411+
struct strbuf ref_file = STRBUF_INIT;
2412+
struct strbuf orig_ref_file = STRBUF_INIT;
24122413
const char *orig_refname = refname;
24132414
struct ref_lock *lock;
24142415
int last_errno = 0;
@@ -2432,20 +2433,19 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
24322433
refname = resolve_ref_unsafe(refname, resolve_flags,
24332434
lock->old_oid.hash, &type);
24342435
if (!refname && errno == EISDIR) {
2435-
/* we are trying to lock foo but we used to
2436+
/*
2437+
* we are trying to lock foo but we used to
24362438
* have foo/bar which now does not exist;
24372439
* it is normal for the empty directory 'foo'
24382440
* to remain.
24392441
*/
2440-
ref_file = git_path("%s", orig_refname);
2441-
if (remove_empty_directories(ref_file)) {
2442+
strbuf_git_path(&orig_ref_file, "%s", orig_refname);
2443+
if (remove_empty_directories(orig_ref_file.buf)) {
24422444
last_errno = errno;
2443-
24442445
if (!verify_refname_available(orig_refname, extras, skip,
24452446
get_loose_refs(&ref_cache), err))
24462447
strbuf_addf(err, "there are still refs under '%s'",
24472448
orig_refname);
2448-
24492449
goto error_return;
24502450
}
24512451
refname = resolve_ref_unsafe(orig_refname, resolve_flags,
@@ -2485,10 +2485,10 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
24852485
}
24862486
lock->ref_name = xstrdup(refname);
24872487
lock->orig_ref_name = xstrdup(orig_refname);
2488-
ref_file = git_path("%s", refname);
2488+
strbuf_git_path(&ref_file, "%s", refname);
24892489

24902490
retry:
2491-
switch (safe_create_leading_directories_const(ref_file)) {
2491+
switch (safe_create_leading_directories_const(ref_file.buf)) {
24922492
case SCLD_OK:
24932493
break; /* success */
24942494
case SCLD_VANISHED:
@@ -2497,11 +2497,12 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
24972497
/* fall through */
24982498
default:
24992499
last_errno = errno;
2500-
strbuf_addf(err, "unable to create directory for %s", ref_file);
2500+
strbuf_addf(err, "unable to create directory for %s",
2501+
ref_file.buf);
25012502
goto error_return;
25022503
}
25032504

2504-
if (hold_lock_file_for_update(lock->lk, ref_file, lflags) < 0) {
2505+
if (hold_lock_file_for_update(lock->lk, ref_file.buf, lflags) < 0) {
25052506
last_errno = errno;
25062507
if (errno == ENOENT && --attempts_remaining > 0)
25072508
/*
@@ -2511,20 +2512,25 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
25112512
*/
25122513
goto retry;
25132514
else {
2514-
unable_to_lock_message(ref_file, errno, err);
2515+
unable_to_lock_message(ref_file.buf, errno, err);
25152516
goto error_return;
25162517
}
25172518
}
25182519
if (old_sha1 && verify_lock(lock, old_sha1, mustexist, err)) {
25192520
last_errno = errno;
25202521
goto error_return;
25212522
}
2522-
return lock;
2523+
goto out;
25232524

25242525
error_return:
25252526
unlock_ref(lock);
2527+
lock = NULL;
2528+
2529+
out:
2530+
strbuf_release(&ref_file);
2531+
strbuf_release(&orig_ref_file);
25262532
errno = last_errno;
2527-
return NULL;
2533+
return lock;
25282534
}
25292535

25302536
/*

0 commit comments

Comments
 (0)