emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Warnings/errors related to possibly clobbered variables


From: Stefan Monnier
Subject: Re: Warnings/errors related to possibly clobbered variables
Date: Tue, 13 Jan 2015 15:55:12 -0500
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/25.0.50 (gnu/linux)

>> > Why not declare the offending variables 'volatile' and forget about
>> > all this stuff?
>> FWIW, `volatile' might not necessarily give us the behavior we
>> want/need.
> How so?  Are you saying that qualifying an automatic variable as
> 'volatile' might change the semantics of the program?

GCC's warning basically points out that the semantics of the program is
unclear, because when we come back from a longjmp we could either see
the latest value of the variable, or a value it had earlier when we did
the setjmp.

So using `volatile' chooses one of the two behaviors, and it might not
be the one we need.

> IME, adding 'volatile' is generally TRT in these cases.  Plus, it's
> very simple, and goes "by the book" (a.k.a. the C Standard).

FWIW, the last time I had to fight with this kind of interaction between
longjmp and local variables, I ended up installing the patch below to
fix the random crashes I was seeing: the problem is more subtle than
just "add `volatile'".


        Stefan


diff --git a/src/bytecode.c b/src/bytecode.c
index 0ea646a..f1bdfd9 100644
--- a/src/bytecode.c
+++ b/src/bytecode.c
@@ -501,7 +501,6 @@ exec_byte_code (Lisp_Object bytestr, Lisp_Object vector, 
Lisp_Object maxdepth,
                Lisp_Object args_template, ptrdiff_t nargs, Lisp_Object *args)
 {
   ptrdiff_t count = SPECPDL_INDEX ();
-  ptrdiff_t volatile count_volatile;
 #ifdef BYTE_CODE_METER
   int volatile this_op = 0;
   int prev_op;
@@ -509,14 +508,12 @@ exec_byte_code (Lisp_Object bytestr, Lisp_Object vector, 
Lisp_Object maxdepth,
   int op;
   /* Lisp_Object v1, v2; */
   Lisp_Object *vectorp;
-  Lisp_Object *volatile vectorp_volatile;
 #ifdef BYTE_CODE_SAFE
-  ptrdiff_t volatile const_length;
-  Lisp_Object *volatile stacke;
-  ptrdiff_t volatile bytestr_length;
+  ptrdiff_t const_length;
+  Lisp_Object *stacke;
+  ptrdiff_t bytestr_length;
 #endif
   struct byte_stack stack;
-  struct byte_stack volatile stack_volatile;
   Lisp_Object *top;
   Lisp_Object result;
   enum handlertype type;
@@ -1122,9 +1119,6 @@ exec_byte_code (Lisp_Object bytestr, Lisp_Object vector, 
Lisp_Object maxdepth,
            PUSH_HANDLER (c, tag, type);
            c->bytecode_dest = dest;
            c->bytecode_top = top;
-           count_volatile = count;
-           stack_volatile = stack;
-           vectorp_volatile = vectorp;
 
            if (sys_setjmp (c->jmp))
              {
@@ -1135,12 +1129,11 @@ exec_byte_code (Lisp_Object bytestr, Lisp_Object 
vector, Lisp_Object maxdepth,
                handlerlist = c->next;
                PUSH (c->val);
                CHECK_RANGE (dest);
-               stack = stack_volatile;
+               /* Might have been re-set by longjmp!  */
+               stack.byte_string_start = SDATA (stack.byte_string);
                stack.pc = stack.byte_string_start + dest;
              }
 
-           count = count_volatile;
-           vectorp = vectorp_volatile;
            NEXT;
          }
 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]