libutil: Properly guard self-allocating getcwd on GNU

It's a GNU extension, as pointed out by pennae.
This commit is contained in:
Cole Helbling 2022-04-01 12:20:34 -07:00
parent 7f5caaa7c0
commit 2a45cf54e4

View file

@ -1698,10 +1698,19 @@ void saveMountNamespace()
#if __linux__ #if __linux__
static std::once_flag done; static std::once_flag done;
std::call_once(done, []() { std::call_once(done, []() {
char* cwd = getcwd(NULL, 0); #ifdef __GNU__
if (cwd == NULL) throw SysError("getting cwd"); // getcwd allocating its return value is a GNU extension.
char *cwd = getcwd(NULL, 0);
if (cwd == NULL)
#else
char cwd[PATH_MAX];
if (!getcwd(cwd, sizeof(cwd)))
#endif
throw SysError("getting cwd");
savedCwd.emplace(cwd); savedCwd.emplace(cwd);
#ifdef __GNU__
free(cwd); free(cwd);
#endif
AutoCloseFD fd = open("/proc/self/ns/mnt", O_RDONLY); AutoCloseFD fd = open("/proc/self/ns/mnt", O_RDONLY);
if (!fd) if (!fd)