Skip to content

Commit af1c05f

Browse files
committed
filesystem: Check SDL_GetPrefPath parameters at the higher level.
...so the backends don't have to do it. Also added a stern warning about `org` being omitted, but leaving it as allowed so as not to break existing apps (more than they are already broken, at least). Fixes #13322.
1 parent 279a50c commit af1c05f

File tree

13 files changed

+22
-102
lines changed

13 files changed

+22
-102
lines changed

include/SDL3/SDL_filesystem.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,12 @@ extern SDL_DECLSPEC const char * SDLCALL SDL_GetBasePath(void);
134134
* - ...only use letters, numbers, and spaces. Avoid punctuation like "Game
135135
* Name 2: Bad Guy's Revenge!" ... "Game Name 2" is sufficient.
136136
*
137+
* Due to historical mistakes, `org` is allowed to be NULL or "". In such
138+
* cases, SDL will omit the org subdirectory, including on platforms where it
139+
* shouldn't, and including on platforms where this would make your app fail
140+
* certification for an app store. New apps should definitely specify a real
141+
* string for `org`.
142+
*
137143
* The returned path is guaranteed to end with a path separator ('\\' on
138144
* Windows, '/' on most other platforms).
139145
*

src/filesystem/SDL_filesystem.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,16 @@ const char *SDL_GetUserFolder(SDL_Folder folder)
502502

503503
char *SDL_GetPrefPath(const char *org, const char *app)
504504
{
505+
if (!app) {
506+
SDL_InvalidParamError("app");
507+
return NULL;
508+
}
509+
510+
// if org is NULL, just make it "" so backends don't have to check both.
511+
if (!org) {
512+
org = "";
513+
}
514+
505515
return SDL_SYS_GetPrefPath(org, app);
506516
}
507517

src/filesystem/cocoa/SDL_sysfilesystem.m

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,6 @@
6969
char *result = NULL;
7070
NSArray *array;
7171

72-
if (!app) {
73-
SDL_InvalidParamError("app");
74-
return NULL;
75-
}
76-
if (!org) {
77-
org = "";
78-
}
79-
8072
#ifndef SDL_PLATFORM_TVOS
8173
array = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
8274
#else
@@ -106,13 +98,12 @@
10698
const size_t len = SDL_strlen(base) + SDL_strlen(org) + SDL_strlen(app) + 4;
10799
result = (char *)SDL_malloc(len);
108100
if (result != NULL) {
109-
char *ptr;
110101
if (*org) {
111102
SDL_snprintf(result, len, "%s/%s/%s/", base, org, app);
112103
} else {
113104
SDL_snprintf(result, len, "%s/%s/", base, app);
114105
}
115-
for (ptr = result + 1; *ptr; ptr++) {
106+
for (char *ptr = result + 1; *ptr; ptr++) {
116107
if (*ptr == '/') {
117108
*ptr = '\0';
118109
mkdir(result, 0700);

src/filesystem/emscripten/SDL_sysfilesystem.c

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,7 @@ char *SDL_SYS_GetPrefPath(const char *org, const char *app)
4242
const char *append = "/libsdl/";
4343
char *result;
4444
char *ptr = NULL;
45-
size_t len = 0;
46-
47-
if (!app) {
48-
SDL_InvalidParamError("app");
49-
return NULL;
50-
}
51-
if (!org) {
52-
org = "";
53-
}
54-
55-
len = SDL_strlen(append) + SDL_strlen(org) + SDL_strlen(app) + 3;
45+
const size_t len = SDL_strlen(append) + SDL_strlen(org) + SDL_strlen(app) + 3;
5646
result = (char *)SDL_malloc(len);
5747
if (!result) {
5848
return NULL;

src/filesystem/gdk/SDL_sysfilesystem.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,6 @@ char *SDL_SYS_GetPrefPath(const char *org, const char *app)
9090
HRESULT result;
9191
const char *csid = SDL_GetHint("SDL_GDK_SERVICE_CONFIGURATION_ID");
9292

93-
if (!app) {
94-
SDL_InvalidParamError("app");
95-
return NULL;
96-
}
97-
9893
// This should be set before calling SDL_GetPrefPath!
9994
if (!csid) {
10095
SDL_LogWarn(SDL_LOG_CATEGORY_SYSTEM, "Set SDL_GDK_SERVICE_CONFIGURATION_ID before calling SDL_GetPrefPath!");

src/filesystem/haiku/SDL_sysfilesystem.cc

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,6 @@ char *SDL_SYS_GetPrefPath(const char *org, const char *app)
7272
const char *append = "/config/settings/";
7373
size_t len = SDL_strlen(home);
7474

75-
if (!app) {
76-
SDL_InvalidParamError("app");
77-
return NULL;
78-
}
79-
if (!org) {
80-
org = "";
81-
}
82-
8375
if (!len || (home[len - 1] == '/')) {
8476
++append; // home empty or ends with separator, skip the one from append
8577
}

src/filesystem/n3ds/SDL_sysfilesystem.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,6 @@ char *SDL_SYS_GetBasePath(void)
4343
char *SDL_SYS_GetPrefPath(const char *org, const char *app)
4444
{
4545
char *pref_path = NULL;
46-
if (!app) {
47-
SDL_InvalidParamError("app");
48-
return NULL;
49-
}
50-
5146
pref_path = MakePrefPath(app);
5247
if (!pref_path) {
5348
return NULL;

src/filesystem/ps2/SDL_sysfilesystem.c

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,6 @@ char *SDL_SYS_GetPrefPath(const char *org, const char *app)
8080
char *result = NULL;
8181
size_t len;
8282

83-
if (!app) {
84-
SDL_InvalidParamError("app");
85-
return NULL;
86-
}
87-
88-
if (!org) {
89-
org = "";
90-
}
91-
9283
const char *base = SDL_GetBasePath();
9384
if (!base) {
9485
return NULL;
@@ -102,7 +93,6 @@ char *SDL_SYS_GetPrefPath(const char *org, const char *app)
10293
} else {
10394
SDL_snprintf(result, len, "%s%s/", base, app);
10495
}
105-
10696
recursive_mkdir(result);
10797
}
10898

src/filesystem/psp/SDL_sysfilesystem.c

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,30 +49,19 @@ char *SDL_SYS_GetBasePath(void)
4949
char *SDL_SYS_GetPrefPath(const char *org, const char *app)
5050
{
5151
char *result = NULL;
52-
size_t len;
53-
if (!app) {
54-
SDL_InvalidParamError("app");
55-
return NULL;
56-
}
57-
5852
const char *base = SDL_GetBasePath();
5953
if (!base) {
6054
return NULL;
6155
}
6256

63-
if (!org) {
64-
org = "";
65-
}
66-
67-
len = SDL_strlen(base) + SDL_strlen(org) + SDL_strlen(app) + 4;
57+
const size_t len = SDL_strlen(base) + SDL_strlen(org) + SDL_strlen(app) + 4;
6858
result = (char *)SDL_malloc(len);
6959
if (result) {
7060
if (*org) {
7161
SDL_snprintf(result, len, "%s%s/%s/", base, org, app);
7262
} else {
7363
SDL_snprintf(result, len, "%s%s/", base, app);
7464
}
75-
7665
mkdir(result, 0755);
7766
}
7867

src/filesystem/riscos/SDL_sysfilesystem.c

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -155,23 +155,14 @@ char *SDL_SYS_GetBasePath(void)
155155
char *SDL_SYS_GetPrefPath(const char *org, const char *app)
156156
{
157157
char *canon, *dir, *result;
158-
size_t len;
159158
_kernel_oserror *error;
160159

161-
if (!app) {
162-
SDL_InvalidParamError("app");
163-
return NULL;
164-
}
165-
if (!org) {
166-
org = "";
167-
}
168-
169160
canon = canonicalisePath("<Choices$Write>", "Run$Path");
170161
if (!canon) {
171162
return NULL;
172163
}
173164

174-
len = SDL_strlen(canon) + SDL_strlen(org) + SDL_strlen(app) + 4;
165+
const size_t len = SDL_strlen(canon) + SDL_strlen(org) + SDL_strlen(app) + 4;
175166
dir = (char *)SDL_malloc(len);
176167
if (!dir) {
177168
SDL_free(canon);

0 commit comments

Comments
 (0)