Skip to content

Commit fd0d846

Browse files
committed
[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
1 parent 48985f5 commit fd0d846

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

flang/runtime/file.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,19 @@ void OpenFile::set_path(OwningPtr<char> &&path, std::size_t bytes) {
3333
static int openfile_mkstemp(IoErrorHandler &handler) {
3434
#ifdef _WIN32
3535
const unsigned int uUnique{0};
36-
// GetTempFileNameA needs a directory name < MAX_PATH-14 characters in length.
37-
// https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-gettempfilenamea
38-
char tempDirName[MAX_PATH - 14];
39-
char tempFileName[MAX_PATH];
36+
// GetTempFileNameW needs a directory name < MAX_PATH-14 characters in length.
37+
// https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-gettempfilenamew
38+
wchar_t tempDirName[MAX_PATH - 14];
39+
wchar_t tempFileName[MAX_PATH];
4040
unsigned long nBufferLength{sizeof(tempDirName)};
41-
nBufferLength = ::GetTempPathA(nBufferLength, tempDirName);
41+
nBufferLength = ::GetTempPathW(nBufferLength, tempDirName);
4242
if (nBufferLength > sizeof(tempDirName) || nBufferLength == 0) {
4343
return -1;
4444
}
45-
if (::GetTempFileNameA(tempDirName, "Fortran", uUnique, tempFileName) == 0) {
45+
if (::GetTempFileNameW(tempDirName, L"Fortran", uUnique, tempFileName) == 0) {
4646
return -1;
4747
}
48-
int fd{::_open(tempFileName, _O_CREAT | _O_BINARY | _O_TEMPORARY | _O_RDWR,
48+
int fd{::_wopen(tempFileName, _O_CREAT | _O_BINARY | _O_TEMPORARY | _O_RDWR,
4949
_S_IREAD | _S_IWRITE)};
5050
#else
5151
char path[]{"/tmp/Fortran-Scratch-XXXXXX"};

0 commit comments

Comments
 (0)