From: Matt Glazar Date: Sat, 4 Feb 2017 00:59:31 +0000 (-0800) Subject: Fix environ enumeration on Apple platforms X-Git-Tag: v2017.03.06.00~56 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=c847b1b4b667dcb6d8d8969d24a38a370053130f;p=folly.git Fix environ enumeration on Apple platforms Summary: environ is documented to not work very well from .dylib-s (dynamic libraries) on macOS. Use the _NSGetEnviron function instead to get a pointer to environ. is missing from some iOS SDKs, so forward-declare _NSGetEnviron in such cases. Reviewed By: Orvid, mzlee Differential Revision: D4491044 fbshipit-source-id: 886d19bdd63dea14225992446b7ba132faf35054 --- diff --git a/folly/portability/Stdlib.h b/folly/portability/Stdlib.h index c3bf7340..333453da 100644 --- a/folly/portability/Stdlib.h +++ b/folly/portability/Stdlib.h @@ -18,6 +18,12 @@ #include +#if defined(__APPLE__) +#if __has_include() +#include +#endif +#endif + extern "C" { #ifdef _WIN32 // These are technically supposed to be defined linux/limits.h and @@ -35,6 +41,10 @@ char* realpath(const char* path, char* resolved_path); int setenv(const char* name, const char* value, int overwrite); int unsetenv(const char* name); #elif defined(__APPLE__) -extern char** environ; +// environ doesn't work well with dylibs, so use _NSGetEnviron instead. +#if !__has_include() +char*** _NSGetEnviron(void); +#endif +#define environ (*_NSGetEnviron()) #endif }