[flang-commits] [flang] fd0d846 - [flang][runtime] Support all non-ASCII characters in temporary path on Windows

Markus Mützel via flang-commits flang-commits at lists.llvm.org
Sat May 27 05:54:30 PDT 2023


Author: Markus Mützel
Date: 2023-05-27T14:54:18+02:00
New Revision: fd0d846633b05e1d0072754aea7f3985be36c0dc

URL: https://github.com/llvm/llvm-project/commit/fd0d846633b05e1d0072754aea7f3985be36c0dc
DIFF: https://github.com/llvm/llvm-project/commit/fd0d846633b05e1d0072754aea7f3985be36c0dc.diff

LOG: [flang][runtime] Support all non-ASCII characters in temporary path on Windows

If the path to the TEMP folder contains (non-ASCII) characters that cannot be
encoded in the current 8-bit locale of the user, openfile_mkstemp might fail
on Windows.
That is an unlikely scenario. But given that the path to the default TEMP
folder on Windows contains the Windows user name, it is still possible.

Use the wide character Windows API to avoid that (unlikely) issue.

Reviewed By: vzakhari

Differential Revision: https://reviews.llvm.org/D151571

Added: 
    

Modified: 
    flang/runtime/file.cpp

Removed: 
    


################################################################################
diff  --git a/flang/runtime/file.cpp b/flang/runtime/file.cpp
index 5cf91b8d64c8..5c7a1b54ba0d 100644
--- a/flang/runtime/file.cpp
+++ b/flang/runtime/file.cpp
@@ -33,19 +33,19 @@ void OpenFile::set_path(OwningPtr<char> &&path, std::size_t bytes) {
 static int openfile_mkstemp(IoErrorHandler &handler) {
 #ifdef _WIN32
   const unsigned int uUnique{0};
-  // GetTempFileNameA needs a directory name < MAX_PATH-14 characters in length.
-  // https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-gettempfilenamea
-  char tempDirName[MAX_PATH - 14];
-  char tempFileName[MAX_PATH];
+  // GetTempFileNameW needs a directory name < MAX_PATH-14 characters in length.
+  // https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-gettempfilenamew
+  wchar_t tempDirName[MAX_PATH - 14];
+  wchar_t tempFileName[MAX_PATH];
   unsigned long nBufferLength{sizeof(tempDirName)};
-  nBufferLength = ::GetTempPathA(nBufferLength, tempDirName);
+  nBufferLength = ::GetTempPathW(nBufferLength, tempDirName);
   if (nBufferLength > sizeof(tempDirName) || nBufferLength == 0) {
     return -1;
   }
-  if (::GetTempFileNameA(tempDirName, "Fortran", uUnique, tempFileName) == 0) {
+  if (::GetTempFileNameW(tempDirName, L"Fortran", uUnique, tempFileName) == 0) {
     return -1;
   }
-  int fd{::_open(tempFileName, _O_CREAT | _O_BINARY | _O_TEMPORARY | _O_RDWR,
+  int fd{::_wopen(tempFileName, _O_CREAT | _O_BINARY | _O_TEMPORARY | _O_RDWR,
       _S_IREAD | _S_IWRITE)};
 #else
   char path[]{"/tmp/Fortran-Scratch-XXXXXX"};


        


More information about the flang-commits mailing list