SDL: Fix improper handling of NULL dialog filter on Cocoa

From 4ac8968f650a599f4607c72c3080acff229fb396 Mon Sep 17 00:00:00 2001
From: Semphris <[EMAIL REDACTED]>
Date: Thu, 2 May 2024 12:22:23 -0400
Subject: [PATCH] Fix improper handling of NULL dialog filter on Cocoa

---
 src/dialog/cocoa/SDL_cocoadialog.m | 90 ++++++++++++++++--------------
 1 file changed, 47 insertions(+), 43 deletions(-)

diff --git a/src/dialog/cocoa/SDL_cocoadialog.m b/src/dialog/cocoa/SDL_cocoadialog.m
index be596cecc8e47..1ca58fafa65bb 100644
--- a/src/dialog/cocoa/SDL_cocoadialog.m
+++ b/src/dialog/cocoa/SDL_cocoadialog.m
@@ -37,12 +37,14 @@ void show_file_dialog(cocoa_FileDialogType type, SDL_DialogFileCallback callback
     SDL_SetError("tvOS and iOS don't support path-based file dialogs");
     callback(userdata, NULL, -1);
 #else
-    const char *msg = validate_filters(filters);
+    if (filters) {
+        const char *msg = validate_filters(filters);
 
-    if (msg) {
-        SDL_SetError("%s", msg);
-        callback(userdata, NULL, -1);
-        return;
+        if (msg) {
+            SDL_SetError("%s", msg);
+            callback(userdata, NULL, -1);
+            return;
+        }
     }
 
     if (SDL_GetHint(SDL_HINT_FILE_DIALOG_DRIVER) != NULL) {
@@ -73,49 +75,51 @@ void show_file_dialog(cocoa_FileDialogType type, SDL_DialogFileCallback callback
         break;
     };
 
-    int n = -1;
-    while (filters[++n].name && filters[n].pattern);
-    // On macOS 11.0 and up, this is an array of UTType. Prior to that, it's an array of NSString
-    NSMutableArray *types = [[NSMutableArray alloc] initWithCapacity:n ];
-
-    int has_all_files = 0;
-    for (int i = 0; i < n; i++) {
-        char *pattern = SDL_strdup(filters[i].pattern);
-        char *pattern_ptr = pattern;
-
-        if (!pattern_ptr) {
-            SDL_OutOfMemory();
-            callback(userdata, NULL, -1);
-            return;
-        }
+    if (filters) {
+        int n = -1;
+        while (filters[++n].name && filters[n].pattern);
+        // On macOS 11.0 and up, this is an array of UTType. Prior to that, it's an array of NSString
+        NSMutableArray *types = [[NSMutableArray alloc] initWithCapacity:n ];
+
+        int has_all_files = 0;
+        for (int i = 0; i < n; i++) {
+            char *pattern = SDL_strdup(filters[i].pattern);
+            char *pattern_ptr = pattern;
+
+            if (!pattern_ptr) {
+                SDL_OutOfMemory();
+                callback(userdata, NULL, -1);
+                return;
+            }
 
-        for (char *c = pattern; *c; c++) {
-            if (*c == ';') {
-                *c = '\0';
-                if(@available(macOS 11.0, *)) {
-                    [types addObject: [UTType typeWithFilenameExtension:[NSString stringWithFormat: @"%s", pattern_ptr]]];
-                } else {
-                    [types addObject: [NSString stringWithFormat: @"%s", pattern_ptr]];
+            for (char *c = pattern; *c; c++) {
+                if (*c == ';') {
+                    *c = '\0';
+                    if(@available(macOS 11.0, *)) {
+                        [types addObject: [UTType typeWithFilenameExtension:[NSString stringWithFormat: @"%s", pattern_ptr]]];
+                    } else {
+                        [types addObject: [NSString stringWithFormat: @"%s", pattern_ptr]];
+                    }
+                    pattern_ptr = c + 1;
+                } else if (*c == '*') {
+                    has_all_files = 1;
                 }
-                pattern_ptr = c + 1;
-            } else if (*c == '*') {
-                has_all_files = 1;
             }
-        }
-        if(@available(macOS 11.0, *)) {
-            [types addObject: [UTType typeWithFilenameExtension:[NSString stringWithFormat: @"%s", pattern_ptr]]];
-        } else {
-            [types addObject: [NSString stringWithFormat: @"%s", pattern_ptr]];
-        }
+            if(@available(macOS 11.0, *)) {
+                [types addObject: [UTType typeWithFilenameExtension:[NSString stringWithFormat: @"%s", pattern_ptr]]];
+            } else {
+                [types addObject: [NSString stringWithFormat: @"%s", pattern_ptr]];
+            }
 
-        SDL_free(pattern);
-    }
+            SDL_free(pattern);
+        }
 
-    if (!has_all_files) {
-        if (@available(macOS 11.0, *)) {
-            [dialog setAllowedContentTypes:types];
-        } else {
-            [dialog setAllowedFileTypes:types];
+        if (!has_all_files) {
+            if (@available(macOS 11.0, *)) {
+                [dialog setAllowedContentTypes:types];
+            } else {
+                [dialog setAllowedFileTypes:types];
+            }
         }
     }