struct lock_file: declare some fields volatile
authorMichael Haggerty <mhagger@alum.mit.edu>
Wed, 1 Oct 2014 10:28:28 +0000 (12:28 +0200)
committerJunio C Hamano <gitster@pobox.com>
Wed, 1 Oct 2014 20:49:00 +0000 (13:49 -0700)
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>
cache.h
lockfile.c
diff --git a/cache.h b/cache.h
index 8e25fce3d8286f2a8452e368fb8aaff463ae5dc7..c2ea6f15e6f5b7acf21abb95e7b1cd91a66473c6 100644 (file)
--- a/cache.h
+++ b/cache.h
@@ -575,10 +575,10 @@ extern int refresh_index(struct index_state *, unsigned int flags, const struct
 #define LOCK_SUFFIX_LEN 5
 
 struct lock_file {
-       struct lock_file *next;
+       struct lock_file *volatile next;
        volatile sig_atomic_t active;
-       int fd;
-       pid_t owner;
+       volatile int fd;
+       volatile pid_t owner;
        char on_list;
        char filename[PATH_MAX];
 };
index d35ac448f0e6259492e4d5c49554e161016bdfee..89043f5acc6c455e04d9c51ac26d244128d5e12b 100644 (file)
@@ -55,7 +55,7 @@
  *     on_list is set.
  */
 
-static struct lock_file *lock_file_list;
+static struct lock_file *volatile lock_file_list;
 
 static void remove_lock_file(void)
 {