packed_ref_cache: increment refcount when locked

Increment the packed_ref_cache reference count while it is locked to prevent its being freed. Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Haggerty committed Jun 20, 2013 at 10:37 UTC 4f6b83e3708943c3b3f4ac911da7d0c30019c20a
1 file changed +7 -1
refs.c
+7 -1
@@ -820,7 +820,9 @@ struct packed_ref_cache {
820 /*
821 * Iff the packed-refs file associated with this instance is
822 * currently locked for writing, this points at the associated
823 - * lock (which is owned by somebody else).
823 + * lock (which is owned by somebody else). The referrer count
824 + * is also incremented when the file is locked and decremented
825 + * when it is unlocked.
826 */
827 struct lock_file *lock;
828 };
@@ -2099,6 +2101,8 @@ int lock_packed_refs(int flags)
2101 /* Read the current packed-refs while holding the lock: */
2102 packed_ref_cache = get_packed_ref_cache(&ref_cache);
2103 packed_ref_cache->lock = &packlock;
2104 + /* Increment the reference count to prevent it from being freed: */
2105 + acquire_packed_ref_cache(packed_ref_cache);
2106 return 0;
2107 }
2108
@@ -2119,6 +2123,7 @@ int commit_packed_refs(void)
2123 if (commit_lock_file(packed_ref_cache->lock))
2124 error = -1;
2125 packed_ref_cache->lock = NULL;
2126 + release_packed_ref_cache(packed_ref_cache);
2127 return error;
2128 }
2129
@@ -2131,6 +2136,7 @@ void rollback_packed_refs(void)
2136 die("internal error: packed-refs not locked");
2137 rollback_lock_file(packed_ref_cache->lock);
2138 packed_ref_cache->lock = NULL;
2139 + release_packed_ref_cache(packed_ref_cache);
2140 clear_packed_ref_cache(&ref_cache);
2141 }
2142