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

Skip to content

Commit dcb8bef

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

File tree

4 files changed

+21
-10
lines changed

4 files changed

+21
-10
lines changed

src/path.c

Lines changed: 4 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,14 @@ 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 ? GIT_WIN_PATH_UTF16 : GIT_WIN_SHORT_PATH_UTF16;
12571259
filename_len = wcslen(diriter->current.cFileName);
12581260

12591261
if (GIT_ADD_SIZET_OVERFLOW(&path_len, diriter->parent_len, filename_len) ||
12601262
GIT_ADD_SIZET_OVERFLOW(&path_len, path_len, 2))
12611263
return -1;
12621264

1263-
if (path_len > GIT_WIN_PATH_UTF16) {
1265+
if (path_len > max_path_length) {
12641266
git_error_set(GIT_ERROR_FILESYSTEM,
12651267
"invalid path '%.*ls\\%ls' (path too long)",
12661268
diriter->parent_len, diriter->path, diriter->current.cFileName);

src/win32/path_w32.c

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

302303
if (git_win32_path_from_utf8(longpath, path) < 0)
303304
return NULL;
304305

305-
len = GetShortPathNameW(longpath, shortpath, GIT_WIN_PATH_UTF16);
306+
len = GetShortPathNameW(longpath, shortpath, max_path_length);
306307

307308
while (len && shortpath[len-1] == L'\\')
308309
shortpath[--len] = L'\0';
309310

310-
if (len == 0 || len >= GIT_WIN_PATH_UTF16)
311+
if (len == 0 || len >= max_path_length)
311312
return NULL;
312313

313314
for (start = shortpath + (len - 1);
@@ -343,6 +344,7 @@ int git_win32_path_readlink_w(git_win32_path dest, const git_win32_path path)
343344
DWORD ioctl_ret;
344345
wchar_t *target;
345346
size_t target_len;
347+
size_t max_path_length = git_win32_longpaths_support ? GIT_WIN_PATH_UTF16 : GIT_WIN_SHORT_PATH_UTF16;
346348

347349
int error = -1;
348350

@@ -389,7 +391,7 @@ int git_win32_path_readlink_w(git_win32_path dest, const git_win32_path path)
389391

390392
/* Need one additional character in the target buffer
391393
* for the terminating NULL. */
392-
if (GIT_WIN_PATH_UTF16 > target_len) {
394+
if (max_path_length > target_len) {
393395
wcscpy(dest, target);
394396
error = (int)target_len;
395397
}

src/win32/posix_w32.c

Lines changed: 8 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,8 @@ 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+
size_t max_path_length = git_win32_longpaths_support ? GIT_WIN_PATH_UTF16 : GIT_WIN_SHORT_PATH_UTF16;
635+
wchar_t *cwd = _wgetcwd(buf, max_path_length);
634636

635637
if (!cwd)
636638
return -1;
@@ -656,6 +658,7 @@ static int getfinalpath_w(
656658
{
657659
HANDLE hFile;
658660
DWORD dwChars;
661+
size_t max_path_length = git_win32_longpaths_support ? GIT_WIN_PATH_UTF16 : GIT_WIN_SHORT_PATH_UTF16;
659662

660663
/* Use FILE_FLAG_BACKUP_SEMANTICS so we can open a directory. Do not
661664
* specify FILE_FLAG_OPEN_REPARSE_POINT; we want to open a handle to the
@@ -667,10 +670,10 @@ static int getfinalpath_w(
667670
return -1;
668671

669672
/* Call GetFinalPathNameByHandle */
670-
dwChars = GetFinalPathNameByHandleW(hFile, dest, GIT_WIN_PATH_UTF16, FILE_NAME_NORMALIZED);
673+
dwChars = GetFinalPathNameByHandleW(hFile, dest, max_path_length, FILE_NAME_NORMALIZED);
671674
CloseHandle(hFile);
672675

673-
if (!dwChars || dwChars >= GIT_WIN_PATH_UTF16)
676+
if (!dwChars || dwChars >= max_path_length)
674677
return -1;
675678

676679
/* The path may be delivered to us with a namespace prefix; remove */
@@ -773,14 +776,15 @@ int p_rmdir(const char* path)
773776
char *p_realpath(const char *orig_path, char *buffer)
774777
{
775778
git_win32_path orig_path_w, buffer_w;
779+
size_t max_path_length = git_win32_longpaths_support ? GIT_WIN_PATH_UTF16 : GIT_WIN_SHORT_PATH_UTF16;
776780

777781
if (git_win32_path_from_utf8(orig_path_w, orig_path) < 0)
778782
return NULL;
779783

780784
/* Note that if the path provided is a relative path, then the current directory
781785
* is used to resolve the path -- which is a concurrency issue because the current
782786
* directory is a process-wide variable. */
783-
if (!GetFullPathNameW(orig_path_w, GIT_WIN_PATH_UTF16, buffer_w, NULL)) {
787+
if (!GetFullPathNameW(orig_path_w, max_path_length, buffer_w, NULL)) {
784788
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
785789
errno = ENAMETOOLONG;
786790
else

src/win32/w32_util.c

Lines changed: 4 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,7 @@ 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 ? GIT_WIN_PATH_UTF16 : GIT_WIN_SHORT_PATH_UTF16;
2225

2326
/* Ensure the path was converted */
2427
if (len < 0)
@@ -35,7 +38,7 @@ bool git_win32__findfirstfile_filter(git_win32_path dest, const char *src)
3538
}
3639

3740
/* Ensure we have enough room to add the suffix */
38-
if ((size_t)len >= GIT_WIN_PATH_UTF16 - CONST_STRLEN(suffix))
41+
if ((size_t)len >= max_path_length - CONST_STRLEN(suffix))
3942
return false;
4043

4144
wcscat(dest, suffix);

0 commit comments

Comments
 (0)
0