@@ -88,9 +88,6 @@ int git_win32_path_canonicalize(git_win32_path path)
88
88
{
89
89
wchar_t * base , * from , * to , * next ;
90
90
size_t len ;
91
- size_t max_path_length = git_win32_longpaths_support
92
- ? GIT_WIN_PATH_UTF16
93
- : GIT_WIN_SHORT_PATH_UTF16 ;
94
91
95
92
base = to = path__skip_prefix (path );
96
93
<
D7AE
div class="d-flex flex-row">@@ -147,22 +144,20 @@ int git_win32_path_canonicalize(git_win32_path path)
147
144
while (to > base && to [-1 ] == L'\\' ) to -- ;
148
145
149
146
* to = L'\0' ;
150
- len = to - path ;
151
- if (len >= 8 && wcsncmp (L"\\\\?\\UNC\\" , path , 8 ) != 0 ) {
152
- /* Not a UNC path, max length shorter by 2 */
153
- max_path_length -= 2 ;
154
- }
155
- if (len >= max_path_length ) {
147
+ if ((to - path ) > INT_MAX ) {
156
148
SetLastError (ERROR_FILENAME_EXCED_RANGE );
157
149
return -1 ;
158
150
}
159
151
160
- return (int )len ;
152
+ return (int )( to - path ) ;
161
153
}
162
154
163
155
static int win32_path_cwd (wchar_t * out , size_t len )
164
156
{
165
157
int cwd_len ;
158
+ int max_path_length = git_win32_longpaths_support
159
+ ? WIN_GIT_PATH_MAX
160
+ : WIN_GIT_SHORT_PATH_MAX ;
166
161
167
162
if (len > INT_MAX ) {
168
163
errno = ENAMETOOLONG ;
@@ -179,7 +174,7 @@ static int win32_path_cwd(wchar_t *out, size_t len)
179
174
* '\'s, but we we add a 'UNC' specifier to the path, plus
180
175
* a trailing directory separator, plus a NUL.
181
176
*/
182
- if (cwd_len > WIN_GIT_PATH_MAX - 4 ) {
177
+ if (cwd_len > max_path_length - 4 ) {
183
178
errno = ENAMETOOLONG ;
184
179
return -1 ;
185
180
}
@@ -196,7 +191,7 @@ static int win32_path_cwd(wchar_t *out, size_t len)
196
191
* working directory. (One character for the directory separator,
197
192
* one for the null.
198
193
*/
199
- else if (cwd_len > WIN_GIT_PATH_MAX - 2 ) {
194
+ else if (cwd_len > max_path_length - 2 ) {
200
195
errno = ENAMETOOLONG ;
201
196
return -1 ;
202
197
}
@@ -207,20 +202,23 @@ static int win32_path_cwd(wchar_t *out, size_t len)
207
202
int git_win32_path_from_utf8 (git_win32_path out , const char * src )
208
203
{
209
204
wchar_t * dest = out ;
205
+ size_t max_path_length = git_win32_longpaths_support
206
+ ? WIN_GIT_PATH_MAX
207
+ : WIN_GIT_SHORT_PATH_MAX ;
210
208
211
209
/* All win32 paths are in NT-prefixed format, beginning with "\\?\". */
212
210
memcpy (dest , PATH__NT_NAMESPACE , sizeof (wchar_t ) * PATH__NT_NAMESPACE_LEN );
213
211
dest += PATH__NT_NAMESPACE_LEN ;
214
212
215
213
/* See if this is an absolute path (beginning with a drive letter) */
216
214
if (git_path_is_absolute (src )) {
217
- if (git__utf8_to_16 (dest , WIN_GIT_PATH_MAX , src ) < 0 )
215
+ if (git__utf8_to_16 (dest , max_path_length , src ) < 0 )
218
216
goto on_error ;
219
217
}
220
218
/* File-prefixed NT-style paths beginning with \\?\ */
221
219
else if (path__is_nt_namespace (src )) {
222
220
/* Skip the NT prefix, the destination already contains it */
223
- if (git__utf8_to_16 (dest , WIN_GIT_PATH_MAX , src + PATH__NT_NAMESPACE_LEN ) < 0 )
221
+ if (git__utf8_to_16 (dest , max_path_length , src + PATH__NT_NAMESPACE_LEN ) < 0 )
224
222
goto on_error ;
225
223
}
226
224
/* UNC paths */
@@ -229,12 +227,12 @@ int git_win32_path_from_utf8(git_win32_path out, const char *src)
229
227
dest += 4 ;
230
228
231
229
/* Skip the leading "\\" */
232
- if (git__utf8_to_16 (dest , WIN_GIT_PATH_MAX - 2 , src + 2 ) < 0 )
230
+ if (git__utf8_to_16 (dest , max_path_length - 2 , src + 2 ) < 0 )
233
231
goto on_error ;
234
232
}
235
233
/* Absolute paths omitting the drive letter */
236
234
else if (path__startswith_slash (src )) {
237
- if (path__cwd (dest , WIN_GIT_MAX_PATH ) < 0 )
235
+ if (path__cwd (dest , ( int ) max_path_length ) < 0 )
238
236
goto on_error ;
239
237
240
238
if (!git_path_is_absolute (dest )) {
@@ -243,19 +241,19 @@ int git_win32_path_from_utf8(git_win32_path out, const char *src)
243
241
}
244
242
245
243
/* Skip the drive letter specification ("C:") */
246
- if (git__utf8_to_16 (dest + 2 , WIN_GIT_PATH_MAX - 2 , src ) < 0 )
244
+ if (git__utf8_to_16 (dest + 2 , max_path_length - 2 , src ) < 0 )
247
245
goto on_error ;
248
246
}
249
247
/* Relative paths */
250
248
else {
251
249
int cwd_len ;
252
250
253
- if ((cwd_len = win32_path_cwd (dest , WIN_GIT_PATH_MAX )) < 0 )
251
+ if ((cwd_len = win32_path_cwd (dest , max_path_length )) < 0 )
254
252
goto on_error ;
255
253
256
254
dest [cwd_len ++ ] = L'\\' ;
257
255
258
- if (git__utf8_to_16 (dest + cwd_len , WIN_GIT_PATH_MAX - cwd_len , src ) < 0 )
256
+ if (git__utf8_to_16 (dest + cwd_len , max_path_length - cwd_len , src ) < 0 )
259
257
goto on_error ;
260
258
}
261
259
@@ -273,6 +271,9 @@ int git_win32_path_relative_from_utf8(git_win32_path out, const char *src)
273
271
{
274
272
wchar_t * dest = out , * p ;
275
273
int len ;
274
+ size_t max_path_length = git_win32_longpaths_support
275
+ ? WIN_GIT_PATH_MAX
276
+ : WIN_GIT_SHORT_PATH_MAX ;
276
277
277
278
/* Handle absolute paths */
278
279
if (git_path_is_absolute (src ) ||
@@ -282,7 +283,7 @@ int git_win32_path_relative_from_utf8(git_win32_path out, const char *src)
282
283
return git_win32_path_from_utf8 (out , src );
283
284
}
284
285
285
- if ((len = git__utf8_to_16 (dest , WIN_GIT_MAX_PATH , src )) < 0 )
286
+ if ((len = git__utf8_to_16 (dest , max_path_length , src )) < 0 )
286
287
return -1 ;
287
288
288
289
for (p = dest ; p < (dest + len ); p ++ ) {
@@ -325,16 +326,19 @@ char *git_win32_path_8dot3_name(const char *path)
325
326
wchar_t * start ;
326
327
char * shortname ;
327
328
int len , namelen = 1 ;
329
+ int max_path_utf16_length = git_win32_longpaths_support
330
+ ? GIT_WIN_PATH_UTF16
331
+ : GIT_WIN_SHORT_PATH_UTF16 ;
328
332
329
333
if (git_win32_path_from_utf8 (longpath , path ) < 0 )
330
334
return NULL ;
331
335
332
- len = GetShortPathNameW (longpath , shortpath , GIT_WIN_PATH_UTF16 );
336
+ len = GetShortPathNameW (longpath , shortpath , max_path_utf16_length );
333
337
334
338
while (len && shortpath [len - 1 ] == L'\\' )
335
339
shortpath [-- len ] = L'\0' ;
336
340
337
- if (len == 0 || len >= GIT_WIN_PATH_UTF16 )
341
+ if (len == 0 || len >= max_path_utf16_length )
338
342
return NULL ;
339
343
340
344
for (start = shortpath + (len - 1 );
@@ -370,6 +374,9 @@ int git_win32_path_readlink_w(git_win32_path dest, const git_win32_path path)
370
374
DWORD ioctl_ret ;
371
375
wchar_t * target ;
372
376
size_t target_len ;
377
+ size_t max_path_utf16_length = git_win32_longpaths_support
378
+ ? GIT_WIN_PATH_UTF16
379
+ : GIT_WIN_SHORT_PATH_UTF16 ;
373
380
374
381
int error = -1 ;
375
382
@@ -416,7 +423,7 @@ int git_win32_path_readlink_w(git_win32_path dest, const git_win32_path path)
416
423
417
424
/* Need one additional character in the target buffer
418
425
* for the terminating NULL. */
419
- if (GIT_WIN_PATH_UTF16 > target_len ) {
426
+ if (max_path_utf16_length > target_len ) {
420
427
wcscpy (dest , target );
421
428
error = (int )target_len ;
422
429
}
0 commit comments