From b89a7a54215955101ac647bad2f59bb99e0ae394 Mon Sep 17 00:00:00 2001 From: JumpMasterJJ <1336724109@qq.com> Date: Fri, 24 May 2024 13:28:19 +0800 Subject: [PATCH] fix unspecific behavior on getcwd --- flang/runtime/command.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) 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; }