SDL_mixer: Fixed crash if music frameworks are missing on iOS or tvOS platforms

From ecce55a7094b5056a0695417cb9c120c951b4d6c Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Mon, 2 May 2022 01:04:12 -0700
Subject: [PATCH] Fixed crash if music frameworks are missing on iOS or tvOS
 platforms

---
 src/codecs/music_flac.c    | 11 ++---------
 src/codecs/music_mikmod.c  | 11 ++---------
 src/codecs/music_modplug.c | 10 ++--------
 src/codecs/music_mpg123.c  | 11 ++---------
 src/codecs/music_ogg.c     | 11 ++---------
 src/codecs/music_opus.c    | 10 ++--------
 src/codecs/music_xmp.c     | 10 ++--------
 7 files changed, 14 insertions(+), 60 deletions(-)

diff --git a/src/codecs/music_flac.c b/src/codecs/music_flac.c
index 586b2c09..ab02f971 100644
--- a/src/codecs/music_flac.c
+++ b/src/codecs/music_flac.c
@@ -77,7 +77,8 @@ static flac_loader flac;
     if (flac.FUNC == NULL) { SDL_UnloadObject(flac.handle); return -1; }
 #else
 #define FUNCTION_LOADER(FUNC, SIG) \
-    flac.FUNC = FUNC;
+    flac.FUNC = FUNC; \
+    if (flac.FUNC == NULL) { Mix_SetError("Missing FLAC.framework"); return -1; }
 #endif
 
 static int FLAC_Load(void)
@@ -88,14 +89,6 @@ static int FLAC_Load(void)
         if (flac.handle == NULL) {
             return -1;
         }
-#elif defined(__MACOSX__)
-        extern FLAC__StreamDecoder *FLAC__stream_decoder_new(void) __attribute__((weak_import));
-        if (FLAC__stream_decoder_new == NULL)
-        {
-            /* Missing weakly linked framework */
-            Mix_SetError("Missing FLAC.framework");
-            return -1;
-        }
 #endif
 
         FUNCTION_LOADER(FLAC__stream_decoder_new, FLAC__StreamDecoder *(*)(void))
diff --git a/src/codecs/music_mikmod.c b/src/codecs/music_mikmod.c
index 3f2f221c..51a06e5b 100644
--- a/src/codecs/music_mikmod.c
+++ b/src/codecs/music_mikmod.c
@@ -80,7 +80,8 @@ static mikmod_loader mikmod;
     if (mikmod.NAME == NULL) { SDL_UnloadObject(mikmod.handle); return -1; }
 #else
 #define FUNCTION_LOADER(FUNC, SIG) \
-    mikmod.FUNC = FUNC;
+    mikmod.FUNC = FUNC; \
+    if (mikmod.FUNC == NULL) { Mix_SetError("Missing mikmod.framework"); return -1; }
 #define VARIABLE_LOADER(NAME, SIG) \
     mikmod.NAME = &NAME;
 #endif
@@ -93,14 +94,6 @@ static int MIKMOD_Load(void)
         if (mikmod.handle == NULL) {
             return -1;
         }
-#elif defined(__MACOSX__)
-        extern void Player_Start(MODULE*) __attribute__((weak_import));
-        if (Player_Start == NULL)
-        {
-            /* Missing weakly linked framework */
-            Mix_SetError("Missing mikmod.framework");
-            return -1;
-        }
 #endif
         FUNCTION_LOADER(MikMod_Exit, void (*)(void))
         FUNCTION_LOADER(MikMod_InfoDriver, CHAR* (*)(void))
diff --git a/src/codecs/music_modplug.c b/src/codecs/music_modplug.c
index f213c810..3b8cc479 100644
--- a/src/codecs/music_modplug.c
+++ b/src/codecs/music_modplug.c
@@ -53,7 +53,8 @@ static ModPlug_Settings settings;
     if (modplug.FUNC == NULL) { SDL_UnloadObject(modplug.handle); return -1; }
 #else
 #define FUNCTION_LOADER(FUNC, SIG) \
-    modplug.FUNC = FUNC;
+    modplug.FUNC = FUNC; \
+    if (modplug.FUNC == NULL) { Mix_SetError("Missing libmodplug.framework"); return -1; }
 #endif
 
 static int MODPLUG_Load(void)
@@ -64,13 +65,6 @@ static int MODPLUG_Load(void)
         if (modplug.handle == NULL) {
             return -1;
         }
-#elif defined(__MACOSX__)
-        extern ModPlugFile* ModPlug_Load(const void* data, int size) __attribute__((weak_import));
-        if (ModPlug_Load == NULL) {
-            /* Missing weakly linked framework */
-            Mix_SetError("Missing modplug.framework");
-            return -1;
-        }
 #endif
         FUNCTION_LOADER(ModPlug_Load, ModPlugFile* (*)(const void* data, int size))
         FUNCTION_LOADER(ModPlug_Unload, void (*)(ModPlugFile* file))
diff --git a/src/codecs/music_mpg123.c b/src/codecs/music_mpg123.c
index 969b7662..c174f253 100644
--- a/src/codecs/music_mpg123.c
+++ b/src/codecs/music_mpg123.c
@@ -77,7 +77,8 @@ static mpg123_loader mpg123;
     if (mpg123.FUNC == NULL) { SDL_UnloadObject(mpg123.handle); return -1; }
 #else
 #define FUNCTION_LOADER(FUNC, SIG) \
-    mpg123.FUNC = FUNC;
+    mpg123.FUNC = FUNC; \
+    if (mpg123.FUNC == NULL) { Mix_SetError("Missing mpg123.framework"); return -1; }
 #endif
 
 static int MPG123_Load(void)
@@ -88,14 +89,6 @@ static int MPG123_Load(void)
         if (mpg123.handle == NULL) {
             return -1;
         }
-#elif defined(__MACOSX__)
-        extern int mpg123_init(void) __attribute__((weak_import));
-        if (mpg123_init == NULL)
-        {
-            /* Missing weakly linked framework */
-            Mix_SetError("Missing mpg123.framework");
-            return -1;
-        }
 #endif
         FUNCTION_LOADER(mpg123_close, int (*)(mpg123_handle *mh))
         FUNCTION_LOADER(mpg123_delete, void (*)(mpg123_handle *mh))
diff --git a/src/codecs/music_ogg.c b/src/codecs/music_ogg.c
index 4ce49130..919cdef5 100644
--- a/src/codecs/music_ogg.c
+++ b/src/codecs/music_ogg.c
@@ -67,7 +67,8 @@ static vorbis_loader vorbis;
     if (vorbis.FUNC == NULL) { SDL_UnloadObject(vorbis.handle); return -1; }
 #else
 #define FUNCTION_LOADER(FUNC, SIG) \
-    vorbis.FUNC = FUNC;
+    vorbis.FUNC = FUNC; \
+    if (vorbis.FUNC == NULL) { Mix_SetError("Missing vorbis.framework or tremor.framework"); return -1; }
 #endif
 
 static int OGG_Load(void)
@@ -78,14 +79,6 @@ static int OGG_Load(void)
         if (vorbis.handle == NULL) {
             return -1;
         }
-#elif defined(__MACOSX__)
-        extern int ov_open_callbacks(void*, OggVorbis_File*, const char*, long, ov_callbacks) __attribute__((weak_import));
-        if (ov_open_callbacks == NULL)
-        {
-            /* Missing weakly linked framework */
-            Mix_SetError("Missing Vorbis.framework");
-            return -1;
-        }
 #endif
         FUNCTION_LOADER(ov_clear, int (*)(OggVorbis_File *))
         FUNCTION_LOADER(ov_info, vorbis_info *(*)(OggVorbis_File *,int))
diff --git a/src/codecs/music_opus.c b/src/codecs/music_opus.c
index c25d96fe..1a79e9c8 100644
--- a/src/codecs/music_opus.c
+++ b/src/codecs/music_opus.c
@@ -52,7 +52,8 @@ static opus_loader opus;
     if (opus.FUNC == NULL) { SDL_UnloadObject(opus.handle); return -1; }
 #else
 #define FUNCTION_LOADER(FUNC, SIG) \
-    opus.FUNC = FUNC;
+    opus.FUNC = FUNC; \
+    if (opus.FUNC == NULL) { Mix_SetError("Missing opus.framework"); return -1; }
 #endif
 
 static int OPUS_Load(void)
@@ -63,13 +64,6 @@ static int OPUS_Load(void)
         if (opus.handle == NULL) {
             return -1;
         }
-#elif defined(__MACOSX__)
-        extern OggOpusFile *op_open_callbacks(void *,const OpusFileCallbacks *,const unsigned char *,size_t,int *) __attribute__((weak_import));
-        if (op_open_callbacks == NULL) {
-            /* Missing weakly linked framework */
-            Mix_SetError("Missing OpusFile.framework");
-            return -1;
-        }
 #endif
         FUNCTION_LOADER(op_open_callbacks, OggOpusFile *(*)(void *,const OpusFileCallbacks *,const unsigned char *,size_t,int *))
         FUNCTION_LOADER(op_tags, const OpusTags *(*)(const OggOpusFile *,int))
diff --git a/src/codecs/music_xmp.c b/src/codecs/music_xmp.c
index 765878c3..a3d03464 100644
--- a/src/codecs/music_xmp.c
+++ b/src/codecs/music_xmp.c
@@ -64,7 +64,8 @@ static xmp_loader libxmp;
     if (libxmp.FUNC == NULL) { SDL_UnloadObject(libxmp.handle); return -1; }
 #else
 #define FUNCTION_LOADER(FUNC, SIG) \
-    libxmp.FUNC = FUNC;
+    libxmp.FUNC = FUNC; \
+    if (lib.FUNC == NULL) { Mix_SetError("Missing xmp.framework"); return -1; }
 #endif
 
 static int XMP_Load(void)
@@ -75,13 +76,6 @@ static int XMP_Load(void)
         if (libxmp.handle == NULL) {
             return -1;
         }
-#elif defined(__MACOSX__)
-        extern xmp_context xmp_create_context(void) __attribute__((weak_import));
-        if (xmp_create_context == NULL) {
-            /* Missing weakly linked framework */
-            Mix_SetError("Missing xmp.framework");
-            return -1;
-        }
 #endif
         FUNCTION_LOADER(xmp_create_context, xmp_context(*)(void))
         FUNCTION_LOADER(xmp_load_module_from_memory, int(*)(xmp_context,LIBXMP_CONST void *,long))