Skip to content

Commit 5a9f668

Browse files
azhirnovTheMostDiligent
authored andcommitted
Windows file system: fixed GetCurrentDirectoryImpl()
1 parent 6c31866 commit 5a9f668

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

Platforms/Win32/src/Win32FileSystem.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -342,18 +342,22 @@ std::string GetCurrentDirectoryImpl()
342342
{
343343
std::string CurrDir;
344344

345-
// If the function succeeds, the return value specifies the number of characters that are
346-
// written to the buffer, not including the terminating null character.
347-
auto NumChars = GetCurrentDirectoryA(0, nullptr);
348-
349-
if (NumChars > 0)
345+
// If the function succeeds, the return value specifies the number of characters that
346+
// are written to the buffer, NOT including the terminating null character.
347+
// HOWEVER, if the buffer that is pointed to by lpBuffer is not large enough,
348+
// the return value specifies the required size of the buffer, in characters,
349+
// INCLUDING the null-terminating character.
350+
// https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getcurrentdirectory
351+
auto BufferSize = GetCurrentDirectoryA(0, nullptr);
352+
353+
if (BufferSize > 1)
350354
{
351-
auto BufferSize = NumChars + 1;
352-
CurrDir.resize(NumChars); // Resize the string to a length of NumChars characters.
355+
// Note that std::string::resize(n) resizes the string to a length of n characters.
356+
CurrDir.resize(BufferSize - 1);
353357

354358
// BufferSize must include room for a terminating null character.
355-
// https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getcurrentdirectory
356-
GetCurrentDirectoryA(BufferSize, &CurrDir[0]);
359+
auto NumChars = GetCurrentDirectoryA(BufferSize, &CurrDir[0]);
360+
VERIFY_EXPR(CurrDir.length() == NumChars);
357361
}
358362
return CurrDir;
359363
}

0 commit comments

Comments
 (0)