8000 path: use proper size for win32 api calls · libgit2/libgit2@ec9cd39 · GitHub
[go: up one dir, main page]

Skip to content

Commit ec9cd39

Browse files
committed
path: use proper size for win32 api calls
1 parent 35656a8 commit ec9cd39

File tree

4 files changed

+35
-10
lines changed

4 files changed

+35
-10
lines changed

src/path.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#include <stdio.h>
2222
#include <ctype.h>
2323

24+
extern bool git_win32_longpaths_support;
25+
2426
static int dos_drive_prefix_length(const char *path)
2527
{
2628
int i;
@@ -1253,14 +1255,16 @@ int git_path_diriter_init(
12531255
static int diriter_update_paths(git_path_diriter *diriter)
12541256
{
12551257
size_t filename_len, path_len;
1256-
1258+
size_t max_path_length = git_win32_longpaths_support
1259+
? GIT_WIN_PATH_UTF16
1260+
: GIT_WIN_SHORT_PATH_UTF16;
12571261
filename_len = wcslen(diriter->current.cFileName);
12581262

12591263
if (GIT_ADD_SIZET_OVERFLOW(&path_len, diriter->parent_len, filename_len) ||
12601264
GIT_ADD_SIZET_OVERFLOW(&path_len, path_len, 2))
12611265
return -1;
12621266

1263-
if (path_len > GIT_WIN_PATH_UTF16) {
1267+
if (path_len > max_path_length) {
12641268
git_error_set(GIT_ERROR_FILESYSTEM,
12651269
"invalid path '%.*ls\\%ls' (path too long)",
12661270
diriter->parent_len, diriter->path, diriter->current.cFileName);

src/win32/path_w32.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -298,16 +298,19 @@ char *git_win32_path_8dot3_name(const char *path)
298298
wchar_t *start;
299299
char *shortname;
300300
int len, namelen = 1;
301+
int max_path_length = git_win32_longpaths_support
302+
? GIT_WIN_PATH_UTF16
303+
: GIT_WIN_SHORT_PATH_UTF16;
301304

302305
if (git_win32_path_from_utf8(longpath, path) < 0)
303306
return NULL;
304307

305-
len = GetShortPathNameW(longpath, shortpath, GIT_WIN_PATH_UTF16);
308+
len = GetShortPathNameW(longpath, shortpath, max_path_length);
306309

307310
while (len && shortpath[len-1] == L'\\')
308311
shortpath[--len] = L'\0';
309312

310-
if (len == 0 || len >= GIT_WIN_PATH_UTF16)
313+
if (len == 0 || len >= max_path_length)
311314
return NULL;
312315

313316
for (start = shortpath + (len - 1);
@@ -343,6 +346,9 @@ int git_win32_path_readlink_w(git_win32_path dest, const git_win32_path path)
343346
DWORD ioctl_ret;
344347
wchar_t *target;
345348
size_t target_len;
349+
size_t max_path_length = git_win32_longpaths_support
350+
? GIT_WIN_PATH_UTF16
351+
: GIT_WIN_SHORT_PATH_UTF16;
346352

347353
int error = -1;
348354

@@ -389,7 +395,7 @@ int git_win32_path_readlink_w(git_win32_path dest, const git_win32_path path)
389395

390396
/* Need one additional character in the target buffer
391397
* for the terminating NULL. */
392-
if (GIT_WIN_PATH_UTF16 > target_len) {
398+
if (max_path_length > target_len) {
393399
wcscpy(dest, target);
394400
error = (int)target_len;
395401
}

src/win32/posix_w32.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
unsigned long git_win32__createfile_sharemode =
4747
FILE_SHARE_READ | FILE_SHARE_WRITE;
4848
int git_win32__retries = 10;
49+
extern bool git_win32_longpaths_support;
4950

5051
GIT_INLINE(void) set_errno(void)
5152
{
@@ -630,7 +631,10 @@ int p_futimes(int fd, const struct p_timeval times[2])
630631
int p_getcwd(char *buffer_out, size_t size)
631632
{
632633
git_win32_path buf;
633-
wchar_t *cwd = _wgetcwd(buf, GIT_WIN_PATH_UTF16);
634+
int max_path_length = git_win32_longpaths_support
635+
? GIT_WIN_PATH_UTF16
636+
: GIT_WIN_SHORT_PATH_UTF16;
637+
wchar_t *cwd = _wgetcwd(buf, max_path_length);
634638

635639
if (!cwd)
636640
return -1;
@@ -656,6 +660,9 @@ static int getfinalpath_w(
656660
{
657661
HANDLE hFile;
658662
DWORD dwChars;
663+
DWORD max_path_length = git_win32_longpaths_support
664+
? GIT_WIN_PATH_UTF16
665+
: GIT_WIN_SHORT_PATH_UTF16;
659666

660667
/* Use FILE_FLAG_BACKUP_SEMANTICS so we can open a directory. Do not
661668
* specify FILE_FLAG_OPEN_REPARSE_POINT; we want to open a handle to the
@@ -667,10 +674,10 @@ static int getfinalpath_w(
667674
return -1;
668675

669676
/* Call GetFinalPathNameByHandle */
670-
dwChars = GetFinalPathNameByHandleW(hFile, dest, GIT_WIN_PATH_UTF16, FILE_NAME_NORMALIZED);
677+
dwChars = GetFinalPathNameByHandleW(hFile, dest, max_path_length, FILE_NAME_NORMALIZED);
671678
CloseHandle(hFile);
672679

673-
if (!dwChars || dwChars >= GIT_WIN_PATH_UTF16)
680+
if (!dwChars || dwChars >= max_path_length)
674681
return -1;
675682

676683
/* The path may be delivered to us with a namespace prefix; remove */
@@ -773,14 +780,17 @@ int p_rmdir(const char* path)
773780
char *p_realpath(const char *orig_path, char *buffer)
774781
{
775782
git_win32_path orig_path_w, buffer_w;
783+
DWORD max_path_length = git_win32_longpaths_support
784+
? GIT_WIN_PATH_UTF16
785+
: GIT_WIN_SHORT_PATH_UTF16;
776786

777787
if (git_win32_path_from_utf8(orig_path_w, orig_path) < 0)
778788
return NULL;
779789

780790
/* Note that if the path provided is a relative path, then the current directory
781791
* is used to resolve the path -- which is a concurrency issue because the current
782792
* directory is a process-wide variable. */
783-
if (!GetFullPathNameW(orig_path_w, GIT_WIN_PATH_UTF16, buffer_w, NULL)) {
793+
if (!GetFullPathNameW(orig_path_w, max_path_length, buffer_w, NULL)) {
784794
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
785795
errno = ENAMETOOLONG;
786796
else

src/win32/w32_util.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
#include "w32_util.h"
99

10+
extern bool git_win32_longpaths_support;
11+
1012
/**
1113
* Creates a FindFirstFile(Ex) filter string from a UTF-8 path.
1214
* The filter string enumerates all items in the directory.
@@ -19,6 +21,9 @@ bool git_win32__findfirstfile_filter(git_win32_path dest, const char *src)
1921
{
2022
static const wchar_t suffix[] = L"\\*";
2123
int len = git_win32_path_from_utf8(dest, src);
24+
size_t max_path_length = git_win32_longpaths_support
25+
? GIT_WIN_PATH_UTF16
26+
: GIT_WIN_SHORT_PATH_UTF16;
2227

2328
/* Ensure the path was converted */
2429
if (len < 0)
@@ -35,7 +40,7 @@ bool git_win32__findfirstfile_filter(git_win32_path dest, const char *src)
3540
}
3641

3742
/* Ensure we have enough room to add the suffix */
38-
if ((size_t)len >= GIT_WIN_PATH_UTF16 - CONST_STRLEN(suffix))
43+
if ((size_t)len >= max_path_length - CONST_STRLEN(suffix))
3944
return false;
4045

4146
wcscat(dest, suffix);

0 commit comments

Comments
 (0)
0