Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit 7839632

Browse files
peffgitster
authored andcommitted
shallow: verify shallow file after taking lock
Before writing the shallow file, we stat() the existing file to make sure it has not been updated since our operation began. However, we do not do so under a lock, so there is a possible race: 1. Process A takes the lock. 2. Process B calls check_shallow_file_for_update and finds no update. 3. Process A commits the lockfile. 4. Process B takes the lock, then overwrite's process A's changes. We can fix this by doing our check while we hold the lock. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0179c94 commit 7839632

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

shallow.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,9 +263,9 @@ void setup_alternate_shallow(struct lock_file *shallow_lock,
263263
struct strbuf sb = STRBUF_INIT;
264264
int fd;
265265

266-
check_shallow_file_for_update();
267266
fd = hold_lock_file_for_update(shallow_lock, git_path("shallow"),
268267
LOCK_DIE_ON_ERROR);
268+
check_shallow_file_for_update();
269269
if (write_shallow_commits(&sb, 0, extra)) {
270270
if (write_in_full(fd, sb.buf, sb.len) != sb.len)
271271
die_errno("failed to write to %s",
@@ -310,9 +310,9 @@ void prune_shallow(int show_only)
310310
strbuf_release(&sb);
311311
return;
312312
}
313-
check_shallow_file_for_update();
314313
fd = hold_lock_file_for_update(&shallow_lock, git_path("shallow"),
315314
LOCK_DIE_ON_ERROR);
315+
check_shallow_file_for_update();
316316
if (write_shallow_commits_1(&sb, 0, NULL, SEEN_ONLY)) {
317317
if (write_in_full(fd, sb.buf, sb.len) != sb.len)
318318
die_errno("failed to write to %s",

0 commit comments

Comments
 (0)