Fixed crash if mutex functions are used before any mutex has been created

This commit is contained in:
Sam Lantinga
2022-12-26 09:49:36 -08:00
parent d31776b17a
commit cc49f1e279

View File

@@ -68,10 +68,8 @@ static SDL_mutex *SDL_CreateMutex_srw(void)
static void SDL_DestroyMutex_srw(SDL_mutex *mutex) static void SDL_DestroyMutex_srw(SDL_mutex *mutex)
{ {
if (mutex != NULL) {
/* There are no kernel allocated resources */ /* There are no kernel allocated resources */
SDL_free(mutex); SDL_free(mutex);
}
} }
static int SDL_LockMutex_srw(SDL_mutex *_mutex) SDL_NO_THREAD_SAFETY_ANALYSIS /* clang doesn't know about NULL mutexes */ static int SDL_LockMutex_srw(SDL_mutex *_mutex) SDL_NO_THREAD_SAFETY_ANALYSIS /* clang doesn't know about NULL mutexes */
@@ -79,10 +77,6 @@ static int SDL_LockMutex_srw(SDL_mutex *_mutex) SDL_NO_THREAD_SAFETY_ANALYSIS /*
SDL_mutex_srw *mutex = (SDL_mutex_srw *)_mutex; SDL_mutex_srw *mutex = (SDL_mutex_srw *)_mutex;
DWORD this_thread; DWORD this_thread;
if (mutex == NULL) {
return 0;
}
this_thread = GetCurrentThreadId(); this_thread = GetCurrentThreadId();
if (mutex->owner == this_thread) { if (mutex->owner == this_thread) {
++mutex->count; ++mutex->count;
@@ -105,10 +99,6 @@ static int SDL_TryLockMutex_srw(SDL_mutex *_mutex)
DWORD this_thread; DWORD this_thread;
int retval = 0; int retval = 0;
if (mutex == NULL) {
return 0;
}
this_thread = GetCurrentThreadId(); this_thread = GetCurrentThreadId();
if (mutex->owner == this_thread) { if (mutex->owner == this_thread) {
++mutex->count; ++mutex->count;
@@ -128,10 +118,6 @@ static int SDL_UnlockMutex_srw(SDL_mutex *_mutex) SDL_NO_THREAD_SAFETY_ANALYSIS
{ {
SDL_mutex_srw *mutex = (SDL_mutex_srw *)_mutex; SDL_mutex_srw *mutex = (SDL_mutex_srw *)_mutex;
if (mutex == NULL) {
return 0;
}
if (mutex->owner == GetCurrentThreadId()) { if (mutex->owner == GetCurrentThreadId()) {
if (--mutex->count == 0) { if (--mutex->count == 0) {
mutex->owner = 0; mutex->owner = 0;
@@ -182,19 +168,15 @@ static SDL_mutex *SDL_CreateMutex_cs(void)
static void SDL_DestroyMutex_cs(SDL_mutex *mutex_) static void SDL_DestroyMutex_cs(SDL_mutex *mutex_)
{ {
SDL_mutex_cs *mutex = (SDL_mutex_cs *)mutex_; SDL_mutex_cs *mutex = (SDL_mutex_cs *)mutex_;
if (mutex != NULL) {
DeleteCriticalSection(&mutex->cs); DeleteCriticalSection(&mutex->cs);
SDL_free(mutex); SDL_free(mutex);
}
} }
/* Lock the mutex */ /* Lock the mutex */
static int SDL_LockMutex_cs(SDL_mutex *mutex_) SDL_NO_THREAD_SAFETY_ANALYSIS /* clang doesn't know about NULL mutexes */ static int SDL_LockMutex_cs(SDL_mutex *mutex_) SDL_NO_THREAD_SAFETY_ANALYSIS /* clang doesn't know about NULL mutexes */
{ {
SDL_mutex_cs *mutex = (SDL_mutex_cs *)mutex_; SDL_mutex_cs *mutex = (SDL_mutex_cs *)mutex_;
if (mutex == NULL) {
return 0;
}
EnterCriticalSection(&mutex->cs); EnterCriticalSection(&mutex->cs);
return 0; return 0;
@@ -205,9 +187,6 @@ static int SDL_TryLockMutex_cs(SDL_mutex *mutex_)
{ {
SDL_mutex_cs *mutex = (SDL_mutex_cs *)mutex_; SDL_mutex_cs *mutex = (SDL_mutex_cs *)mutex_;
int retval = 0; int retval = 0;
if (mutex == NULL) {
return 0;
}
if (TryEnterCriticalSection(&mutex->cs) == 0) { if (TryEnterCriticalSection(&mutex->cs) == 0) {
retval = SDL_MUTEX_TIMEDOUT; retval = SDL_MUTEX_TIMEDOUT;
@@ -219,9 +198,6 @@ static int SDL_TryLockMutex_cs(SDL_mutex *mutex_)
static int SDL_UnlockMutex_cs(SDL_mutex *mutex_) SDL_NO_THREAD_SAFETY_ANALYSIS /* clang doesn't know about NULL mutexes */ static int SDL_UnlockMutex_cs(SDL_mutex *mutex_) SDL_NO_THREAD_SAFETY_ANALYSIS /* clang doesn't know about NULL mutexes */
{ {
SDL_mutex_cs *mutex = (SDL_mutex_cs *)mutex_; SDL_mutex_cs *mutex = (SDL_mutex_cs *)mutex_;
if (mutex == NULL) {
return 0;
}
LeaveCriticalSection(&mutex->cs); LeaveCriticalSection(&mutex->cs);
return 0; return 0;
@@ -275,21 +251,35 @@ SDL_CreateMutex(void)
void SDL_DestroyMutex(SDL_mutex *mutex) void SDL_DestroyMutex(SDL_mutex *mutex)
{ {
if (mutex) {
SDL_mutex_impl_active.Destroy(mutex); SDL_mutex_impl_active.Destroy(mutex);
}
} }
int SDL_LockMutex(SDL_mutex *mutex) int SDL_LockMutex(SDL_mutex *mutex)
{ {
if (mutex == NULL) {
return 0;
}
return SDL_mutex_impl_active.Lock(mutex); return SDL_mutex_impl_active.Lock(mutex);
} }
int SDL_TryLockMutex(SDL_mutex *mutex) int SDL_TryLockMutex(SDL_mutex *mutex)
{ {
if (mutex == NULL) {
return 0;
}
return SDL_mutex_impl_active.TryLock(mutex); return SDL_mutex_impl_active.TryLock(mutex);
} }
int SDL_UnlockMutex(SDL_mutex *mutex) int SDL_UnlockMutex(SDL_mutex *mutex)
{ {
if (mutex == NULL) {
return 0;
}
return SDL_mutex_impl_active.Unlock(mutex); return SDL_mutex_impl_active.Unlock(mutex);
} }