Skip to content

Commit

Permalink
[flang][runtime] Support all non-ASCII characters in temporary path o…
Browse files Browse the repository at this point in the history
…n 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
  • Loading branch information
mmuetzel committed May 27, 2023
1 parent 48985f5 commit fd0d846
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions flang/runtime/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"};
Expand Down

0 comments on commit fd0d846

Please sign in to comment.