Skip to content

Commit b227586

Browse files
Martin Ågrengitster
authored andcommitted
lock_file: make function-local locks non-static
Placing `struct lock_file`s on the stack used to be a bad idea, because the temp- and lockfile-machinery would keep a pointer into the struct. But after 076aa2c (tempfile: auto-allocate tempfiles on heap, 2017-09-05), we can safely have lockfiles on the stack. (This applies even if a user returns early, leaving a locked lock behind.) These `struct lock_file`s are local to their respective functions and we can drop their staticness. For good measure, I have inspected these sites and come to believe that they always release the lock, with the possible exception of bailing out using `die()` or `exit()` or by returning from a `cmd_foo()`. As pointed out by Jeff King, it would be bad if someone held on to a `struct lock_file *` for some reason. After some grepping, I agree with his findings: no-one appears to be doing that. Signed-off-by: Martin Ågren <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3c6fad4 commit b227586

File tree

10 files changed

+11
-11
lines changed

10 files changed

+11
-11
lines changed

apply.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4058,7 +4058,7 @@ static int build_fake_ancestor(struct apply_state *state, struct patch *list)
40584058
{
40594059
struct patch *patch;
40604060
struct index_state result = { NULL };
4061-
static struct lock_file lock;
4061+
struct lock_file lock = LOCK_INIT;
40624062
int res;
40634063

40644064
/* Once we start supporting the reverse patch, it may be

builtin/describe.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
612612
suffix = broken;
613613
}
614614
} else if (dirty) {
615-
static struct lock_file index_lock;
615+
struct lock_file index_lock = LOCK_INIT;
616616
struct rev_info revs;
617617
struct argv_array args = ARGV_ARRAY_INIT;
618618
int fd, result;

builtin/difftool.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
610610
continue;
611611

612612
if (!indices_loaded) {
613-
static struct lock_file lock;
613+
struct lock_file lock = LOCK_INIT;
614614
strbuf_reset(&buf);
615615
strbuf_addf(&buf, "%s/wtindex", tmpdir);
616616
if (hold_lock_file_for_update(&lock, buf.buf, 0) < 0 ||

builtin/gc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ static int need_to_gc(void)
233233
/* return NULL on success, else hostname running the gc */
234234
static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
235235
{
236-
static struct lock_file lock;
236+
struct lock_file lock = LOCK_INIT;
237237
char my_host[HOST_NAME_MAX + 1];
238238
struct strbuf sb = STRBUF_INIT;
239239
struct stat st;

builtin/merge.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,7 @@ static int try_merge_strategy(const char *strategy, struct commit_list *common,
647647
struct commit_list *remoteheads,
648648
struct commit *head)
649649
{
650-
static struct lock_file lock;
650+
struct lock_file lock = LOCK_INIT;
651651
const char *head_arg = "HEAD";
652652

653653
hold_locked_index(&lock, LOCK_DIE_ON_ERROR);
@@ -805,7 +805,7 @@ static int merge_trivial(struct commit *head, struct commit_list *remoteheads)
805805
{
806806
struct object_id result_tree, result_commit;
807807
struct commit_list *parents, **pptr = &parents;
808-
static struct lock_file lock;
808+
struct lock_file lock = LOCK_INIT;
809809

810810
hold_locked_index(&lock, LOCK_DIE_ON_ERROR);
811811
refresh_cache(REFRESH_QUIET);

builtin/receive-pack.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -875,7 +875,7 @@ static void refuse_unconfigured_deny_delete_current(void)
875875
static int command_singleton_iterator(void *cb_data, struct object_id *oid);
876876
static int update_shallow_ref(struct command *cmd, struct shallow_info *si)
877877
{
878-
static struct lock_file shallow_lock;
878+
struct lock_file shallow_lock = LOCK_INIT;
879879
struct oid_array extra = OID_ARRAY_INIT;
880880
struct check_connected_options opt = CHECK_CONNECTED_INIT;
881881
uint32_t mask = 1 << (cmd->index % 32);

bundle.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ static int write_bundle_refs(int bundle_fd, struct rev_info *revs)
409409
int create_bundle(struct bundle_header *header, const char *path,
410410
int argc, const char **argv)
411411
{
412-
static struct lock_file lock;
412+
struct lock_file lock = LOCK_INIT;
413413
int bundle_fd = -1;
414414
int bundle_to_stdout;
415415
int ref_count = 0;

fast-import.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1858,7 +1858,7 @@ static void dump_marks_helper(FILE *f,
18581858

18591859
static void dump_marks(void)
18601860
{
1861-
static struct lock_file mark_lock;
1861+
struct lock_file mark_lock = LOCK_INIT;
18621862
FILE *f;
18631863

18641864
if (!export_marks_file || (import_marks_file && !import_marks_file_done))

refs/files-backend.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2989,7 +2989,7 @@ static int files_reflog_expire(struct ref_store *ref_store,
29892989
{
29902990
struct files_ref_store *refs =
29912991
files_downcast(ref_store, REF_STORE_WRITE, "reflog_expire");
2992-
static struct lock_file reflog_lock;
2992+
struct lock_file reflog_lock = LOCK_INIT;
29932993
struct expire_reflog_cb cb;
29942994
struct ref_lock *lock;
29952995
struct strbuf log_file_sb = STRBUF_INIT;

shallow.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ void advertise_shallow_grafts(int fd)
353353
*/
354354
void prune_shallow(int show_only)
355355
{
356-
static struct lock_file shallow_lock;
356+
struct lock_file shallow_lock = LOCK_INIT;
357357
struct strbuf sb = STRBUF_INIT;
358358
int fd;
359359

0 commit comments

Comments
 (0)