Windows GetBasePath should use GetModuleFileNameExW() and check for overflows.
authorRyan C. Gordon
Thu, 28 May 2015 01:54:52 -0400
changeset 96710e1f57b051f4
parent 9670 418e4dee26cb
child 9672 bc1ba207ff16
Windows GetBasePath should use GetModuleFileNameExW() and check for overflows.

Apparently you might get strange paths from GetModuleFileName(), such as
short path names or UNC filenames, so this avoids that problem. Since you have
to tapdance with linking different libraries and defining macros depending on
what Windows you plan to target, we dynamically load the API we need, which
works on all versions of Windows (on Win7, it'll load a compatibility wrapper
for the newer API location).

What a mess.

This also now does the right thing if there isn't enough space to store the
path, looping with a larger allocated buffer each try.

Fixes Bugzilla #2435.
src/filesystem/windows/SDL_sysfilesystem.c
     1.1 --- a/src/filesystem/windows/SDL_sysfilesystem.c	Thu May 28 14:34:38 2015 -0400
     1.2 +++ b/src/filesystem/windows/SDL_sysfilesystem.c	Thu May 28 01:54:52 2015 -0400
     1.3 @@ -36,11 +36,44 @@
     1.4  char *
     1.5  SDL_GetBasePath(void)
     1.6  {
     1.7 -    TCHAR path[MAX_PATH];
     1.8 -    const DWORD len = GetModuleFileName(NULL, path, SDL_arraysize(path));
     1.9 -    size_t i;
    1.10 +    DWORD (WINAPI * pGetModuleFileNameExW)(HANDLE, HMODULE, LPWSTR, DWORD) = NULL;
    1.11 +    DWORD buflen = 128;
    1.12 +    WCHAR *path = NULL;
    1.13 +    HANDLE psapi = LoadLibrary(L"psapi.dll");
    1.14 +    char *retval = NULL;
    1.15 +    DWORD len = 0;
    1.16 +
    1.17 +    if (!psapi) {
    1.18 +        WIN_SetError("Couldn't load psapi.dll");
    1.19 +        return NULL;
    1.20 +    }
    1.21 +
    1.22 +    pGetModuleFileNameExW = GetProcAddress(psapi, "GetModuleFileNameExW");
    1.23 +    if (!pGetModuleFileNameExW) {
    1.24 +        WIN_SetError("Couldn't find GetModuleFileNameExW");
    1.25 +        FreeLibrary(psapi);
    1.26 +        return NULL;
    1.27 +    }
    1.28  
    1.29 -    SDL_assert(len < SDL_arraysize(path));
    1.30 +    while (SDL_TRUE) {
    1.31 +        path = (WCHAR *) SDL_malloc(path, buflen * sizeof (WCHAR));
    1.32 +        if (!path) {
    1.33 +            FreeLibrary(psapi);
    1.34 +            SDL_OutOfMemory();
    1.35 +            return NULL;
    1.36 +        }
    1.37 +
    1.38 +        len = pGetModuleFileNameEx(GetCurrentProcess(), NULL, path, buflen);
    1.39 +        if (len != buflen) {
    1.40 +            break;
    1.41 +        }
    1.42 +
    1.43 +        /* buffer too small? Try again. */
    1.44 +        SDL_free(path);
    1.45 +        len *= 2;
    1.46 +    }
    1.47 +
    1.48 +    FreeLibrary(psapi);
    1.49  
    1.50      if (len == 0) {
    1.51          WIN_SetError("Couldn't locate our .exe");
    1.52 @@ -55,7 +88,11 @@
    1.53  
    1.54      SDL_assert(i > 0); /* Should have been an absolute path. */
    1.55      path[i+1] = '\0';  /* chop off filename. */
    1.56 -    return WIN_StringToUTF8(path);
    1.57 +
    1.58 +    retval = WIN_StringToUTF8(path);
    1.59 +    SDL_free(path);
    1.60 +
    1.61 +    return retval;
    1.62  }
    1.63  
    1.64  char *