Skip to content

Commit dfe1d55

Browse files
committed
includes_normalizes: Full fix for long path support.
Use GetFullPathNameW() instead of GetFullPathNameA() to ensure that normalization works for all long paths on Windows. Adjust unit tests accordingly. Fixes #2442
1 parent 417572b commit dfe1d55

File tree

5 files changed

+124
-27
lines changed

5 files changed

+124
-27
lines changed

src/includes_normalize-win32.cc

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,60 @@
1414

1515
#include "includes_normalize.h"
1616

17-
#include "string_piece.h"
18-
#include "string_piece_util.h"
19-
#include "util.h"
17+
#include <string.h>
18+
19+
#include <windows.h>
2020

2121
#include <algorithm>
2222
#include <iterator>
2323
#include <sstream>
2424

25-
#include <windows.h>
25+
#include "string_piece.h"
26+
#include "string_piece_util.h"
27+
#include "util.h"
2628

2729
namespace {
2830

31+
constexpr const std::size_t MAX_PATH_LONG = 32767;
32+
2933
bool InternalGetFullPathName(const StringPiece& file_name, char* buffer,
3034
size_t buffer_length, std::string* err) {
31-
DWORD result_size = GetFullPathNameA(file_name.AsString().c_str(),
32-
buffer_length, buffer, NULL);
33-
if (result_size == 0) {
34-
*err = "GetFullPathNameA(" + file_name.AsString() + "): " +
35-
GetLastErrorString();
35+
// IMPORTANT: Using GetFullPathNameA() with a long paths will fail with
36+
// "The filename or extension is too long" even if long path supported is
37+
// enabled. GetFullPathNameW() must be used for this function to work!
38+
buffer[0] = '\0';
39+
// Convert to wide filename first.
40+
std::wstring wide_filename;
41+
if (!ConvertUTF8ToWin32Unicode(file_name, &wide_filename, err))
3642
return false;
37-
} else if (result_size > buffer_length) {
38-
*err = "path too long";
43+
44+
// Call GetFullPathNameW()
45+
DWORD wide_full_size = GetFullPathNameW(wide_filename.c_str(), 0, NULL, NULL);
46+
if (wide_full_size == 0) {
47+
*err = "GetFullPathNameW(" +
48+
std::string(wide_filename.begin(), wide_filename.end()) +
49+
"): " + GetLastErrorString();
50+
return false;
51+
}
52+
53+
// NOTE: wide_full_size includes the null-terminating character.
54+
std::wstring wide_path;
55+
wide_path.resize(static_cast<size_t>(wide_full_size - 1));
56+
DWORD wide_full_size2 =
57+
GetFullPathNameW(wide_filename.c_str(), wide_full_size,
58+
const_cast<wchar_t*>(wide_path.data()), NULL);
59+
if (wide_full_size2 == 0) {
60+
*err = "GetFullPathNameW(" + file_name.AsString() + "): " + GetLastErrorString();
61+
return false;
62+
}
63+
64+
// Convert wide_path to Unicode.
65+
int utf8_size =
66+
WideCharToMultiByte(CP_UTF8, 0, wide_path.c_str(), wide_path.size() + 1,
67+
buffer, buffer_length, NULL, NULL);
68+
if (utf8_size <= 0 || utf8_size >= buffer_length) {
69+
*err = "WideCharToMultiByte(" + std::string(wide_path.begin(), wide_path.end()) +
70+
"): " + GetLastErrorString();
3971
return false;
4072
}
4173
return true;
@@ -74,8 +106,8 @@ bool SameDrive(StringPiece a, StringPiece b, std::string* err) {
74106
return true;
75107
}
76108

77-
char a_absolute[_MAX_PATH];
78-
char b_absolute[_MAX_PATH];
109+
char a_absolute[MAX_PATH_LONG];
110+
char b_absolute[MAX_PATH_LONG];
79111
if (!InternalGetFullPathName(a, a_absolute, sizeof(a_absolute), err)) {
80112
return false;
81113
}
@@ -144,7 +176,7 @@ std::string IncludesNormalize::AbsPath(StringPiece s, std::string* err) {
144176
return result;
145177
}
146178

147-
char result[_MAX_PATH];
179+
char result[MAX_PATH_LONG];
148180
if (!InternalGetFullPathName(s, result, sizeof(result), err)) {
149181
return "";
150182
}
@@ -183,9 +215,9 @@ std::string IncludesNormalize::Relativize(
183215

184216
bool IncludesNormalize::Normalize(const std::string& input, std::string* result,
185217
std::string* err) const {
186-
char copy[_MAX_PATH + 1];
218+
char copy[MAX_PATH_LONG + 1];
187219
size_t len = input.size();
188-
if (len > _MAX_PATH) {
220+
if (len > MAX_PATH_LONG) {
189221
*err = "path too long";
190222
return false;
191223
}

src/includes_normalize_test.cc

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -98,18 +98,20 @@ TEST(IncludesNormalize, DifferentDrive) {
9898
}
9999

100100
TEST(IncludesNormalize, LongInvalidPath) {
101+
// A long an invalid path that is larger than _MAX_PATH will still be
102+
// canonicalized without crashing.
101103
const char kLongInputString[] =
102104
"C:\\Program Files (x86)\\Microsoft Visual Studio "
103105
"12.0\\VC\\INCLUDEwarning #31001: The dll for reading and writing the "
104106
"pdb (for example, mspdb110.dll) could not be found on your path. This "
105107
"is usually a configuration error. Compilation will continue using /Z7 "
106108
"instead of /Zi, but expect a similar error when you link your program.";
107-
// Too long, won't be canonicalized. Ensure doesn't crash.
109+
ASSERT_GT(sizeof(kLongInputString) - 1u, _MAX_PATH);
108110
std::string result, err;
109111
IncludesNormalize normalizer(".");
110-
EXPECT_FALSE(
111-
normalizer.Normalize(kLongInputString, &result, &err));
112-
EXPECT_EQ("path too long", err);
112+
EXPECT_TRUE(normalizer.Normalize(kLongInputString, &result, &err));
113+
EXPECT_FALSE(result.empty());
114+
EXPECT_TRUE(err.empty()) << err;
113115

114116

115117
// Construct max size path having cwd prefix.
@@ -150,19 +152,39 @@ TEST(IncludesNormalize, ShortRelativeButTooLongAbsolutePath) {
150152
EXPECT_TRUE(normalizer.Normalize("a", &result, &err));
151153
EXPECT_EQ("", err);
152154

155+
constexpr const int MAX_PATH_LONG = 32767;
156+
153157
// Construct max size path having cwd prefix.
154158
// kExactlyMaxPath = "aaaa\\aaaa...aaaa\0";
155-
char kExactlyMaxPath[_MAX_PATH + 1];
156-
for (int i = 0; i < _MAX_PATH; ++i) {
157-
if (i < _MAX_PATH - 1 && i % 10 == 4)
159+
char kExactlyMaxPath[MAX_PATH_LONG + 1];
160+
for (int i = 0; i < MAX_PATH_LONG; ++i) {
161+
if (i < MAX_PATH_LONG - 1 && i % 10 == 4)
158162
kExactlyMaxPath[i] = '\\';
159163
else
160164
kExactlyMaxPath[i] = 'a';
161165
}
162-
kExactlyMaxPath[_MAX_PATH] = '\0';
163-
EXPECT_EQ(strlen(kExactlyMaxPath), static_cast<size_t>(_MAX_PATH));
166+
kExactlyMaxPath[MAX_PATH_LONG] = '\0';
167+
EXPECT_EQ(strlen(kExactlyMaxPath), static_cast<size_t>(MAX_PATH_LONG));
164168

165-
// Make sure a path that's exactly _MAX_PATH long fails with a proper error.
169+
// Make sure a path that's exactly MAX_PATH_LONG long fails with a proper error.
166170
EXPECT_FALSE(normalizer.Normalize(kExactlyMaxPath, &result, &err));
167171
EXPECT_TRUE(err.find("GetFullPathName") != std::string::npos);
172+
173+
err = "";
174+
// Make sure a path that's exactly _MAX_PATH long fails does not fail.
175+
// Construct max size path having cwd prefix.
176+
// exactly_max_path = "aaaa\\aaaa...aaaa\0";
177+
std::string exactly_max_path;
178+
for (int i = 0; i < _MAX_PATH; ++i) {
179+
if (i < _MAX_PATH - 1 && i % 10 == 4)
180+
exactly_max_path.push_back('\\');
181+
else
182+
exactly_max_path.push_back('a');
183+
}
184+
EXPECT_EQ(exactly_max_path.size(), static_cast<size_t>(_MAX_PATH));
185+
EXPECT_TRUE(normalizer.Normalize(exactly_max_path, &result, &err)) << err;
186+
187+
// Make sue a path of _MAX_PATH + 1 characters also works.
188+
std::string more_than_max_path = exactly_max_path + "\\a";
189+
EXPECT_TRUE(normalizer.Normalize(more_than_max_path, &result, &err)) << err;
168190
}

src/util.cc

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,30 @@ void Win32Fatal(const char* function, const char* hint) {
561561
Fatal("%s: %s", function, GetLastErrorString().c_str());
562562
}
563563
}
564-
#endif
564+
565+
bool ConvertUTF8ToWin32Unicode(const StringPiece& input, std::wstring* output,
566+
std::string* err) {
567+
output->clear();
568+
if (input.empty())
569+
return true;
570+
571+
int int_size = static_cast<int>(input.size());
572+
if (static_cast<size_t>(int_size) != input.size()) {
573+
*err = "Input string length > INT_MAX";
574+
return false;
575+
}
576+
int wide_size =
577+
MultiByteToWideChar(CP_UTF8, 0, input.begin(), int_size, nullptr, 0);
578+
if (wide_size <= 0) {
579+
*err = "MultiByteToWideChar(" + input.AsString() + "): " + GetLastErrorString();
580+
return false;
581+
}
582+
output->resize(static_cast<size_t>(wide_size));
583+
MultiByteToWideChar(CP_UTF8, 0, input.begin(), int_size,
584+
const_cast<wchar_t*>(output->data()), wide_size);
585+
return true;
586+
}
587+
#endif // _WIN32
565588

566589
bool islatinalpha(int c) {
567590
// isalpha() is locale-dependent.

src/util.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
#include <string>
2727
#include <vector>
2828

29+
#include "string_piece.h"
30+
2931
#if !defined(__has_cpp_attribute)
3032
# define __has_cpp_attribute(x) 0
3133
#endif
@@ -133,6 +135,12 @@ std::string GetLastErrorString();
133135
/// Calls Fatal() with a function name and GetLastErrorString.
134136
NORETURN void Win32Fatal(const char* function, const char* hint = NULL);
135137

138+
/// Convert UTF-8 string to Win32 Unicode.
139+
/// On success, set |*output| then return true.
140+
/// On Failure, clear |*output|, set |*err| then return false.
141+
bool ConvertUTF8ToWin32Unicode(const StringPiece& input, std::wstring* output,
142+
std::string* err);
143+
136144
/// Naive implementation of C++ 20 std::bit_cast(), used to fix Clang and GCC
137145
/// [-Wcast-function-type] warning on casting result of GetProcAddress().
138146
template <class To, class From>

src/util_test.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,3 +502,15 @@ TEST(StripAnsiEscapeCodes, StripColors) {
502502
EXPECT_EQ("affixmgr.cxx:286:15: warning: using the result... [-Wparentheses]",
503503
stripped);
504504
}
505+
506+
#ifdef _WIN32
507+
TEST(ConvertUTF8ToWin32Unicode, Test) {
508+
std::string err;
509+
std::wstring output;
510+
EXPECT_TRUE(ConvertUTF8ToWin32Unicode(StringPiece("B\xC3\xA9"
511+
"b\xC3\xA9"),
512+
&output, &err));
513+
EXPECT_TRUE(err.empty()) << err;
514+
EXPECT_EQ(output, std::wstring(L"B\u00E9b\u00E9"));
515+
}
516+
#endif // _WIN32

0 commit comments

Comments
 (0)