Skip to content

Commit 2091c50

Browse files
mhaggergitster
authored andcommitted
struct lock_file: declare some fields volatile
The function remove_lock_file_on_signal() is used as a signal handler. It is not realistic to make the signal handler conform strictly to the C standard, which is very restrictive about what a signal handler is allowed to do. But let's increase the likelihood that it will work: The lock_file_list global variable and several fields from struct lock_file are used by the signal handler. Declare those values "volatile" to (1) force the main process to write the values to RAM promptly, and (2) prevent updates to these fields from being reordered in a way that leaves an opportunity for a jump to the signal handler while the object is in an inconsistent state. We don't mark the filename field volatile because that would prevent the use of strcpy(), and it is anyway unlikely that a compiler re-orders a strcpy() call across other expressions. So in practice it should be possible to get away without "volatile" in the "filename" case. Suggested-by: Johannes Sixt <j6t@kdbg.org> Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 707103f commit 2091c50

2 files changed

Lines changed: 4 additions & 4 deletions

File tree

cache.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -575,10 +575,10 @@ extern int refresh_index(struct index_state *, unsigned int flags, const struct
575575
#define LOCK_SUFFIX_LEN 5
576576

577577
struct lock_file {
578-
struct lock_file *next;
578+
struct lock_file *volatile next;
579579
volatile sig_atomic_t active;
580-
int fd;
581-
pid_t owner;
580+
volatile int fd;
581+
volatile pid_t owner;
582582
char on_list;
583583
char filename[PATH_MAX];
584584
};

lockfile.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
* on_list is set.
5656
*/
5757

58-
static struct lock_file *lock_file_list;
58+
static struct lock_file *volatile lock_file_list;
5959

6060
static void remove_lock_file(void)
6161
{

0 commit comments

Comments
 (0)