winmain: Don't use SDL_malloc (or SDL_stack_alloc, which might be malloc). Otherwise, we are using the allocator before the app can set up its own hooks. Now we use VirtualAlloc, and WideCharToMultiByte (because SDL_iconv uses SDL_malloc, too!) to get ready to call into SDL_main. This also makes console_wmain() call into the same routines as everything else, so we don't have to deal with those allocations, too. Hopefully we end up with the same results from GetCommandLine() as we do in wargv. Fixes Bugzilla #4340.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
diff --git a/src/main/windows/SDL_windows_main.c b/src/main/windows/SDL_windows_main.c
index 5e643a4..0395032 100644
--- a/src/main/windows/SDL_windows_main.c
+++ b/src/main/windows/SDL_windows_main.c
@@ -116,50 +116,65 @@ OutOfMemory(void)
# endif
#endif
-/* WinMain, main, and wmain eventually call into here. */
-static int
-main_utf8(int argc, char *argv[])
-{
- SDL_SetMainReady();
-
- /* Run the application main() code */
- return SDL_main(argc, argv);
-}
-
/* Gets the arguments with GetCommandLine, converts them to argc and argv
- and calls main_utf8 */
+ and calls SDL_main */
static int
main_getcmdline()
{
char **argv;
int argc;
- char *cmdline;
+ char *cmdline = NULL;
int retval = 0;
+ int cmdalloc = 0;
+ const TCHAR *text = GetCommandLine();
+ const TCHAR *ptr;
+ int argc_guess = 2; /* space for NULL and initial argument. */
+ int rc;
+
+ /* make a rough guess of command line arguments. Overestimates if there
+ are quoted things. */
+ for (ptr = text; *ptr; ptr++) {
+ if ((*ptr == ' ') || (*ptr == '\t')) {
+ argc_guess++;
+ }
+ }
- /* Grab the command line */
- TCHAR *text = GetCommandLine();
#if UNICODE
- cmdline = WIN_StringToUTF8(text);
+ rc = WideCharToMultiByte(CP_UTF8, 0, text, -1, NULL, 0, NULL, NULL);
+ if (rc > 0) {
+ cmdalloc = rc + (sizeof (char *) * argc_guess);
+ argv = (char **) VirtualAlloc(NULL, cmdalloc, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE);
+ if (argv) {
+ cmdline = (char *) (argv + argc_guess);
+ const int rc2 = WideCharToMultiByte(CP_UTF8, 0, text, -1, cmdline, rc, NULL, NULL);
+ SDL_assert(rc2 == rc);
+ }
+ }
#else
/* !!! FIXME: are these in the system codepage? We need to convert to UTF-8. */
- cmdline = SDL_strdup(text);
+ rc = ((int) SDL_strlen(text)) + 1;
+ cmdalloc = rc + (sizeof (char *) * argc_guess);
+ argv = (char **) VirtualAlloc(NULL, cmdalloc, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE);
+ if (argv) {
+ cmdline = (char *) (argv + argc_guess);
+ SDL_strcpy(cmdline, text);
+ }
#endif
if (cmdline == NULL) {
return OutOfMemory();
}
/* Parse it into argv and argc */
- argc = ParseCommandLine(cmdline, NULL);
- argv = SDL_stack_alloc(char *, argc + 1);
- if (argv == NULL) {
- return OutOfMemory();
- }
- ParseCommandLine(cmdline, argv);
+ SDL_assert(ParseCommandLine(cmdline, NULL) <= argc_guess);
+ argc = ParseCommandLine(cmdline, argv);
- retval = main_utf8(argc, argv);
+ SDL_SetMainReady();
- SDL_stack_free(argv);
- SDL_free(cmdline);
+ /* Run the application main() code */
+ retval = SDL_main(argc, argv);
+
+ VirtualFree(argv, cmdalloc, MEM_DECOMMIT);
+ VirtualFree(argv, 0, MEM_RELEASE);
return retval;
}
@@ -177,21 +192,7 @@ console_ansi_main(int argc, char *argv[])
int
console_wmain(int argc, wchar_t *wargv[], wchar_t *wenvp)
{
- int retval = 0;
- char **argv = SDL_stack_alloc(char*, argc + 1);
- int i;
-
- for (i = 0; i < argc; ++i) {
- argv[i] = WIN_StringToUTF8(wargv[i]);
- }
- argv[argc] = NULL;
-
- retval = main_utf8(argc, argv);
-
- /* !!! FIXME: we are leaking all the elements of argv we allocated. */
- SDL_stack_free(argv);
-
- return retval;
+ return main_getcmdline();
}
#endif