Skip to content

Commit 10458f2

Browse files
bsdcodeslouken
authored andcommitted
SDL_getenv.c: fix dynamic loading of environ symbol on FreeBSD
The current implementation uses the returned address of the `dlsym` function directly to load the `environ` symbol. But this function doesn't return the address to the symbol itself, instead it returns the address to the location where the actual address is stored, i.e. it's an additional indirection. Consequently, the implementation fails to load and process the environment variables successfully. One example where this error shows up is in the `Dialog API`: in an `X11` environment, the `zenity` driver requires access to the user's `DISPLAY` and `XAUTHORITY` environment variables. Because these variables aren't transfered to the `zenity` process, no dialogs are shown. This can be exercised in the `test/testdialog.c` testprogram. The fix changes the indirection level of the `dlsym` call from `char **` to `char ***`, does a `NULL`-check in case the call failed, and returns the dereferenced actual adress to the `environ` symbol.
1 parent e239298 commit 10458f2

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/stdlib/SDL_getenv.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,12 @@
4141
#define environ (*_NSGetEnviron())
4242
#elif defined(SDL_PLATFORM_FREEBSD)
4343
#include <dlfcn.h>
44-
#define environ ((char **)dlsym(RTLD_DEFAULT, "environ"))
44+
static char **get_environ_rtld(void)
45+
{
46+
char ***environ_rtld = (char ***)dlsym(RTLD_DEFAULT, "environ");
47+
return environ_rtld ? *environ_rtld : NULL;
48+
}
49+
#define environ (get_environ_rtld())
4550
#else
4651
extern char **environ;
4752
#endif

0 commit comments

Comments
 (0)