Skip to content

Commit aa81cba

Browse files
committed
main: Ensure SDL_RunApp() calls mainFunction() with a non-null argv
If the specified argv is NULL, `SDL_RunApp()` will now use a dummy argv instead, on all platforms.
1 parent 210b317 commit aa81cba

File tree

13 files changed

+85
-30
lines changed

13 files changed

+85
-30
lines changed

VisualC-GDK/SDL/SDL.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,7 @@
478478
<ClInclude Include="..\..\src\libm\math_private.h" />
479479
<ClInclude Include="..\..\src\locale\SDL_syslocale.h" />
480480
<ClInclude Include="..\..\src\main\SDL_main_callbacks.h" />
481+
<ClInclude Include="..\..\src\main\SDL_runapp.h" />
481482
<ClInclude Include="..\..\src\misc\SDL_sysurl.h" />
482483
<ClInclude Include="..\..\src\power\SDL_syspower.h" />
483484
<ClInclude Include="..\..\src\render\direct3d11\SDL_shaders_d3d11.h" />

VisualC-GDK/SDL/SDL.vcxproj.filters

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@
370370
<ClInclude Include="..\..\src\libm\math_private.h" />
371371
<ClInclude Include="..\..\src\locale\SDL_syslocale.h" />
372372
<ClInclude Include="..\..\src\main\SDL_main_callbacks.h" />
373+
<ClInclude Include="..\..\src\main\SDL_runapp.h" />
373374
<ClInclude Include="..\..\src\misc\SDL_sysurl.h" />
374375
<ClInclude Include="..\..\src\power\SDL_syspower.h" />
375376
<ClInclude Include="..\..\src\render\direct3d11\SDL_shaders_d3d11.h" />

VisualC/SDL/SDL.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@
391391
<ClInclude Include="..\..\src\libm\math_private.h" />
392392
<ClInclude Include="..\..\src\locale\SDL_syslocale.h" />
393393
<ClInclude Include="..\..\src\main\SDL_main_callbacks.h" />
394+
<ClInclude Include="..\..\src\main\SDL_runapp.h" />
394395
<ClInclude Include="..\..\src\misc\SDL_sysurl.h" />
395396
<ClInclude Include="..\..\src\power\SDL_syspower.h" />
396397
<ClInclude Include="..\..\src\render\direct3d11\SDL_shaders_d3d11.h" />

VisualC/SDL/SDL.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,9 @@
459459
<ClInclude Include="..\..\src\main\SDL_main_callbacks.h">
460460
<Filter>main</Filter>
461461
</ClInclude>
462+
<ClInclude Include="..\..\src\main\SDL_runapp.h">
463+
<Filter>main</Filter>
464+
</ClInclude>
462465
<ClInclude Include="..\..\src\SDL_error_c.h" />
463466
<ClInclude Include="..\..\src\SDL_hashtable.h" />
464467
<ClInclude Include="..\..\src\SDL_list.h" />

src/main/SDL_runapp.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
3. This notice may not be removed or altered from any source distribution.
2020
*/
2121
#include "SDL_internal.h"
22+
#include "SDL_runapp.h"
2223

2324
/* Most platforms that use/need SDL_main have their own SDL_RunApp() implementation.
2425
* If not, you can special case it here by appending || defined(__YOUR_PLATFORM__) */
@@ -28,16 +29,20 @@ int SDL_RunApp(int argc, char* argv[], SDL_main_func mainFunction, void * reserv
2829
{
2930
(void)reserved;
3031

31-
if(!argv)
32-
{
33-
// make sure argv isn't NULL, in case some user code doesn't like that
34-
static char dummyargv0[] = { 'S', 'D', 'L', '_', 'a', 'p', 'p', '\0' };
35-
static char* argvdummy[2] = { dummyargv0, NULL };
32+
return SDL_CallMain(argc, argv, mainFunction);
33+
}
34+
35+
#endif
36+
37+
int SDL_CallMain(int argc, char* argv[], SDL_main_func mainFunction)
38+
{
39+
char dummyargv0[] = { 'S', 'D', 'L', '_', 'a', 'p', 'p', '\0' };
40+
char *dummyargv[2] = { dummyargv0, NULL };
41+
42+
if (!argv || argc < 0) {
3643
argc = 1;
37-
argv = argvdummy;
44+
argv = dummyargv;
3845
}
3946

4047
return mainFunction(argc, argv);
4148
}
42-
43-
#endif

src/main/SDL_runapp.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
Simple DirectMedia Layer
3+
Copyright (C) 1997-2025 Sam Lantinga <[email protected]>
4+
5+
This software is provided 'as-is', without any express or implied
6+
warranty. In no event will the authors be held liable for any damages
7+
arising from the use of this software.
8+
9+
Permission is granted to anyone to use this software for any purpose,
10+
including commercial applications, and to alter it and redistribute it
11+
freely, subject to the following restrictions:
12+
13+
1. The origin of this software must not be misrepresented; you must not
14+
claim that you wrote the original software. If you use this software
15+
in a product, an acknowledgment in the product documentation would be
16+
appreciated but is not required.
17+
2. Altered source versions must be plainly marked as such, and must not be
18+
misrepresented as being the original software.
19+
3. This notice may not be removed or altered from any source distribution.
20+
*/
21+
22+
#ifndef SDL_runapp_h_
23+
#define SDL_runapp_h_
24+
25+
int SDL_CallMain(int argc, char* argv[], SDL_main_func mainFunction);
26+
27+
#endif // SDL_runapp_h_

src/main/emscripten/SDL_sysmain_runapp.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
#include <emscripten/emscripten.h>
2626

27+
#include "../SDL_runapp.h"
28+
2729
EM_JS_DEPS(sdlrunapp, "$dynCall,$stringToNewUTF8");
2830

2931
int SDL_RunApp(int argc, char* argv[], SDL_main_func mainFunction, void * reserved)
@@ -50,7 +52,7 @@ int SDL_RunApp(int argc, char* argv[], SDL_main_func mainFunction, void * reserv
5052
}
5153
}, SDL_setenv_unsafe);
5254

53-
return mainFunction(argc, argv);
55+
return SDL_CallMain(argc, argv, mainFunction);
5456
}
5557

5658
#endif

src/main/gdk/SDL_sysmain_runapp.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "SDL_internal.h"
2222

2323
extern "C" {
24+
#include "../SDL_runapp.h"
2425
#include "../../core/gdk/SDL_gdk.h"
2526
#include "../../core/windows/SDL_windows.h"
2627
#include "../../events/SDL_events_c.h"
@@ -40,7 +41,7 @@ static BOOL OutOfMemory(void)
4041
/* Gets the arguments with GetCommandLine, converts them to argc and argv
4142
and calls SDL_main */
4243
extern "C"
43-
int SDL_RunApp(int, char**, SDL_main_func mainFunction, void *reserved)
44+
int SDL_RunApp(int, char**, SDL_main_func mainFunction, void *)
4445
{
4546
LPWSTR *argvw;
4647
char **argv;
@@ -111,7 +112,7 @@ int SDL_RunApp(int, char**, SDL_main_func mainFunction, void *reserved)
111112
}
112113

113114
// Run the application main() code
114-
result = mainFunction(argc, argv);
115+
result = SDL_CallMain(argc, argv, mainFunction);
115116

116117
GDK_UnregisterChangeNotifications();
117118

src/main/n3ds/SDL_sysmain_runapp.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,24 @@
2525

2626
#include <3ds.h>
2727

28+
#include "../SDL_runapp.h"
29+
2830
int SDL_RunApp(int argc, char* argv[], SDL_main_func mainFunction, void * reserved)
2931
{
3032
int result;
33+
(void)reserved;
34+
3135
// init
3236
osSetSpeedupEnable(true);
3337
romfsInit();
3438

3539
SDL_SetMainReady();
36-
result = mainFunction(argc, argv);
40+
result = SDL_CallMain(argc, argv, mainFunction);
3741

3842
// quit
3943
romfsExit();
4044

4145
return result;
4246
}
4347

44-
#ifdef __cplusplus
45-
} // extern "C"
46-
#endif
47-
4848
#endif

src/main/ps2/SDL_sysmain_runapp.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
#include <sbv_patches.h>
3737
#include <ps2_filesystem_driver.h>
3838

39+
#include "../SDL_runapp.h"
40+
3941
__attribute__((weak)) void reset_IOP(void)
4042
{
4143
SifInitRpc(0);
@@ -74,7 +76,7 @@ int SDL_RunApp(int argc, char* argv[], SDL_main_func mainFunction, void * reserv
7476

7577
SDL_SetMainReady();
7678

77-
res = mainFunction(argc, argv);
79+
res = SDL_CallMain(argc, argv, mainFunction);
7880

7981
deinit_drivers();
8082

0 commit comments

Comments
 (0)