delete_ref_loose(): don't muck around in the lock_file's filename
authorMichael Haggerty <mhagger@alum.mit.edu>
Wed, 1 Oct 2014 10:28:16 +0000 (12:28 +0200)
committerJunio C Hamano <gitster@pobox.com>
Wed, 1 Oct 2014 20:45:11 +0000 (13:45 -0700)
It's bad manners. Especially since there could be a signal during the
call to unlink_or_warn(), in which case the signal handler will see
the wrong filename and delete the reference file, leaving the lockfile
behind.

So make our own copy to work with.

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
refs.c
diff --git a/refs.c b/refs.c
index 73d6baedb09568c7938d6842fd4ed3f9a589ffbd..a4151315e32f38b89caa42704d16048b6c718521 100644 (file)
--- a/refs.c
+++ b/refs.c
@@ -2602,12 +2602,15 @@ int repack_without_refs(const char **refnames, int n, struct strbuf *err)
 static int delete_ref_loose(struct ref_lock *lock, int flag)
 {
        if (!(flag & REF_ISPACKED) || flag & REF_ISSYMREF) {
-               /* loose */
-               int err, i = strlen(lock->lk->filename) - LOCK_SUFFIX_LEN;
-
-               lock->lk->filename[i] = 0;
-               err = unlink_or_warn(lock->lk->filename);
-               lock->lk->filename[i] = LOCK_SUFFIX[0];
+               /*
+                * loose.  The loose file name is the same as the
+                * lockfile name, minus ".lock":
+                */
+               char *loose_filename = xmemdupz(
+                               lock->lk->filename,
+                               strlen(lock->lk->filename) - LOCK_SUFFIX_LEN);
+               int err = unlink_or_warn(loose_filename);
+               free(loose_filename);
                if (err && errno != ENOENT)
                        return 1;
        }