Fixed crash if mutex functions are used before any mutex has been created (cherry picked from commit cc49f1e279b7ef0edf30d8eeaee9e05fd079bf82) (cherry picked from commit 488a91eb401c2642f80373bfe029b4e1db275d01)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
diff --git a/src/thread/windows/SDL_sysmutex.c b/src/thread/windows/SDL_sysmutex.c
index 70fd0ed..6091819 100644
--- a/src/thread/windows/SDL_sysmutex.c
+++ b/src/thread/windows/SDL_sysmutex.c
@@ -71,10 +71,8 @@ static SDL_mutex *SDL_CreateMutex_srw(void)
static void SDL_DestroyMutex_srw(SDL_mutex *mutex)
{
- if (mutex != NULL) {
- /* There are no kernel allocated resources */
- SDL_free(mutex);
- }
+ /* There are no kernel allocated resources */
+ SDL_free(mutex);
}
static int SDL_LockMutex_srw(SDL_mutex *_mutex) SDL_NO_THREAD_SAFETY_ANALYSIS /* clang doesn't know about NULL mutexes */
@@ -82,10 +80,6 @@ static int SDL_LockMutex_srw(SDL_mutex *_mutex) SDL_NO_THREAD_SAFETY_ANALYSIS /*
SDL_mutex_srw *mutex = (SDL_mutex_srw *)_mutex;
DWORD this_thread;
- if (mutex == NULL) {
- return 0;
- }
-
this_thread = GetCurrentThreadId();
if (mutex->owner == this_thread) {
++mutex->count;
@@ -108,10 +102,6 @@ static int SDL_TryLockMutex_srw(SDL_mutex *_mutex)
DWORD this_thread;
int retval = 0;
- if (mutex == NULL) {
- return 0;
- }
-
this_thread = GetCurrentThreadId();
if (mutex->owner == this_thread) {
++mutex->count;
@@ -131,10 +121,6 @@ static int SDL_UnlockMutex_srw(SDL_mutex *_mutex) SDL_NO_THREAD_SAFETY_ANALYSIS
{
SDL_mutex_srw *mutex = (SDL_mutex_srw *)_mutex;
- if (mutex == NULL) {
- return 0;
- }
-
if (mutex->owner == GetCurrentThreadId()) {
if (--mutex->count == 0) {
mutex->owner = 0;
@@ -185,19 +171,15 @@ static SDL_mutex *SDL_CreateMutex_cs(void)
static void SDL_DestroyMutex_cs(SDL_mutex *mutex_)
{
SDL_mutex_cs *mutex = (SDL_mutex_cs *)mutex_;
- if (mutex != NULL) {
- DeleteCriticalSection(&mutex->cs);
- SDL_free(mutex);
- }
+
+ DeleteCriticalSection(&mutex->cs);
+ SDL_free(mutex);
}
/* Lock the mutex */
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_;
- if (mutex == NULL) {
- return 0;
- }
EnterCriticalSection(&mutex->cs);
return 0;
@@ -208,9 +190,6 @@ static int SDL_TryLockMutex_cs(SDL_mutex *mutex_)
{
SDL_mutex_cs *mutex = (SDL_mutex_cs *)mutex_;
int retval = 0;
- if (mutex == NULL) {
- return 0;
- }
if (TryEnterCriticalSection(&mutex->cs) == 0) {
retval = SDL_MUTEX_TIMEDOUT;
@@ -222,9 +201,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 */
{
SDL_mutex_cs *mutex = (SDL_mutex_cs *)mutex_;
- if (mutex == NULL) {
- return 0;
- }
LeaveCriticalSection(&mutex->cs);
return 0;
@@ -277,21 +253,35 @@ SDL_mutex *SDL_CreateMutex(void)
void SDL_DestroyMutex(SDL_mutex *mutex)
{
- SDL_mutex_impl_active.Destroy(mutex);
+ if (mutex) {
+ SDL_mutex_impl_active.Destroy(mutex);
+ }
}
int SDL_LockMutex(SDL_mutex *mutex)
{
+ if (mutex == NULL) {
+ return 0;
+ }
+
return SDL_mutex_impl_active.Lock(mutex);
}
int SDL_TryLockMutex(SDL_mutex *mutex)
{
+ if (mutex == NULL) {
+ return 0;
+ }
+
return SDL_mutex_impl_active.TryLock(mutex);
}
int SDL_UnlockMutex(SDL_mutex *mutex)
{
+ if (mutex == NULL) {
+ return 0;
+ }
+
return SDL_mutex_impl_active.Unlock(mutex);
}