diff --git a/src/dialog/cocoa/SDL_cocoadialog.m b/src/dialog/cocoa/SDL_cocoadialog.m index be596cecc8..1ca58fafa6 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 ]; + 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; + 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]]; - } - pattern_ptr = c + 1; - } else if (*c == '*') { - has_all_files = 1; + if (!pattern_ptr) { + SDL_OutOfMemory(); + callback(userdata, NULL, -1); + return; } - } - 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; + } + } + 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]; + } } }