diff options
-rw-r--r-- | llvm/lib/Support/Windows/Path.inc | 17 | ||||
-rw-r--r-- | llvm/unittests/Support/Path.cpp | 33 |
2 files changed, 46 insertions, 4 deletions
diff --git a/llvm/lib/Support/Windows/Path.inc b/llvm/lib/Support/Windows/Path.inc index 5f588d7884e..8e5931ce6d8 100644 --- a/llvm/lib/Support/Windows/Path.inc +++ b/llvm/lib/Support/Windows/Path.inc @@ -821,11 +821,19 @@ std::error_code getPathFromOpenFD(int FD, SmallVectorImpl<char> &ResultPath) { DWORD CharCount; do { + // FIXME: We should be using the W version of this API and converting the + // resulting path to UTF-8 to handle non-ASCII file paths. CharCount = ::GetFinalPathNameByHandleA(FileHandle, ResultPath.begin(), - ResultPath.capacity(), FILE_NAME_NORMALIZED); - if (CharCount <= ResultPath.capacity()) + ResultPath.capacity(), + FILE_NAME_NORMALIZED); + if (CharCount < ResultPath.capacity()) break; - ResultPath.reserve(CharCount); + + // Reserve sufficient space for the path as well as the null character. Even + // though the API does not document that it is required, if we reserve just + // CharCount space, the function call will not store the resulting path and + // still report success. + ResultPath.reserve(CharCount + 1); } while (true); if (CharCount == 0) @@ -833,7 +841,8 @@ std::error_code getPathFromOpenFD(int FD, SmallVectorImpl<char> &ResultPath) { ResultPath.set_size(CharCount); - // On earlier Windows releases, the character count includes the terminating null. + // On earlier Windows releases, the character count includes the terminating + // null. if (ResultPath.back() == '\0') ResultPath.pop_back(); diff --git a/llvm/unittests/Support/Path.cpp b/llvm/unittests/Support/Path.cpp index 705a90bd0a3..74b74ac4195 100644 --- a/llvm/unittests/Support/Path.cpp +++ b/llvm/unittests/Support/Path.cpp @@ -1024,6 +1024,39 @@ TEST_F(FileSystemTest, PathFromFD) { ::close(FileDescriptor); } +TEST_F(FileSystemTest, PathFromFDWin32) { + // Create a temp file. + int FileDescriptor; + SmallString<64> TempPath; + ASSERT_NO_ERROR( + fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath)); + + // Make sure it exists. + ASSERT_TRUE(sys::fs::exists(Twine(TempPath))); + + SmallVector<char, 8> ResultPath; + std::error_code ErrorCode = + fs::getPathFromOpenFD(FileDescriptor, ResultPath); + + if (!ErrorCode) { + // Now that we know how much space is required for the path, create a path + // buffer with exactly enough space (sans null terminator, which should not + // be present), and call getPathFromOpenFD again to ensure that the API + // properly handles exactly-sized buffers. + SmallVector<char, 8> ExactSizedPath(ResultPath.size()); + ErrorCode = fs::getPathFromOpenFD(FileDescriptor, ExactSizedPath); + ResultPath = ExactSizedPath; + } + + if (!ErrorCode) { + fs::UniqueID D1, D2; + ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), D1)); + ASSERT_NO_ERROR(fs::getUniqueID(Twine(ResultPath), D2)); + ASSERT_EQ(D1, D2); + } + ::close(FileDescriptor); +} + TEST_F(FileSystemTest, OpenFileForRead) { // Create a temp file. int FileDescriptor; |