From 7b058bd042a7c624c277aaddbca6fb2f6ae5233d Mon Sep 17 00:00:00 2001 From: GaryOderNichts <12049776+GaryOderNichts@users.noreply.github.com> Date: Wed, 8 Nov 2023 19:56:46 +0100 Subject: [PATCH] wiiu/thread: Add missing NULL checks to mutex code According to the changes in d59caffe2c8e80aa9d38574fd17974ec5cf05d2b: > [...] SDL has the concept of a NULL mutex, so the mutex functions have been changed not to report errors if a mutex hasn't been initialized. > We do have mutexes that might be accessed when they are NULL, notably in the event system, so this is an important change. --- src/thread/wiiu/SDL_sysmutex.c | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/thread/wiiu/SDL_sysmutex.c b/src/thread/wiiu/SDL_sysmutex.c index f0c81699a..4d80b161e 100644 --- a/src/thread/wiiu/SDL_sysmutex.c +++ b/src/thread/wiiu/SDL_sysmutex.c @@ -34,22 +34,30 @@ SDL_CreateMutex(void) /* Allocate the structure */ mutex = (OSMutex *) SDL_calloc(1, sizeof(OSMutex)); - OSInitMutex(mutex); + if (mutex != NULL) { + OSInitMutex(mutex); + } else { + SDL_OutOfMemory(); + } return (SDL_mutex *)mutex; } void SDL_DestroyMutex(SDL_mutex * mutex) { - if (mutex) { + if (mutex != NULL) { SDL_free(mutex); } } /* Lock the mutex */ int -SDL_LockMutex(SDL_mutex * mutex) +SDL_LockMutex(SDL_mutex * mutex) SDL_NO_THREAD_SAFETY_ANALYSIS /* clang doesn't know about NULL mutexes */ { + if (mutex == NULL) { + return 0; + } + OSLockMutex((OSMutex *)mutex); return 0; } @@ -57,12 +65,20 @@ SDL_LockMutex(SDL_mutex * mutex) int SDL_TryLockMutex(SDL_mutex * mutex) { + if (mutex == NULL) { + return 0; + } + return OSTryLockMutex((OSMutex *)mutex) ? 0 : SDL_MUTEX_TIMEDOUT; } int -SDL_UnlockMutex(SDL_mutex * mutex) +SDL_UnlockMutex(SDL_mutex * mutex) SDL_NO_THREAD_SAFETY_ANALYSIS /* clang doesn't know about NULL mutexes */ { + if (mutex == NULL) { + return 0; + } + OSUnlockMutex((OSMutex *)mutex); return 0; }