diff --git a/flang/runtime/command.cpp b/flang/runtime/command.cpp index 7355cdeef516c5..e642248a25e688 100644 --- a/flang/runtime/command.cpp +++ b/flang/runtime/command.cpp @@ -19,12 +19,17 @@ #include "flang/Common/windows-include.h" #include #define getcwd _getcwd +#define PATH_MAX MAX_PATH // On Windows GetCurrentProcessId returns a DWORD aka uint32_t #include inline pid_t getpid() { return GetCurrentProcessId(); } #else #include //getpid() + +#ifndef PATH_MAX +#define PATH_MAX 4096 +#endif #endif namespace Fortran::runtime { @@ -247,8 +252,9 @@ std::int32_t RTNAME(GetCwd)( RUNTIME_CHECK(terminator, IsValidCharDescriptor(&cwd)); - char *buf{getcwd(nullptr, 0)}; - if (!buf) { + char *buf{(char *)AllocateMemoryOrCrash(terminator, PATH_MAX)}; + + if (!getcwd(buf, PATH_MAX)) { return StatMissingCurrentWorkDirectory; }