Skip to content

Commit d2693d4

Browse files
committed
dynapi: Don't use SDL_getenv; it might malloc before the app sets an allocator.
Use platform-specific code instead, so SDL's allocator never comes into play.
1 parent ed7e7ed commit d2693d4

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/dynapi/SDL_dynapi.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,16 @@ extern SDL_NORETURN void SDL_ExitProcess(int exitcode);
440440

441441
static void SDL_InitDynamicAPILocked(void)
442442
{
443-
char *libname = SDL_getenv_REAL(SDL_DYNAMIC_API_ENVVAR);
443+
/* this can't use SDL_getenv_REAL, because it might allocate memory before the app can set their allocator */
444+
#if defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__)
445+
/* We've always used LoadLibraryA for this, so this has never worked with Unicode paths on Windows. Sorry. */
446+
char envbuf[512]; /* overflows will just report as environment variable being unset, but LoadLibraryA has a MAX_PATH of 260 anyhow, apparently. */
447+
const DWORD rc = GetEnvironmentVariableA(SDL_DYNAMIC_API_ENVVAR, envbuf, (DWORD) sizeof (envbuf));
448+
char *libname = ((rc != 0) && (rc < sizeof (envbuf))) ? envbuf : NULL;
449+
#else
450+
char *libname = getenv(SDL_DYNAMIC_API_ENVVAR);
451+
#endif
452+
444453
SDL_DYNAPI_ENTRYFN entry = NULL; /* funcs from here by default. */
445454
SDL_bool use_internal = SDL_TRUE;
446455

0 commit comments

Comments
 (0)