Fixed bug 4097 - Segmentation fault by SDL_CreateThreadWithStackSize
authorSam Lantinga <slouken@libsdl.org>
Sun, 25 Feb 2018 19:51:34 -0800
changeset 1190529bf3038a246
parent 11904 43dbca2813e1
child 11906 d97ab6d12404
Fixed bug 4097 - Segmentation fault by SDL_CreateThreadWithStackSize

Dongsun Kim

Normal case
1. [thread 1] SDL_CreateThreadWithStackSize calls SDL_SYS_CreateThread.
2. [thread 1] If successful, it calls SDL_SemWait.
3. [thread 2] SDL_RunThread calls SDL_SYS_SetupThread, SDL_ThreadID, SDL_SemPost.
4. [thread 1] SDL_CreateThreadWithStackSize calls SDL_DestroySemaphore, SDL_free.

Crash case (Segmentation fault)
1. [thread 1] SDL_CreateThreadWithStackSize calls SDL_SYS_CreateThread.
2. [thread 1] If successful, it calls SDL_SemWait.
--> Error return due to SIGNAL(SYSTEM or Real Time) at sem_wait(pthread).
3. [thread 1] SDL_CreateThreadWithStackSize calls SDL_DestroySemaphore, SDL_free.
4. [thread 2] SDL_RunThread calls SDL_SYS_SetupThread, SDL_ThreadID, SDL_SemPost.
--> Segmentation fault at strlen or sem_post.
src/thread/pthread/SDL_syssem.c
     1.1 --- a/src/thread/pthread/SDL_syssem.c	Sun Feb 25 19:22:47 2018 +0200
     1.2 +++ b/src/thread/pthread/SDL_syssem.c	Sun Feb 25 19:51:34 2018 -0800
     1.3 @@ -91,7 +91,10 @@
     1.4          return SDL_SetError("Passed a NULL semaphore");
     1.5      }
     1.6  
     1.7 -    retval = sem_wait(&sem->sem);
     1.8 +    do {
     1.9 +        retval = sem_wait(&sem->sem);
    1.10 +    } while (retval < 0 && errno == EINTR);
    1.11 +
    1.12      if (retval < 0) {
    1.13          retval = SDL_SetError("sem_wait() failed");
    1.14      }