src/main/windows/SDL_windows_main.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Wed, 29 Oct 2014 20:20:47 +0100
changeset 9201 21d9f9babb30
parent 9016 648dbad92045
child 9246 a761913e5e91
permissions -rw-r--r--
Fixed bug 2647 - Memory leak in SDL_AddHintCallback function - SDL_hints.c

Nitz

Variable entry going out of scope leaks the storage it points to, at:

/* Need to add a hint entry for this watcher */
hint = (SDL_Hint *)SDL_malloc(sizeof(*hint));
if (!hint) {
return;
}

Patch is attached.
slouken@754
     1
/*
slouken@8616
     2
    SDL_windows_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@6044
     6
#include "SDL_config.h"
slouken@6044
     7
slouken@6044
     8
#ifdef __WIN32__
slouken@754
     9
slouken@7828
    10
/* Include this so we define UNICODE properly */
slouken@7828
    11
#include "../../core/windows/SDL_windows.h"
slouken@7828
    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
#  undef main
slouken@754
    19
#endif /* main */
slouken@754
    20
slouken@2286
    21
static void
slouken@2286
    22
UnEscapeQuotes(char *arg)
slouken@2286
    23
{
slouken@2286
    24
    char *last = NULL;
slouken@2286
    25
slouken@2286
    26
    while (*arg) {
aschiffler@6719
    27
        if (*arg == '"' && (last != NULL && *last == '\\')) {
slouken@2286
    28
            char *c_curr = arg;
slouken@2286
    29
            char *c_last = last;
slouken@2286
    30
slouken@2286
    31
            while (*c_curr) {
slouken@2286
    32
                *c_last = *c_curr;
slouken@2286
    33
                c_last = c_curr;
slouken@2286
    34
                c_curr++;
slouken@2286
    35
            }
slouken@2286
    36
            *c_last = '\0';
slouken@2286
    37
        }
slouken@2286
    38
        last = arg;
slouken@2286
    39
        arg++;
slouken@2286
    40
    }
slouken@2286
    41
}
slouken@2286
    42
slouken@754
    43
/* Parse a command line buffer into arguments */
slouken@1895
    44
static int
slouken@1895
    45
ParseCommandLine(char *cmdline, char **argv)
slouken@754
    46
{
slouken@1895
    47
    char *bufp;
slouken@2286
    48
    char *lastp = NULL;
slouken@2286
    49
    int argc, last_argc;
slouken@754
    50
slouken@2286
    51
    argc = last_argc = 0;
slouken@1895
    52
    for (bufp = cmdline; *bufp;) {
slouken@1895
    53
        /* Skip leading whitespace */
philipp@7450
    54
        while (SDL_isspace(*bufp)) {
slouken@1895
    55
            ++bufp;
slouken@1895
    56
        }
slouken@1895
    57
        /* Skip over argument */
slouken@1895
    58
        if (*bufp == '"') {
slouken@1895
    59
            ++bufp;
slouken@1895
    60
            if (*bufp) {
slouken@1895
    61
                if (argv) {
slouken@1895
    62
                    argv[argc] = bufp;
slouken@1895
    63
                }
slouken@1895
    64
                ++argc;
slouken@1895
    65
            }
slouken@1895
    66
            /* Skip over word */
slouken@4511
    67
            lastp = bufp;
slouken@2286
    68
            while (*bufp && (*bufp != '"' || *lastp == '\\')) {
slouken@2286
    69
                lastp = bufp;
slouken@1895
    70
                ++bufp;
slouken@1895
    71
            }
slouken@1895
    72
        } else {
slouken@1895
    73
            if (*bufp) {
slouken@1895
    74
                if (argv) {
slouken@1895
    75
                    argv[argc] = bufp;
slouken@1895
    76
                }
slouken@1895
    77
                ++argc;
slouken@1895
    78
            }
slouken@1895
    79
            /* Skip over word */
philipp@7450
    80
            while (*bufp && !SDL_isspace(*bufp)) {
slouken@1895
    81
                ++bufp;
slouken@1895
    82
            }
slouken@1895
    83
        }
slouken@1895
    84
        if (*bufp) {
slouken@1895
    85
            if (argv) {
slouken@1895
    86
                *bufp = '\0';
slouken@1895
    87
            }
slouken@1895
    88
            ++bufp;
slouken@1895
    89
        }
slouken@2286
    90
slouken@2286
    91
        /* Strip out \ from \" sequences */
slouken@2286
    92
        if (argv && last_argc != argc) {
slouken@2286
    93
            UnEscapeQuotes(argv[last_argc]);
slouken@2286
    94
        }
slouken@2286
    95
        last_argc = argc;
slouken@1895
    96
    }
slouken@1895
    97
    if (argv) {
slouken@1895
    98
        argv[argc] = NULL;
slouken@1895
    99
    }
slouken@1895
   100
    return (argc);
slouken@754
   101
}
slouken@754
   102
slouken@754
   103
/* Pop up an out of memory message, returns to Windows */
slouken@1895
   104
static BOOL
slouken@1895
   105
OutOfMemory(void)
slouken@754
   106
{
icculus@9016
   107
    SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Fatal Error", "Out of memory - aborting", NULL);
slouken@1895
   108
    return FALSE;
slouken@754
   109
}
slouken@754
   110
icculus@6430
   111
#if defined(_MSC_VER)
slouken@754
   112
/* The VC++ compiler needs main defined */
slouken@754
   113
#define console_main main
slouken@754
   114
#endif
slouken@754
   115
slouken@754
   116
/* This is where execution begins [console apps] */
slouken@1895
   117
int
slouken@1895
   118
console_main(int argc, char *argv[])
slouken@754
   119
{
slouken@7281
   120
    SDL_SetMainReady();
slouken@7281
   121
slouken@1895
   122
    /* Run the application main() code */
icculus@9016
   123
    return SDL_main(argc, argv);
slouken@754
   124
}
slouken@754
   125
slouken@754
   126
/* This is where execution begins [windowed apps] */
slouken@1895
   127
int WINAPI
slouken@6672
   128
WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw)
slouken@1895
   129
{
slouken@1895
   130
    char **argv;
slouken@1895
   131
    int argc;
slouken@1895
   132
    char *cmdline;
slouken@754
   133
slouken@1895
   134
    /* Grab the command line */
slouken@6672
   135
    TCHAR *text = GetCommandLine();
slouken@6672
   136
#if UNICODE
slouken@6672
   137
    cmdline = SDL_iconv_string("UTF-8", "UCS-2-INTERNAL", (char *)(text), (SDL_wcslen(text)+1)*sizeof(WCHAR));
slouken@6672
   138
#else
slouken@6672
   139
    cmdline = SDL_strdup(text);
slouken@6672
   140
#endif
slouken@1895
   141
    if (cmdline == NULL) {
slouken@1895
   142
        return OutOfMemory();
slouken@1895
   143
    }
slouken@754
   144
slouken@1895
   145
    /* Parse it into argv and argc */
slouken@1895
   146
    argc = ParseCommandLine(cmdline, NULL);
slouken@1895
   147
    argv = SDL_stack_alloc(char *, argc + 1);
slouken@1895
   148
    if (argv == NULL) {
slouken@1895
   149
        return OutOfMemory();
slouken@1895
   150
    }
slouken@1895
   151
    ParseCommandLine(cmdline, argv);
slouken@754
   152
slouken@1895
   153
    /* Run the main program */
slouken@1895
   154
    console_main(argc, argv);
slouken@754
   155
philipp@7356
   156
    SDL_stack_free(argv);
philipp@7356
   157
slouken@6672
   158
    SDL_free(cmdline);
slouken@6672
   159
slouken@1895
   160
    /* Hush little compiler, don't you cry... */
slouken@1895
   161
    return 0;
slouken@1895
   162
}
slouken@754
   163
slouken@6044
   164
#endif /* __WIN32__ */
slouken@6044
   165
slouken@1895
   166
/* vi: set ts=4 sw=4 expandtab: */