34
34
namespace bazel {
35
35
namespace windows {
36
36
37
- using std::wstring;
38
37
using std::unique_ptr;
39
38
using std::wstring;
40
39
41
40
static const wstring kUncPrefix = wstring(L" \\\\ ?\\ " );
41
+ // Using `MAX_PATH` - 4 instead of `MAX_PATH` to fix
42
+ // https://github.com/bazelbuild/bazel/issues/12310
43
+ constexpr size_t kMaxPath = MAX_PATH - 4 ;
42
44
43
45
// Retrieves TEST_TMPDIR as a shortened path. Result won't have a "\\?\" prefix.
44
46
static void GetShortTempDir (wstring* result) {
@@ -66,11 +68,11 @@ static void GetShortTempDir(wstring* result) {
66
68
::GetShortPathNameW (tmpdir.c_str(), buf.get(), size);
67
69
68
70
// Set the result, omit the "\\?\" prefix.
69
- // Ensure that the result is shorter than MAX_PATH and also has room for a
71
+ // Ensure that the result is shorter than `kMaxPath` and also has room for a
70
72
// backslash (1 wchar) and a single-letter executable name with .bat
71
73
// extension (5 wchars).
72
74
*result = wstring (buf.get () + 4 );
73
- ASSERT_LT (result->size (), MAX_PATH - 6 );
75
+ ASSERT_LT (result->size (), kMaxPath - 6 );
74
76
}
75
77
76
78
// If `success` is true, returns an empty string, otherwise an error message.
@@ -165,14 +167,14 @@ static wstring DeleteDir(wstring path) {
165
167
// `result_path` will be also a short path under `basedir`.
166
168
//
167
169
// Every directory in `result_path` will be created. The entire length of this
168
- // path will be exactly MAX_PATH - 7 (not including null-terminator).
170
+ // path will be exactly `kMaxPath` - 7 (not including null-terminator).
169
171
// Just by appending a file name segment between 6 and 8 characters long (i.e.
170
172
// "\a.bat", "\ab.bat", or "\abc.bat") the caller can obtain a path that is
171
- // MAX_PATH - 1 long, or MAX_PATH long, or MAX_PATH + 1 long, respectively ,
172
- // and cannot be shortened further.
173
+ // `kMaxPath` - 1 long, or `kMaxPath` long, or `kMaxPath` + 1 long,
174
+ // respectively, and cannot be shortened further.
173
175
static void CreateShortDirsUnder (wstring basedir, wstring* result_path) {
174
- ASSERT_LT (basedir.size (), MAX_PATH );
175
- size_t remaining_len = MAX_PATH - 1 - basedir.size ();
176
+ ASSERT_LT (basedir.size (), kMaxPath );
177
+ size_t remaining_len = kMaxPath - 1 - basedir.size ();
176
178
ASSERT_GE (remaining_len, 6 ); // has room for suffix "\a.bat"
177
179
178
180
// If `remaining_len` is odd, make it even.
@@ -188,7 +190,7 @@ static void CreateShortDirsUnder(wstring basedir, wstring* result_path) {
188
190
basedir += wstring (L" \\ a" );
189
191
CREATE_DIR (basedir);
190
192
}
191
- ASSERT_EQ (basedir.size (), MAX_PATH - 1 - 6 );
193
+ ASSERT_EQ (basedir.size (), kMaxPath - 1 - 6 );
192
194
*result_path = basedir;
193
195
}
194
196
@@ -260,7 +262,7 @@ TEST(WindowsUtilTest, TestAsExecutablePathForCreateProcessBadInputs) {
260
262
ASSERT_SHORTENING_FAILS (L" \\ bar.exe" , L" path is absolute" );
261
263
262
264
wstring dummy = L" hello" ;
263
- while (dummy.size () < MAX_PATH ) {
265
+ while (dummy.size () < kMaxPath ) {
264
266
dummy += dummy;
265
267
}
266
268
dummy += L" .exe" ;
@@ -281,24 +283,24 @@ TEST(WindowsUtilTest, TestAsExecutablePathForCreateProcessConversions) {
281
283
CreateShortDirsUnder (tmpdir, &short_root);
282
284
283
285
// Assert that we have enough room to append a file name that is just short
284
- // enough to fit into MAX_PATH - 1, or one that's just long enough to make
285
- // the whole path MAX_PATH long or longer.
286
- ASSERT_EQ (short_root.size (), MAX_PATH - 1 - 6 );
286
+ // enough to fit into `kMaxPath` - 1, or one that's just long enough to make
287
+ // the whole path `kMaxPath` long or longer.
288
+ ASSERT_EQ (short_root.size (), kMaxPath - 1 - 6 );
287
289
288
290
wstring actual;
289
291
wstring error;
290
292
for (size_t i = 0 ; i < 3 ; ++i) {
291
293
wstring wfilename = short_root;
292
294
293
295
APPEND_FILE_SEGMENT (6 + i, &wfilename);
294
- ASSERT_EQ (wfilename.size (), MAX_PATH - 1 + i);
296
+ ASSERT_EQ (wfilename.size (), kMaxPath - 1 + i);
295
297
296
- // When i=0 then `wfilename` is MAX_PATH - 1 long, so
298
+ // When i=0 then `wfilename` is `kMaxPath` - 1 long, so
297
299
// `AsExecutablePathForCreateProcess` will not attempt to shorten it, and
298
300
// so it also won't notice that the file doesn't exist. If however we pass
299
301
// a non-existent path to CreateProcessA, then it'll fail, so we'll find out
300
302
// about this error in production code.
301
- // When i>0 then `wfilename` is at least MAX_PATH long, so
303
+ // When i>0 then `wfilename` is at least `kMaxPath` long, so
302
304
// `AsExecutablePathForCreateProcess` will attempt to shorten it, but
303
305
// because the file doesn't yet exist, the shortening attempt will fail.
304
306
if (i > 0 ) {
@@ -326,14 +328,14 @@ TEST(WindowsUtilTest, TestAsExecutablePathForCreateProcessConversions) {
326
328
// Finally construct a path that can and will be shortened. Just walk up a few
327
329
// levels in `short_root` and create a long file name that can be shortened.
328
330
wstring wshortenable_root = short_root;
329
- while (wshortenable_root.size () > MAX_PATH - 1 - 13 ) {
331
+ while (wshortenable_root.size () > kMaxPath - 1 - 13 ) {
330
332
wshortenable_root =
331
333
wshortenable_root.substr (0 , wshortenable_root.find_last_of (L' \\ ' ));
332
334
}
333
335
wstring wshortenable = wshortenable_root + wstring (L" \\ " ) +
334
- wstring (MAX_PATH - wshortenable_root.size (), L' a' ) +
336
+ wstring (kMaxPath - wshortenable_root.size (), L' a' ) +
335
337
wstring (L" .bat" );
336
- ASSERT_GT (wshortenable.size (), MAX_PATH );
338
+ ASSERT_GT (wshortenable.size (), kMaxPath );
337
339
338
340
// Attempt to shorten. It will fail because the file doesn't exist yet.
339
341
ASSERT_SHORTENING_FAILS (wshortenable, L" GetShortPathNameW" );
0 commit comments