Skip to content

Commit f684355

Browse files
ArcsinXtstellar
authored andcommitted
[Support][Windows] Fix incorrect GetFinalPathNameByHandleW() return value check in realPathFromHandle()
`GetFinalPathNameByHandleW(,,N,)` returns: - `< N` on success (this value does not include the size of the terminating null character) - `>= N` if buffer is too small (this value includes the size of the terminating null character) So, when `N == Buffer.capacity() - 1`, we need to resize buffer if return value is > `Buffer.capacity() - 2`. Also, we can set `N` to `Buffer.capacity()`. Thus, without this patch `realPathFromHandle()` returns unfilled buffer when length of the final path of the file is equal to `Buffer.capacity()` or `Buffer.capacity() - 1`. Reviewed By: andrewng, amccarth Differential Revision: https://reviews.llvm.org/D86564 (cherry picked from commit ceffd69)
1 parent 98f575f commit f684355

File tree

1 file changed

+3
-4
lines changed

1 file changed

+3
-4
lines changed

llvm/lib/Support/Windows/Path.inc

+3-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
#include "llvm/Support/ConvertUTF.h"
2020
#include "llvm/Support/WindowsError.h"
2121
#include <fcntl.h>
22-
#include <io.h>
2322
#include <sys/stat.h>
2423
#include <sys/types.h>
2524

@@ -352,13 +351,13 @@ std::error_code is_local(const Twine &path, bool &result) {
352351
static std::error_code realPathFromHandle(HANDLE H,
353352
SmallVectorImpl<wchar_t> &Buffer) {
354353
DWORD CountChars = ::GetFinalPathNameByHandleW(
355-
H, Buffer.begin(), Buffer.capacity() - 1, FILE_NAME_NORMALIZED);
356-
if (CountChars > Buffer.capacity()) {
354+
H, Buffer.begin(), Buffer.capacity(), FILE_NAME_NORMALIZED);
355+
if (CountChars && CountChars >= Buffer.capacity()) {
357356
// The buffer wasn't big enough, try again. In this case the return value
358357
// *does* indicate the size of the null terminator.
359358
Buffer.reserve(CountChars);
360359
CountChars = ::GetFinalPathNameByHandleW(
361-
H, Buffer.data(), Buffer.capacity() - 1, FILE_NAME_NORMALIZED);
360+
H, Buffer.begin(), Buffer.capacity(), FILE_NAME_NORMALIZED);
362361
}
363362
if (CountChars == 0)
364363
return mapWindowsError(GetLastError());

0 commit comments

Comments
 (0)