src/main/win32/SDL_win32_main.c
author Sam Lantinga
Mon, 12 Jul 2010 22:08:50 -0700
changeset 4511 ae7799d16c87
parent 2286 41a6675d8700
permissions -rw-r--r--
Daniel Wyatt 2010-03-26 14:52:53 PDT

If a non-console Windows SDL program has a non-quoted 0th argument followed
optionally by more non-quoted arguments and then by an empty quoted argument,
it will crash (attempts to dereference a NULL pointer).

In other words, something like this:
test.exe [non-quoted args] "" [...]

The fix is a one-liner in ParseCommandLine() of
src/main/win32/SDL_win32_main.c.

You can test this with any non-console SDL program on windows like this:
1) Open a console (cmd.exe)
2) Launch the program in one of the following ways:
program ""
program arg1 ""
program arg1 "" arg3

These will not cause a crash:
"program" [...]
program "arg1" ""

When a Windows program is launched from Explorer, its 0th argument seems to
always be quoted, so it won't be a problem in that case.

I've tested this on Windows XP SP3 and Windows 7.
slouken@754
     1
/*
slouken@754
     2
    SDL_main.c, placed in the public domain by Sam Lantinga  4/13/98
slouken@754
     3
slouken@754
     4
    The WinMain function -- calls your program's main() function
slouken@754
     5
*/
slouken@754
     6
slouken@754
     7
#include <stdio.h>
slouken@754
     8
#include <stdlib.h>
slouken@754
     9
slouken@1433
    10
#define WIN32_LEAN_AND_MEAN
slouken@1433
    11
#include <windows.h>
slouken@1336
    12
slouken@754
    13
/* Include the SDL main definition header */
slouken@754
    14
#include "SDL.h"
slouken@754
    15
#include "SDL_main.h"
slouken@754
    16
slouken@754
    17
#ifdef main
slouken@754
    18
# ifndef _WIN32_WCE_EMULATION
slouken@754
    19
#  undef main
slouken@754
    20
# endif /* _WIN32_WCE_EMULATION */
slouken@754
    21
#endif /* main */
slouken@754
    22
slouken@754
    23
#if defined(_WIN32_WCE) && _WIN32_WCE < 300
slouken@754
    24
/* seems to be undefined in Win CE although in online help */
slouken@754
    25
#define isspace(a) (((CHAR)a == ' ') || ((CHAR)a == '\t'))
slouken@754
    26
#endif /* _WIN32_WCE < 300 */
slouken@754
    27
slouken@2286
    28
static void
slouken@2286
    29
UnEscapeQuotes(char *arg)
slouken@2286
    30
{
slouken@2286
    31
    char *last = NULL;
slouken@2286
    32
slouken@2286
    33
    while (*arg) {
slouken@2286
    34
        if (*arg == '"' && *last == '\\') {
slouken@2286
    35
            char *c_curr = arg;
slouken@2286
    36
            char *c_last = last;
slouken@2286
    37
slouken@2286
    38
            while (*c_curr) {
slouken@2286
    39
                *c_last = *c_curr;
slouken@2286
    40
                c_last = c_curr;
slouken@2286
    41
                c_curr++;
slouken@2286
    42
            }
slouken@2286
    43
            *c_last = '\0';
slouken@2286
    44
        }
slouken@2286
    45
        last = arg;
slouken@2286
    46
        arg++;
slouken@2286
    47
    }
slouken@2286
    48
}
slouken@2286
    49
slouken@754
    50
/* Parse a command line buffer into arguments */
slouken@1895
    51
static int
slouken@1895
    52
ParseCommandLine(char *cmdline, char **argv)
slouken@754
    53
{
slouken@1895
    54
    char *bufp;
slouken@2286
    55
    char *lastp = NULL;
slouken@2286
    56
    int argc, last_argc;
slouken@754
    57
slouken@2286
    58
    argc = last_argc = 0;
slouken@1895
    59
    for (bufp = cmdline; *bufp;) {
slouken@1895
    60
        /* Skip leading whitespace */
slouken@1895
    61
        while (isspace(*bufp)) {
slouken@1895
    62
            ++bufp;
slouken@1895
    63
        }
slouken@1895
    64
        /* Skip over argument */
slouken@1895
    65
        if (*bufp == '"') {
slouken@1895
    66
            ++bufp;
slouken@1895
    67
            if (*bufp) {
slouken@1895
    68
                if (argv) {
slouken@1895
    69
                    argv[argc] = bufp;
slouken@1895
    70
                }
slouken@1895
    71
                ++argc;
slouken@1895
    72
            }
slouken@1895
    73
            /* Skip over word */
slouken@4511
    74
            lastp = bufp;
slouken@2286
    75
            while (*bufp && (*bufp != '"' || *lastp == '\\')) {
slouken@2286
    76
                lastp = bufp;
slouken@1895
    77
                ++bufp;
slouken@1895
    78
            }
slouken@1895
    79
        } else {
slouken@1895
    80
            if (*bufp) {
slouken@1895
    81
                if (argv) {
slouken@1895
    82
                    argv[argc] = bufp;
slouken@1895
    83
                }
slouken@1895
    84
                ++argc;
slouken@1895
    85
            }
slouken@1895
    86
            /* Skip over word */
slouken@1895
    87
            while (*bufp && !isspace(*bufp)) {
slouken@1895
    88
                ++bufp;
slouken@1895
    89
            }
slouken@1895
    90
        }
slouken@1895
    91
        if (*bufp) {
slouken@1895
    92
            if (argv) {
slouken@1895
    93
                *bufp = '\0';
slouken@1895
    94
            }
slouken@1895
    95
            ++bufp;
slouken@1895
    96
        }
slouken@2286
    97
slouken@2286
    98
        /* Strip out \ from \" sequences */
slouken@2286
    99
        if (argv && last_argc != argc) {
slouken@2286
   100
            UnEscapeQuotes(argv[last_argc]);
slouken@2286
   101
        }
slouken@2286
   102
        last_argc = argc;
slouken@1895
   103
    }
slouken@1895
   104
    if (argv) {
slouken@1895
   105
        argv[argc] = NULL;
slouken@1895
   106
    }
slouken@1895
   107
    return (argc);
slouken@754
   108
}
slouken@754
   109
slouken@754
   110
/* Show an error message */
slouken@1895
   111
static void
slouken@1895
   112
ShowError(const char *title, const char *message)
slouken@754
   113
{
slouken@754
   114
/* If USE_MESSAGEBOX is defined, you need to link with user32.lib */
slouken@754
   115
#ifdef USE_MESSAGEBOX
slouken@1895
   116
    MessageBox(NULL, message, title, MB_ICONEXCLAMATION | MB_OK);
slouken@754
   117
#else
slouken@1895
   118
    fprintf(stderr, "%s: %s\n", title, message);
slouken@754
   119
#endif
slouken@754
   120
}
slouken@754
   121
slouken@754
   122
/* Pop up an out of memory message, returns to Windows */
slouken@1895
   123
static BOOL
slouken@1895
   124
OutOfMemory(void)
slouken@754
   125
{
slouken@1895
   126
    ShowError("Fatal Error", "Out of memory - aborting");
slouken@1895
   127
    return FALSE;
slouken@754
   128
}
slouken@754
   129
slouken@754
   130
#if defined(_MSC_VER) && !defined(_WIN32_WCE)
slouken@754
   131
/* The VC++ compiler needs main defined */
slouken@754
   132
#define console_main main
slouken@754
   133
#endif
slouken@754
   134
slouken@754
   135
/* This is where execution begins [console apps] */
slouken@1895
   136
int
slouken@1895
   137
console_main(int argc, char *argv[])
slouken@754
   138
{
slouken@1895
   139
    int status;
slouken@754
   140
slouken@1895
   141
    /* Run the application main() code */
slouken@1895
   142
    status = SDL_main(argc, argv);
slouken@754
   143
slouken@1895
   144
    /* Exit cleanly, calling atexit() functions */
slouken@1895
   145
    exit(status);
slouken@754
   146
slouken@1895
   147
    /* Hush little compiler, don't you cry... */
slouken@1895
   148
    return 0;
slouken@754
   149
}
slouken@754
   150
slouken@754
   151
/* This is where execution begins [windowed apps] */
slouken@1895
   152
int WINAPI
slouken@1895
   153
WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPTSTR szCmdLine, int sw)
slouken@754
   154
{
slouken@1895
   155
    char **argv;
slouken@1895
   156
    int argc;
slouken@1895
   157
    char *cmdline;
slouken@1286
   158
#ifdef _WIN32_WCE
slouken@1895
   159
    wchar_t *bufp;
slouken@1895
   160
    int nLen;
slouken@754
   161
#else
slouken@1895
   162
    char *bufp;
slouken@1895
   163
    size_t nLen;
slouken@754
   164
#endif
slouken@754
   165
slouken@1465
   166
#ifdef _WIN32_WCE
slouken@1895
   167
    nLen = wcslen(szCmdLine) + 128 + 1;
slouken@1895
   168
    bufp = SDL_stack_alloc(wchar_t, nLen * 2);
slouken@1895
   169
    wcscpy(bufp, TEXT("\""));
slouken@1895
   170
    GetModuleFileName(NULL, bufp + 1, 128 - 3);
slouken@1895
   171
    wcscpy(bufp + wcslen(bufp), TEXT("\" "));
slouken@1895
   172
    wcsncpy(bufp + wcslen(bufp), szCmdLine, nLen - wcslen(bufp));
slouken@1895
   173
    nLen = wcslen(bufp) + 1;
slouken@1895
   174
    cmdline = SDL_stack_alloc(char, nLen);
slouken@1895
   175
    if (cmdline == NULL) {
slouken@1895
   176
        return OutOfMemory();
slouken@1895
   177
    }
slouken@1895
   178
    WideCharToMultiByte(CP_ACP, 0, bufp, -1, cmdline, nLen, NULL, NULL);
slouken@754
   179
#else
slouken@1895
   180
    /* Grab the command line */
slouken@1895
   181
    bufp = GetCommandLine();
slouken@1895
   182
    nLen = SDL_strlen(bufp) + 1;
slouken@1895
   183
    cmdline = SDL_stack_alloc(char, nLen);
slouken@1895
   184
    if (cmdline == NULL) {
slouken@1895
   185
        return OutOfMemory();
slouken@1895
   186
    }
slouken@1895
   187
    SDL_strlcpy(cmdline, bufp, nLen);
slouken@1465
   188
#endif
slouken@754
   189
slouken@1895
   190
    /* Parse it into argv and argc */
slouken@1895
   191
    argc = ParseCommandLine(cmdline, NULL);
slouken@1895
   192
    argv = SDL_stack_alloc(char *, argc + 1);
slouken@1895
   193
    if (argv == NULL) {
slouken@1895
   194
        return OutOfMemory();
slouken@1895
   195
    }
slouken@1895
   196
    ParseCommandLine(cmdline, argv);
slouken@754
   197
slouken@1895
   198
    /* Run the main program */
slouken@1895
   199
    console_main(argc, argv);
slouken@754
   200
slouken@1895
   201
    /* Hush little compiler, don't you cry... */
slouken@1895
   202
    return 0;
slouken@1895
   203
}
slouken@754
   204
slouken@1895
   205
/* vi: set ts=4 sw=4 expandtab: */