sdl2-compat: Fixed macOS crashes in testautomation

From a81922afec9657c35935052face7912ac102d68a Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Thu, 8 Aug 2024 14:12:32 -0700
Subject: [PATCH] Fixed macOS crashes in testautomation

---
 src/sdl2_compat.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)

diff --git a/src/sdl2_compat.c b/src/sdl2_compat.c
index 6fd48a3..b6c81bd 100644
--- a/src/sdl2_compat.c
+++ b/src/sdl2_compat.c
@@ -2045,17 +2045,19 @@ SDL_RWFromFile(const char *file, const char *mode)
     #if defined(SDL_PLATFORM_APPLE)
     char *adjusted_path = NULL;
     /* If the file mode is writable, skip all the bundle stuff because generally the bundle is read-only. */
-    if ((SDL3_strchr(mode, 'r') != NULL) && SDL3_GetHintBoolean(SDL_HINT_APPLE_RWFROMFILE_USE_RESOURCES, SDL_TRUE)) {
+    if (mode && (SDL3_strchr(mode, 'r') != NULL) && SDL3_GetHintBoolean(SDL_HINT_APPLE_RWFROMFILE_USE_RESOURCES, SDL_TRUE)) {
         const char *base = SDL3_GetBasePath();
-        if (!base) {
-            return NULL;
-        } else if (SDL3_asprintf(&adjusted_path, "%s%s", base, file) < 0) {
-            return NULL;
+        if (base) {
+            if (SDL3_asprintf(&adjusted_path, "%s%s", base, file) < 0) {
+                return NULL;
+            }
+            rwops2 = RWops3to2(SDL3_IOFromFile(adjusted_path, mode), SDL_RWOPS_PLATFORM_FILE);
+            SDL_free(adjusted_path);
         }
-        file = adjusted_path;
     }
-    rwops2 = RWops3to2(SDL3_IOFromFile(file, mode), SDL_RWOPS_PLATFORM_FILE);
-    SDL_free(adjusted_path);
+    if (!rwops2) {
+        rwops2 = RWops3to2(SDL3_IOFromFile(file, mode), SDL_RWOPS_PLATFORM_FILE);
+    }
     #else
     rwops2 = RWops3to2(SDL3_IOFromFile(file, mode), SDL_RWOPS_PLATFORM_FILE);
     #endif