From: Eli Zaretskii Date: Sun, 1 Sep 2013 15:43:43 +0000 (+0300) Subject: Fix crashes when unbind_for_thread_switch signals an error. X-Git-Tag: emacs-26.0.90~1144^2~17^2~5 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=bed10876dba330b24419a6144dc62db52bb273ab;p=emacs.git Fix crashes when unbind_for_thread_switch signals an error. src/eval.c (unbind_for_thread_switch): Accept a 'struct thread_state *' argument and use specpdl_ptr and specpdl of that thread. Fixes crashes if find_symbol_value signals an error. src/thread.c (post_acquire_global_lock): Update current_thread before calling unbind_for_thread_switch. Pass the previous thread to unbind_for_thread_switch. --- diff --git a/src/ChangeLog b/src/ChangeLog index 3e901d84db9..705b9c771df 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,13 @@ +2013-09-01 Eli Zaretskii + + * eval.c (unbind_for_thread_switch): Accept a 'struct + thread_state *' argument and use specpdl_ptr and specpdl of that + thread. Fixes crashes if find_symbol_value signals an error. + + * thread.c (post_acquire_global_lock): Update current_thread + before calling unbind_for_thread_switch. Pass the previous thread + to unbind_for_thread_switch. + 2013-08-31 Eli Zaretskii * systhread.c (sys_cond_init): Set the 'initialized' member to diff --git a/src/eval.c b/src/eval.c index 68a3691ad9b..b8a61590387 100644 --- a/src/eval.c +++ b/src/eval.c @@ -3484,11 +3484,11 @@ unbind_to (ptrdiff_t count, Lisp_Object value) } void -unbind_for_thread_switch (void) +unbind_for_thread_switch (struct thread_state *thr) { union specbinding *bind; - for (bind = specpdl_ptr; bind != specpdl; --bind) + for (bind = thr->m_specpdl_ptr; bind != thr->m_specpdl; --bind) { if (bind->kind >= SPECPDL_LET) { diff --git a/src/lisp.h b/src/lisp.h index 51c09e0abb3..03628e13ac2 100644 --- a/src/lisp.h +++ b/src/lisp.h @@ -3828,7 +3828,7 @@ extern void set_unwind_protect (ptrdiff_t, void (*) (Lisp_Object), Lisp_Object); extern void set_unwind_protect_ptr (ptrdiff_t, void (*) (void *), void *); extern Lisp_Object unbind_to (ptrdiff_t, Lisp_Object); extern void rebind_for_thread_switch (void); -extern void unbind_for_thread_switch (void); +extern void unbind_for_thread_switch (struct thread_state *); extern _Noreturn void error (const char *, ...) ATTRIBUTE_FORMAT_PRINTF (1, 2); extern _Noreturn void verror (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0); diff --git a/src/thread.c b/src/thread.c index 39a21518ec6..f060a002a3a 100644 --- a/src/thread.c +++ b/src/thread.c @@ -55,15 +55,20 @@ static void post_acquire_global_lock (struct thread_state *self) { Lisp_Object buffer; + struct thread_state *prev_thread = current_thread; - if (self != current_thread) + /* Do this early on, so that code below could signal errors (e.g., + unbind_for_thread_switch might) correctly, because we are already + running in the context of the thread pointed by SELF. */ + current_thread = self; + + if (prev_thread != current_thread) { - /* CURRENT_THREAD is NULL if the previously current thread + /* PREV_THREAD is NULL if the previously current thread exited. In this case, there is no reason to unbind, and trying will crash. */ - if (current_thread != NULL) - unbind_for_thread_switch (); - current_thread = self; + if (prev_thread != NULL) + unbind_for_thread_switch (prev_thread); rebind_for_thread_switch (); }