From 2c2c923b68ceca7d2c9dee994aba61941bb5e139 Mon Sep 17 00:00:00 2001 From: Corentin Recanzone Date: Fri, 12 Sep 2025 20:17:11 +0200 Subject: [PATCH] Fix Android TitleStorage: avoid leading '/' and handle NULL base path --- src/storage/generic/SDL_genericstorage.c | 31 ++++++++++++++++++++---- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/src/storage/generic/SDL_genericstorage.c b/src/storage/generic/SDL_genericstorage.c index cc0804feacab7..9fd41ffddadef 100644 --- a/src/storage/generic/SDL_genericstorage.c +++ b/src/storage/generic/SDL_genericstorage.c @@ -26,8 +26,19 @@ static char *GENERIC_INTERNAL_CreateFullPath(const char *base, const char *relative) { + const char *rel = relative; + +#ifdef SDL_PLATFORM_ANDROID + if (rel) { + // Removes any leading slash + if (rel[0] == '/' || rel[0] == '\\') { + rel += 1; + } + } +#endif + char *result = NULL; - SDL_asprintf(&result, "%s%s", base ? base : "", relative); + SDL_asprintf(&result, "%s%s", base ? base : "", rel ? rel : ""); return result; } @@ -239,15 +250,25 @@ static SDL_Storage *GENERIC_Title_Create(const char *override, SDL_PropertiesID char *basepath = NULL; if (override != NULL) { - // make sure override has a path separator at the end. If you're not on Windows and used '\\', that's on you. const size_t slen = SDL_strlen(override); - const bool need_sep = (!slen || ((override[slen-1] != '/') && (override[slen-1] != '\\'))); - if (SDL_asprintf(&basepath, "%s%s", override, need_sep ? "/" : "") == -1) { - return NULL; + if (slen > 0) { + // make sure override has a path separator at the end. If you're not on Windows and used '\\', that's on you. + const bool need_sep = ((override[slen - 1] != '/') && (override[slen - 1] != '\\')); + if (SDL_asprintf(&basepath, "%s%s", override, need_sep ? "/" : "") == -1) { + return NULL; + } + } else { + // override == "" -> empty base (not "/") + basepath = SDL_strdup(""); } } else { const char *base = SDL_GetBasePath(); + // On Android, SDL_GetBasePath() can be NULL: use empty base. +#ifdef SDL_PLATFORM_ANDROID + basepath = base ? SDL_strdup(base) : SDL_strdup(""); +#else basepath = base ? SDL_strdup(base) : NULL; +#endif } if (basepath != NULL) {