소스 검색

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.
Ryan C. Gordon 10 년 전
부모
커밋
6d1ad3847a
1개의 변경된 파일42개의 추가작업 그리고 5개의 파일을 삭제
  1. 42 5
      src/filesystem/windows/SDL_sysfilesystem.c

+ 42 - 5
src/filesystem/windows/SDL_sysfilesystem.c

@@ -36,11 +36,44 @@
 char *
 SDL_GetBasePath(void)
 {
-    TCHAR path[MAX_PATH];
-    const DWORD len = GetModuleFileName(NULL, path, SDL_arraysize(path));
-    size_t i;
+    DWORD (WINAPI * pGetModuleFileNameExW)(HANDLE, HMODULE, LPWSTR, DWORD) = NULL;
+    DWORD buflen = 128;
+    WCHAR *path = NULL;
+    HANDLE psapi = LoadLibrary(L"psapi.dll");
+    char *retval = NULL;
+    DWORD len = 0;
+
+    if (!psapi) {
+        WIN_SetError("Couldn't load psapi.dll");
+        return NULL;
+    }
+
+    pGetModuleFileNameExW = GetProcAddress(psapi, "GetModuleFileNameExW");
+    if (!pGetModuleFileNameExW) {
+        WIN_SetError("Couldn't find GetModuleFileNameExW");
+        FreeLibrary(psapi);
+        return NULL;
+    }
 
-    SDL_assert(len < SDL_arraysize(path));
+    while (SDL_TRUE) {
+        path = (WCHAR *) SDL_malloc(path, buflen * sizeof (WCHAR));
+        if (!path) {
+            FreeLibrary(psapi);
+            SDL_OutOfMemory();
+            return NULL;
+        }
+
+        len = pGetModuleFileNameEx(GetCurrentProcess(), NULL, path, buflen);
+        if (len != buflen) {
+            break;
+        }
+
+        /* buffer too small? Try again. */
+        SDL_free(path);
+        len *= 2;
+    }
+
+    FreeLibrary(psapi);
 
     if (len == 0) {
         WIN_SetError("Couldn't locate our .exe");
@@ -55,7 +88,11 @@ SDL_GetBasePath(void)
 
     SDL_assert(i > 0); /* Should have been an absolute path. */
     path[i+1] = '\0';  /* chop off filename. */
-    return WIN_StringToUTF8(path);
+
+    retval = WIN_StringToUTF8(path);
+    SDL_free(path);
+
+    return retval;
 }
 
 char *