[Commits] SDL: Fixed bug 3639 - SDL_GetPrefPath returns a path with two co...

libsdl.org revision control commits-owner at libsdl.org
Fri Aug 11 11:32:03 PDT 2017


details:   https://hg.libsdl.org/SDL/rev/083133ba8f9b
changeset: 11222:083133ba8f9b
user:      Sam Lantinga <slouken at libsdl.org>
date:      Fri Aug 11 11:32:00 2017 -0700
description:
Fixed bug 3639 - SDL_GetPrefPath returns a path with two consecutive slashes on Unix if org is omitted

Fabian Greffrath

we use SDL_GetPrefPath() in Chocolate Doom to get a reasonable directory to save and restore config files and savegames:

https://github.com/chocolate-doom/chocolate-doom/blob/sdl2-branch/src/m_config.c#L2162

However, since there is no "organization" behind Chocolate Doom and there is really only one "product" called Chocolate Doom, we pass an empty string for the org parameter and the package string for app.

This leads to two consecutive slashes in the path returned by SDL_GetPrefPath() like this:

/home/user/.local/share//chocolate-doom/

While this is harmless, it sure looks bad.

I believe that it should be possible to either pass a NULL pointer for the org parameter or at least have the function detect an empty string as a means to express "there is no origanization, just a single product". The generation of the path string to be returned by the function will have to get adapted accordingly.

diffstat:

 src/filesystem/cocoa/SDL_sysfilesystem.m      |  15 +++++++++++++--
 src/filesystem/emscripten/SDL_sysfilesystem.c |  14 +++++++++++++-
 src/filesystem/haiku/SDL_sysfilesystem.cc     |  15 ++++++++++++++-
 src/filesystem/nacl/SDL_sysfilesystem.c       |   1 +
 src/filesystem/unix/SDL_sysfilesystem.c       |  14 +++++++++++++-
 src/filesystem/windows/SDL_sysfilesystem.c    |  14 ++++++++++++--
 src/filesystem/winrt/SDL_sysfilesystem.cpp    |  18 +++++++++++++++---
 test/testfilesystem.c                         |   9 +++++++++
 8 files changed, 90 insertions(+), 10 deletions(-)

diffs (224 lines):

diff -r 4753f2320927 -r 083133ba8f9b src/filesystem/cocoa/SDL_sysfilesystem.m
--- a/src/filesystem/cocoa/SDL_sysfilesystem.m	Fri Aug 11 10:42:26 2017 -0700
+++ b/src/filesystem/cocoa/SDL_sysfilesystem.m	Fri Aug 11 11:32:00 2017 -0700
@@ -71,8 +71,15 @@
 SDL_GetPrefPath(const char *org, const char *app)
 { @autoreleasepool
 {
+    if (!app) {
+        SDL_InvalidParamError("app");
+        return NULL;
+    }
+    if (!org) {
+        org = "";
+    }
+
     char *retval = NULL;
-
     NSArray *array = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
 
     if ([array count] > 0) {  /* we only want the first item in the list. */
@@ -85,7 +92,11 @@
                 SDL_OutOfMemory();
             } else {
                 char *ptr;
-                SDL_snprintf(retval, len, "%s/%s/%s/", base, org, app);
+                if (*org) {
+                    SDL_snprintf(retval, len, "%s/%s/%s/", base, org, app);
+                } else {
+                    SDL_snprintf(retval, len, "%s/%s/", base, app);
+                }
                 for (ptr = retval+1; *ptr; ptr++) {
                     if (*ptr == '/') {
                         *ptr = '\0';
diff -r 4753f2320927 -r 083133ba8f9b src/filesystem/emscripten/SDL_sysfilesystem.c
--- a/src/filesystem/emscripten/SDL_sysfilesystem.c	Fri Aug 11 10:42:26 2017 -0700
+++ b/src/filesystem/emscripten/SDL_sysfilesystem.c	Fri Aug 11 11:32:00 2017 -0700
@@ -46,6 +46,14 @@
     char *retval;
     size_t len = 0;
 
+    if (!app) {
+        SDL_InvalidParamError("app");
+        return NULL;
+    }
+    if (!org) {
+        org = "";
+    }
+
     len = SDL_strlen(append) + SDL_strlen(org) + SDL_strlen(app) + 3;
     retval = (char *) SDL_malloc(len);
     if (!retval) {
@@ -53,7 +61,11 @@
         return NULL;
     }
 
-    SDL_snprintf(retval, len, "%s%s/%s/", append, org, app);
+    if (*org) {
+        SDL_snprintf(retval, len, "%s%s/%s/", append, org, app);
+    } else {
+        SDL_snprintf(retval, len, "%s%s/", append, app);
+    }
 
     if (mkdir(retval, 0700) != 0 && errno != EEXIST) {
         SDL_SetError("Couldn't create directory '%s': '%s'", retval, strerror(errno));
diff -r 4753f2320927 -r 083133ba8f9b src/filesystem/haiku/SDL_sysfilesystem.cc
--- a/src/filesystem/haiku/SDL_sysfilesystem.cc	Fri Aug 11 10:42:26 2017 -0700
+++ b/src/filesystem/haiku/SDL_sysfilesystem.cc	Fri Aug 11 11:32:00 2017 -0700
@@ -77,6 +77,15 @@
     const char *home = SDL_getenv("HOME");
     const char *append = "/config/settings/";
     size_t len = SDL_strlen(home);
+
+    if (!app) {
+        SDL_InvalidParamError("app");
+        return NULL;
+    }
+    if (!org) {
+        org = "";
+    }
+
     if (!len || (home[len - 1] == '/')) {
         ++append; // home empty or ends with separator, skip the one from append
     }
@@ -85,7 +94,11 @@
     if (!retval) {
         SDL_OutOfMemory();
     } else {
-        SDL_snprintf(retval, len, "%s%s%s/%s/", home, append, org, app);
+        if (*org) {
+            SDL_snprintf(retval, len, "%s%s%s/%s/", home, append, org, app);
+        } else {
+            SDL_snprintf(retval, len, "%s%s%s/", home, append, app);
+        }
         create_directory(retval, 0700);  // Haiku api: creates missing dirs
     }
 
diff -r 4753f2320927 -r 083133ba8f9b src/filesystem/nacl/SDL_sysfilesystem.c
--- a/src/filesystem/nacl/SDL_sysfilesystem.c	Fri Aug 11 10:42:26 2017 -0700
+++ b/src/filesystem/nacl/SDL_sysfilesystem.c	Fri Aug 11 11:32:00 2017 -0700
@@ -40,3 +40,4 @@
 
 #endif /* SDL_FILESYSTEM_NACL */
 
+/* vi: set ts=4 sw=4 expandtab: */
diff -r 4753f2320927 -r 083133ba8f9b src/filesystem/unix/SDL_sysfilesystem.c
--- a/src/filesystem/unix/SDL_sysfilesystem.c	Fri Aug 11 10:42:26 2017 -0700
+++ b/src/filesystem/unix/SDL_sysfilesystem.c	Fri Aug 11 11:32:00 2017 -0700
@@ -180,6 +180,14 @@
     char *ptr = NULL;
     size_t len = 0;
 
+    if (!app) {
+        SDL_InvalidParamError("app");
+        return NULL;
+    }
+    if (!org) {
+        org = "";
+    }
+
     if (!envr) {
         /* You end up with "$HOME/.local/share/Game Name 2" */
         envr = SDL_getenv("HOME");
@@ -204,7 +212,11 @@
         return NULL;
     }
 
-    SDL_snprintf(retval, len, "%s%s%s/%s/", envr, append, org, app);
+    if (*org) {
+        SDL_snprintf(retval, len, "%s%s%s/%s/", envr, append, org, app);
+    } else {
+        SDL_snprintf(retval, len, "%s%s%s/", envr, append, app);
+    }
 
     for (ptr = retval+1; *ptr; ptr++) {
         if (*ptr == '/') {
diff -r 4753f2320927 -r 083133ba8f9b src/filesystem/windows/SDL_sysfilesystem.c
--- a/src/filesystem/windows/SDL_sysfilesystem.c	Fri Aug 11 10:42:26 2017 -0700
+++ b/src/filesystem/windows/SDL_sysfilesystem.c	Fri Aug 11 11:32:00 2017 -0700
@@ -118,6 +118,14 @@
     size_t new_wpath_len = 0;
     BOOL api_result = FALSE;
 
+    if (!app) {
+        SDL_InvalidParamError("app");
+        return NULL;
+    }
+    if (!org) {
+        org = "";
+    }
+
     if (!SUCCEEDED(SHGetFolderPathW(NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, 0, path))) {
         WIN_SetError("Couldn't locate our prefpath");
         return NULL;
@@ -145,8 +153,10 @@
         return NULL;
     }
 
-    lstrcatW(path, L"\\");
-    lstrcatW(path, worg);
+    if (*worg) {
+        lstrcatW(path, L"\\");
+        lstrcatW(path, worg);
+    }
     SDL_free(worg);
 
     api_result = CreateDirectoryW(path, NULL);
diff -r 4753f2320927 -r 083133ba8f9b src/filesystem/winrt/SDL_sysfilesystem.cpp
--- a/src/filesystem/winrt/SDL_sysfilesystem.cpp	Fri Aug 11 10:42:26 2017 -0700
+++ b/src/filesystem/winrt/SDL_sysfilesystem.cpp	Fri Aug 11 11:32:00 2017 -0700
@@ -152,6 +152,14 @@
     size_t new_wpath_len = 0;
     BOOL api_result = FALSE;
 
+    if (!app) {
+        SDL_InvalidParamError("app");
+        return NULL;
+    }
+    if (!org) {
+        org = "";
+    }
+
     srcPath = SDL_WinRTGetFSPathUNICODE(SDL_WINRT_PATH_LOCAL_FOLDER);
     if ( ! srcPath) {
         SDL_SetError("Unable to find a source path");
@@ -186,9 +194,11 @@
         return NULL;
     }
 
-    SDL_wcslcat(path, L"\\", new_wpath_len + 1);
-    SDL_wcslcat(path, worg, new_wpath_len + 1);
-    SDL_free(worg);
+    if (*worg) {
+        SDL_wcslcat(path, L"\\", new_wpath_len + 1);
+        SDL_wcslcat(path, worg, new_wpath_len + 1);
+        SDL_free(worg);
+    }
 
     api_result = CreateDirectoryW(path, NULL);
     if (api_result == FALSE) {
@@ -219,3 +229,5 @@
 }
 
 #endif /* __WINRT__ */
+
+/* vi: set ts=4 sw=4 expandtab: */
diff -r 4753f2320927 -r 083133ba8f9b test/testfilesystem.c
--- a/test/testfilesystem.c	Fri Aug 11 10:42:26 2017 -0700
+++ b/test/testfilesystem.c	Fri Aug 11 11:32:00 2017 -0700
@@ -46,6 +46,15 @@
     SDL_Log("pref path: '%s'\n", pref_path); 
     SDL_free(pref_path);
 
+    pref_path = SDL_GetPrefPath(NULL, "testfilesystem");
+    if(pref_path == NULL){
+      SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't find pref path without organization: %s\n",
+                   SDL_GetError());
+      return 1;
+    }
+    SDL_Log("pref path: '%s'\n", pref_path); 
+    SDL_free(pref_path);
+
     SDL_Quit();
     return 0;
 }


More information about the commits mailing list