Unix: Don't send quit events during signal handler.
authorRyan C. Gordon <icculus@icculus.org>
Mon, 08 Jun 2015 01:52:43 -0400
changeset 9720fd60d77139fc
parent 9719 8e78db33b5de
child 9721 957a6ed39ad0
Unix: Don't send quit events during signal handler.

Make note to send it, and send next time we SDL_PumpEvents().

Otherwise, we might be trying to use malloc() to push a new event on the
queue while a signal is interrupting malloc() elsewhere, usually causing a
crash.

Fixes Bugzilla #2870.
src/events/SDL_events.c
src/events/SDL_events_c.h
src/events/SDL_quit.c
     1.1 --- a/src/events/SDL_events.c	Mon Jun 08 01:17:58 2015 -0400
     1.2 +++ b/src/events/SDL_events.c	Mon Jun 08 01:52:43 2015 -0400
     1.3 @@ -406,6 +406,8 @@
     1.4          SDL_JoystickUpdate();
     1.5      }
     1.6  #endif
     1.7 +
     1.8 +    SDL_SendPendingQuit();  /* in case we had a signal handler fire, etc. */
     1.9  }
    1.10  
    1.11  /* Public functions */
     2.1 --- a/src/events/SDL_events_c.h	Mon Jun 08 01:17:58 2015 -0400
     2.2 +++ b/src/events/SDL_events_c.h	Mon Jun 08 01:52:43 2015 -0400
     2.3 @@ -43,6 +43,8 @@
     2.4  extern int SDL_SendQuit(void);
     2.5  extern void SDL_QuitQuit(void);
     2.6  
     2.7 +extern void SDL_SendPendingQuit(void);
     2.8 +
     2.9  /* The event filter function */
    2.10  extern SDL_EventFilter SDL_EventOK;
    2.11  extern void *SDL_EventOKParam;
     3.1 --- a/src/events/SDL_quit.c	Mon Jun 08 01:17:58 2015 -0400
     3.2 +++ b/src/events/SDL_quit.c	Mon Jun 08 01:52:43 2015 -0400
     3.3 @@ -20,6 +20,7 @@
     3.4  */
     3.5  #include "../SDL_internal.h"
     3.6  #include "SDL_hints.h"
     3.7 +#include "SDL_assert.h"
     3.8  
     3.9  /* General quit handling code for SDL */
    3.10  
    3.11 @@ -30,8 +31,8 @@
    3.12  #include "SDL_events.h"
    3.13  #include "SDL_events_c.h"
    3.14  
    3.15 -
    3.16  static SDL_bool disable_signals = SDL_FALSE;
    3.17 +static SDL_bool send_quit_pending = SDL_FALSE;
    3.18  
    3.19  #ifdef HAVE_SIGNAL_H
    3.20  static void
    3.21 @@ -40,8 +41,9 @@
    3.22      /* Reset the signal handler */
    3.23      signal(sig, SDL_HandleSIG);
    3.24  
    3.25 -    /* Signal a quit interrupt */
    3.26 -    SDL_SendQuit();
    3.27 +    /* Send a quit event next time the event loop pumps. */
    3.28 +    /* We can't send it in signal handler; malloc() might be interrupted! */
    3.29 +    send_quit_pending = SDL_TRUE;
    3.30  }
    3.31  #endif /* HAVE_SIGNAL_H */
    3.32  
    3.33 @@ -136,7 +138,17 @@
    3.34  int
    3.35  SDL_SendQuit(void)
    3.36  {
    3.37 +    send_quit_pending = SDL_FALSE;
    3.38      return SDL_SendAppEvent(SDL_QUIT);
    3.39  }
    3.40  
    3.41 +void
    3.42 +SDL_SendPendingQuit(void)
    3.43 +{
    3.44 +    if (send_quit_pending) {
    3.45 +        SDL_SendQuit();
    3.46 +        SDL_assert(!send_quit_pending);
    3.47 +    }
    3.48 +}
    3.49 +
    3.50  /* vi: set ts=4 sw=4 expandtab: */