3434namespace bazel {
3535namespace windows {
3636
37- using std::wstring;
3837using std::unique_ptr;
3938using std::wstring;
4039
4140static 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 ;
4244
4345// Retrieves TEST_TMPDIR as a shortened path. Result won't have a "\\?\" prefix.
4446static void GetShortTempDir (wstring* result) {
@@ -66,11 +68,11 @@ static void GetShortTempDir(wstring* result) {
6668 ::GetShortPathNameW (tmpdir.c_str(), buf.get(), size);
6769
6870 // 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
7072 // backslash (1 wchar) and a single-letter executable name with .bat
7173 // extension (5 wchars).
7274 *result = wstring (buf.get () + 4 );
73- ASSERT_LT (result->size (), MAX_PATH - 6 );
75+ ASSERT_LT (result->size (), kMaxPath - 6 );
7476}
7577
7678// If `success` is true, returns an empty string, otherwise an error message.
@@ -165,14 +167,14 @@ static wstring DeleteDir(wstring path) {
165167// `result_path` will be also a short path under `basedir`.
166168//
167169// 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).
169171// Just by appending a file name segment between 6 and 8 characters long (i.e.
170172// "\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.
173175static 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 ();
176178 ASSERT_GE (remaining_len, 6 ); // has room for suffix "\a.bat"
177179
178180 // If `remaining_len` is odd, make it even.
@@ -188,7 +190,7 @@ static void CreateShortDirsUnder(wstring basedir, wstring* result_path) {
188190 basedir += wstring (L" \\ a" );
189191 CREATE_DIR (basedir);
190192 }
191- ASSERT_EQ (basedir.size (), MAX_PATH - 1 - 6 );
193+ ASSERT_EQ (basedir.size (), kMaxPath - 1 - 6 );
192194 *result_path = basedir;
193195}
194196
@@ -260,7 +262,7 @@ TEST(WindowsUtilTest, TestAsExecutablePathForCreateProcessBadInputs) {
260262 ASSERT_SHORTENING_FAILS (L" \\ bar.exe" , L" path is absolute" );
261263
262264 wstring dummy = L" hello" ;
263- while (dummy.size () < MAX_PATH ) {
265+ while (dummy.size () < kMaxPath ) {
264266 dummy += dummy;
265267 }
266268 dummy += L" .exe" ;
@@ -281,24 +283,24 @@ TEST(WindowsUtilTest, TestAsExecutablePathForCreateProcessConversions) {
281283 CreateShortDirsUnder (tmpdir, &short_root);
282284
283285 // 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 );
287289
288290 wstring actual;
289291 wstring error;
290292 for (size_t i = 0 ; i < 3 ; ++i) {
291293 wstring wfilename = short_root;
292294
293295 APPEND_FILE_SEGMENT (6 + i, &wfilename);
294- ASSERT_EQ (wfilename.size (), MAX_PATH - 1 + i);
296+ ASSERT_EQ (wfilename.size (), kMaxPath - 1 + i);
295297
296- // When i=0 then `wfilename` is MAX_PATH - 1 long, so
298+ // When i=0 then `wfilename` is `kMaxPath` - 1 long, so
297299 // `AsExecutablePathForCreateProcess` will not attempt to shorten it, and
298300 // so it also won't notice that the file doesn't exist. If however we pass
299301 // a non-existent path to CreateProcessA, then it'll fail, so we'll find out
300302 // 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
302304 // `AsExecutablePathForCreateProcess` will attempt to shorten it, but
303305 // because the file doesn't yet exist, the shortening attempt will fail.
304306 if (i > 0 ) {
@@ -326,14 +328,14 @@ TEST(WindowsUtilTest, TestAsExecutablePathForCreateProcessConversions) {
326328 // Finally construct a path that can and will be shortened. Just walk up a few
327329 // levels in `short_root` and create a long file name that can be shortened.
328330 wstring wshortenable_root = short_root;
329- while (wshortenable_root.size () > MAX_PATH - 1 - 13 ) {
331+ while (wshortenable_root.size () > kMaxPath - 1 - 13 ) {
330332 wshortenable_root =
331333 wshortenable_root.substr (0 , wshortenable_root.find_last_of (L' \\ ' ));
332334 }
333335 wstring wshortenable = wshortenable_root + wstring (L" \\ " ) +
334- wstring (MAX_PATH - wshortenable_root.size (), L' a' ) +
336+ wstring (kMaxPath - wshortenable_root.size (), L' a' ) +
335337 wstring (L" .bat" );
336- ASSERT_GT (wshortenable.size (), MAX_PATH );
338+ ASSERT_GT (wshortenable.size (), kMaxPath );
337339
338340 // Attempt to shorten. It will fail because the file doesn't exist yet.
339341 ASSERT_SHORTENING_FAILS (wshortenable, L" GetShortPathNameW" );
0 commit comments