Repository: libsdl-org/SDL
File: src/dialog/unix/SDL_portaldialog.c (C)

Code before:
    const char *default_location = SDL_GetStringProperty(props, SDL_PROP_FILE_DIALOG_LOCATION_STRING, NULL);
    const char *accept = SDL_GetStringProperty(props, SDL_PROP_FILE_DIALOG_ACCEPT_STRING, NULL);
    char *location_name = NULL;
    char *location_folder = NULL;
    struct stat statbuf;
    bool open_folders = false;
    bool save_file_existing = false;
    bool save_file_new_named = false;

    switch (type) {
    case SDL_FILEDIALOG_OPENFILE:
        method = "OpenFile";
        method_title = SDL_GetStringProperty(props, SDL_PROP_FILE_DIALOG_TITLE_STRING, "Open File");
        break;

    case SDL_FILEDIALOG_SAVEFILE:
        method = "SaveFile";
        method_title = SDL_GetStringProperty(props, SDL_PROP_FILE_DIALOG_TITLE_STRING, "Save File");
        if (default_location) {
            if (stat(default_location, &statbuf) == 0) {
                save_file_existing = S_ISREG(statbuf.st_mode) || S_ISLNK(statbuf.st_mode);
            } else if (errno == ENOENT) {
                char *dirc = SDL_strdup(default_location);
                if (dirc) {
                    location_folder = SDL_strdup(dirname(dirc));
                    SDL_free(dirc);
                    if (location_folder) {
                        save_file_new_named = (stat(location_folder, &statbuf) == 0) && S_ISDIR(statbuf.st_mode);
                    }
                }
            }

            if (save_file_existing || save_file_new_named) {
                char *basec = SDL_strdup(default_location);
                if (basec) {
                    location_name = SDL_strdup(basename(basec));
                    SDL_free(basec);
                }
            }
        }
        break;

    case SDL_FILEDIALOG_OPENFOLDER:
        method = "OpenFile";
        method_title = SDL_GetStringProperty(props, SDL_PROP_FILE_DIALOG_TITLE_STRING, "Open Folder");
        open_folders = true;
        break;

    default:
        /* This is already checked in ../SDL_dialog.c; this silences compiler warnings */
        SDL_SetError("Invalid file dialog type: %d", type);

Review comment (suggestion):
location_folder might be pointing at memory in dirc, which should be freed after location_folder is used.

Code after:
    const char *default_location = SDL_GetStringProperty(props, SDL_PROP_FILE_DIALOG_LOCATION_STRING, NULL);
    const char *accept = SDL_GetStringProperty(props, SDL_PROP_FILE_DIALOG_ACCEPT_STRING, NULL);
    char *location_name = NULL;
    char *location_folder = NULL;
    struct stat statbuf;
    bool open_folders = false;
    bool save_file_existing = false;
    bool save_file_new_named = false;

    switch (type) {
    case SDL_FILEDIALOG_OPENFILE:
        method = "OpenFile";
        method_title = SDL_GetStringProperty(props, SDL_PROP_FILE_DIALOG_TITLE_STRING, "Open File");
        break;

    case SDL_FILEDIALOG_SAVEFILE:
        method = "SaveFile";
        method_title = SDL_GetStringProperty(props, SDL_PROP_FILE_DIALOG_TITLE_STRING, "Save File");
        if (default_location) {
            if (stat(default_location, &statbuf) == 0) {
                save_file_existing = S_ISREG(statbuf.st_mode);
            } else if (errno == ENOENT) {
                char *dirc = SDL_strdup(default_location);
                if (dirc) {
                    location_folder = SDL_strdup(dirname(dirc));
                    SDL_free(dirc);
                    if (location_folder) {
                        save_file_new_named = (stat(location_folder, &statbuf) == 0) && S_ISDIR(statbuf.st_mode);
                    }
                }
            }

            if (save_file_existing || save_file_new_named) {
                char *basec = SDL_strdup(default_location);
                if (basec) {
                    location_name = SDL_strdup(basename(basec));
                    SDL_free(basec);
                }
            }
        }
        break;

    case SDL_FILEDIALOG_OPENFOLDER:
        method = "OpenFile";
        method_title = SDL_GetStringProperty(props, SDL_PROP_FILE_DIALOG_TITLE_STRING, "Open Folder");
        open_folders = true;
        break;

    default:
        /* This is already checked in ../SDL_dialog.c; this silences compiler warnings */
        SDL_SetError("Invalid file dialog type: %d", type);