Skip to content

Commit 69c65b9

Browse files
ianhattendorfethomson
authored andcommitted
path: bump most Win32 unicode buffer sizes from MAX_PATH to GIT_PATH_MAX
1 parent 5368be3 commit 69c65b9

File tree

4 files changed

+33
-46
lines changed

4 files changed

+33
-46
lines changed

src/win32/path_w32.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ static int win32_path_cwd(wchar_t *out, size_t len)
170170
* '\'s, but we we add a 'UNC' specifier to the path, plus
171171
* a trailing directory separator, plus a NUL.
172172
*/
173-
if (cwd_len > MAX_PATH - 4) {
173+
if (cwd_len > GIT_WIN_PATH_MAX - 4) {
174174
errno = ENAMETOOLONG;
175175
return -1;
176176
}
@@ -187,7 +187,7 @@ static int win32_path_cwd(wchar_t *out, size_t len)
187187
* working directory. (One character for the directory separator,
188188
* one for the null.
189189
*/
190-
else if (cwd_len > MAX_PATH - 2) {
190+
else if (cwd_len > GIT_WIN_PATH_MAX - 2) {
191191
errno = ENAMETOOLONG;
192192
return -1;
193193
}
@@ -205,13 +205,13 @@ int git_win32_path_from_utf8(git_win32_path out, const char *src)
205205

206206
/* See if this is an absolute path (beginning with a drive letter) */
207207
if (git_path_is_absolute(src)) {
208-
if (git__utf8_to_16(dest, MAX_PATH, src) < 0)
208+
if (git__utf8_to_16(dest, GIT_WIN_PATH_MAX, src) < 0)
209209
goto on_error;
210210
}
211211
/* File-prefixed NT-style paths beginning with \\?\ */
212212
else if (path__is_nt_namespace(src)) {
213213
/* Skip the NT prefix, the destination already contains it */
214-
if (git__utf8_to_16(dest, MAX_PATH, src + PATH__NT_NAMESPACE_LEN) < 0)
214+
if (git__utf8_to_16(dest, GIT_WIN_PATH_MAX, src + PATH__NT_NAMESPACE_LEN) < 0)
215215
goto on_error;
216216
}
217217
/* UNC paths */
@@ -220,12 +220,12 @@ int git_win32_path_from_utf8(git_win32_path out, const char *src)
220220
dest += 4;
221221

222222
/* Skip the leading "\\" */
223-
if (git__utf8_to_16(dest, MAX_PATH - 2, src + 2) < 0)
223+
if (git__utf8_to_16(dest, GIT_WIN_PATH_MAX - 2, src + 2) < 0)
224224
goto on_error;
225225
}
226226
/* Absolute paths omitting the drive letter */
227227
else if (path__startswith_slash(src)) {
228-
if (path__cwd(dest, MAX_PATH) < 0)
228+
if (path__cwd(dest, GIT_WIN_PATH_MAX) < 0)
229229
goto on_error;
230230

231231
if (!git_path_is_absolute(dest)) {
@@ -234,19 +234,19 @@ int git_win32_path_from_utf8(git_win32_path out, const char *src)
234234
}
235235

236236
/* Skip the drive letter specification ("C:") */
237-
if (git__utf8_to_16(dest + 2, MAX_PATH - 2, src) < 0)
237+
if (git__utf8_to_16(dest + 2, GIT_WIN_PATH_MAX - 2, src) < 0)
238238
goto on_error;
239239
}
240240
/* Relative paths */
241241
else {
242242
int cwd_len;
243243

244-
if ((cwd_len = win32_path_cwd(dest, MAX_PATH)) < 0)
244+
if ((cwd_len = win32_path_cwd(dest, GIT_WIN_PATH_MAX)) < 0)
245245
goto on_error;
246246

247247
dest[cwd_len++] = L'\\';
248248

249-
if (git__utf8_to_16(dest + cwd_len, MAX_PATH - cwd_len, src) < 0)
249+
if (git__utf8_to_16(dest + cwd_len, GIT_WIN_PATH_MAX - cwd_len, src) < 0)
250250
goto on_error;
251251
}
252252

@@ -273,7 +273,7 @@ int git_win32_path_relative_from_utf8(git_win32_path out, const char *src)
273273
return git_win32_path_from_utf8(out, src);
274274
}
275275

276-
if ((len = git__utf8_to_16(dest, MAX_PATH, src)) < 0)
276+
if ((len = git__utf8_to_16(dest, GIT_WIN_PATH_MAX, src)) < 0)
277277
return -1;
278278

279279
for (p = dest; p < (dest + len); p++) {

src/win32/w32_common.h

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,33 @@
88
#ifndef INCLUDE_win32_w32_common_h__
99
#define INCLUDE_win32_w32_common_h__
1010

11+
#include <git2/common.h>
12+
13+
/*
14+
* 4096 is the max allowed Git path. `MAX_PATH` (260) is the typical max allowed
15+
* Windows path length, however win32 Unicode APIs generally allow up to 32,767
16+
* if prefixed with "\\?\" (i.e. converted to an NT-style name).
17+
*/
18+
#define GIT_WIN_PATH_MAX GIT_PATH_MAX
19+
1120
/*
12-
* Provides a large enough buffer to support Windows paths: MAX_PATH is
13-
* 260, corresponding to a maximum path length of 259 characters plus a
14-
* NULL terminator. Prefixing with "\\?\" adds 4 characters, but if the
15-
* original was a UNC path, then we turn "\\server\share" into
21+
* Provides a large enough buffer to support Windows Git paths:
22+
* GIT_WIN_PATH_MAX is 4096, corresponding to a maximum path length of 4095
23+
* characters plus a NULL terminator. Prefixing with "\\?\" adds 4 characters,
24+
* but if the original was a UNC path, then we turn "\\server\share" into
1625
* "\\?\UNC\server\share". So we replace the first two characters with
17-
* 8 characters, a net gain of 6, so the maximum length is MAX_PATH+6.
26+
* 8 characters, a net gain of 6, so the maximum length is GIT_WIN_PATH_MAX+6.
1827
*/
19-
#define GIT_WIN_PATH_UTF16 MAX_PATH+6
28+
#define GIT_WIN_PATH_UTF16 GIT_WIN_PATH_MAX+6
2029

21-
/* Maximum size of a UTF-8 Win32 path. We remove the "\\?\" or "\\?\UNC\"
22-
* prefixes for presentation, bringing us back to 259 (non-NULL)
30+
/* Maximum size of a UTF-8 Win32 Git path. We remove the "\\?\" or "\\?\UNC\"
31+
* prefixes for presentation, bringing us back to 4095 (non-NULL)
2332
* characters. UTF-8 does have 4-byte sequences, but they are encoded in
2433
* UTF-16 using surrogate pairs, which takes up the space of two characters.
2534
* Two characters in the range U+0800 -> U+FFFF take up more space in UTF-8
2635
* (6 bytes) than one surrogate pair (4 bytes).
2736
*/
28-
#define GIT_WIN_PATH_UTF8 (259 * 3 + 1)
37+
#define GIT_WIN_PATH_UTF8 ((GIT_WIN_PATH_MAX - 1) * 3 + 1)
2938

3039
/*
3140
* The length of a Windows "shortname", for 8.3 compatibility.

tests/path/win32.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,11 @@ void test_path_win32__honors_max_path(void)
7878
#ifdef GIT_WIN32
7979
git_win32_path path_utf16;
8080

81-
test_utf8_to_utf16("C:\\This path is 259 chars and is the max length in windows\\0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij",
82-
L"\\\\?\\C:\\This path is 259 chars and is the max length in windows\\0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij");
83-
test_utf8_to_utf16("\\\\unc\\paths may also be 259 characters including the server\\123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij",
84-
L"\\\\?\\UNC\\unc\\paths may also be 259 characters including the server\\123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij");
81+
test_utf8_to_utf16("C:\\This path is 261 characters which is fine for our path handling functions which cope with paths longer than MAX_PATH\\0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghijk",
82+
L"\\\\?\\C:\\This path is 261 characters which is fine for our path handling functions which cope with paths longer than MAX_PATH\\0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghijk");
83+
84+
cl_check_fail(git_win32_path_from_utf8(path_utf16, "C:\\This path is 4097 chars and exceeds our maximum path length on Windows which is limited to 4096 characters\\alas\\0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij01"));
8585

86-
cl_check_fail(git_win32_path_from_utf8(path_utf16, "C:\\This path is 260 chars and is sadly too long for windows\\0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij"));
87-
cl_check_fail(git_win32_path_from_utf8(path_utf16, "\\\\unc\\paths are also bound by 260 character restrictions\\including the server name portion\\bcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij"));
8886
#endif
8987
}
9088

tests/win32/longpath.c

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,33 +33,13 @@ void test_win32_longpath__cleanup(void)
3333
cl_git_sandbox_cleanup();
3434
}
3535

36-
#ifdef GIT_WIN32
37-
void assert_name_too_long(void)
38-
{
39-
const git_error *err;
40-
size_t expected_len, actual_len;
41-
char *expected_msg;
42-
43-
err = git_error_last();
44-
actual_len = strlen(err->message);
45-
46-
expected_msg = git_win32_get_error_message(ERROR_FILENAME_EXCED_RANGE);
47-
expected_len = strlen(expected_msg);
48-
49-
/* check the suffix */
50-
cl_assert_equal_s(expected_msg, err->message + (actual_len - expected_len));
51-
52-
git__free(expected_msg);
53-
}
54-
#endif
55-
5636
void test_win32_longpath__errmsg_on_checkout(void)
5737
{
5838
#ifdef GIT_WIN32
5939
git_repository *repo;
6040

6141
cl_git_fail(git_clone(&repo, cl_fixture("testrepo.git"), path.ptr, NULL));
62-
assert_name_too_long();
42+
cl_assert(git__prefixcmp(git_error_last()->message, "path too long") == 0);
6343
#endif
6444
}
6545

0 commit comments

Comments
 (0)