@@ -85,9 +85,6 @@ int git_win32_path_canonicalize(git_win32_path path)
85
85
{
86
86
wchar_t * base , * from , * to , * next ;
87
87
size_t len ;
88
- size_t max_path_length = git_win32_longpaths_support
89
- ? GIT_WIN_PATH_UTF16
90
- : GIT_WIN_SHORT_PATH_UTF16 ;
91
88
92
89
base = to = path__skip_prefix (path );
93
90
@@ -144,22 +141,20 @@ int git_win32_path_canonicalize(git_win32_path path)
144
141
while (to > base && to [-1 ] == L'\\' ) to -- ;
145
142
146
143
* to = L'\0' ;
147
- len = to - path ;
148
- if (len >= 8 && wcsncmp (L"\\\\?\\UNC\\" , path , 8 ) != 0 ) {
149
- /* Not a UNC path, max length shorter by 2 */
150
- max_path_length -= 2 ;
151
- }
152
- if (len >= max_path_length ) {
144
+ if ((to - path ) > INT_MAX ) {
153
145
SetLastError (ERROR_FILENAME_EXCED_RANGE );
154
146
return -1 ;
155
147
}
156
148
157
- return (int )len ;
149
+ return (int )( to - path ) ;
158
150
}
159
151
160
152
int git_win32_path__cwd (wchar_t * out , size_t len )
161
153
{
162
154
int cwd_len ;
155
+ int max_path_length = git_win32_longpaths_support
156
+ ? WIN_GIT_PATH_MAX
157
+ : WIN_GIT_SHORT_PATH_MAX ;
163
158
164
159
if (len > INT_MAX ) {
165
160
errno = ENAMETOOLONG ;
@@ -176,7 +171,7 @@ int git_win32_path__cwd(wchar_t *out, size_t len)
176
171
* '\'s, but we we add a 'UNC' specifier to the path, plus
177
172
* a trailing directory separator, plus a NUL.
178
173
*/
179
- if (cwd_len > WIN_GIT_PATH_MAX - 4 ) {
174
+ if (cwd_len > max_path_length - 4 ) {
180
175
errno = ENAMETOOLONG ;
181
176
return -1 ;
182
177
}
@@ -193,7 +188,7 @@ int git_win32_path__cwd(wchar_t *out, size_t len)
193
188
* working directory. (One character for the directory separator,
194
189
* one for the null.
195
190
*/
196
- else if (cwd_len > WIN_GIT_PATH_MAX - 2 ) {
191
+ else if (cwd_len > max_path_length - 2 ) {
197
192
errno = ENAMETOOLONG ;
198
193
return -1 ;
199
194
}
@@ -204,20 +199,23 @@ int git_win32_path__cwd(wchar_t *out, size_t len)
204
199
int git_win32_path_from_utf8 (git_win32_path out , const char * src )
205
200
{
206
201
wchar_t * dest = out ;
202
+ size_t max_path_length = git_win32_longpaths_support
203
+ ? WIN_GIT_PATH_MAX
204
+ : WIN_GIT_SHORT_PATH_MAX ;
207
205
208
206
/* All win32 paths are in NT-prefixed format, beginning with "\\?\". */
209
207
memcpy (dest , PATH__NT_NAMESPACE , sizeof (wchar_t ) * PATH__NT_NAMESPACE_LEN );
210
208
dest += PATH__NT_NAMESPACE_LEN ;
211
209
212
210
/* See if this is an absolute path (beginning with a drive letter) */
213
211
if (git_path_is_absolute (src )) {
214
- if (git__utf8_to_16 (dest , WIN_GIT_PATH_MAX , src ) < 0 )
212
+ if (git__utf8_to_16 (dest , max_path_length , src ) < 0 )
215
213
goto on_error ;
216
214
}
217
215
/* File-prefixed NT-style paths beginning with \\?\ */
218
216
else if (path__is_nt_namespace (src )) {
219
217
/* Skip the NT prefix, the destination already contains it */
220
- if (git__utf8_to_16 (dest , WIN_GIT_PATH_MAX , src + PATH__NT_NAMESPACE_LEN ) < 0 )
218
+ if (git__utf8_to_16 (dest , max_path_length , src + PATH__NT_NAMESPACE_LEN ) < 0 )
221
219
goto on_error ;
222
220
}
223
221
/* UNC paths */
@@ -226,12 +224,12 @@ int git_win32_path_from_utf8(git_win32_path out, const char *src)
226
224
dest += 4 ;
227
225
228
226
/* Skip the leading "\\" */
229
- if (git__utf8_to_16 (dest , WIN_GIT_PATH_MAX - 2 , src + 2 ) < 0 )
227
+ if (git__utf8_to_16 (dest , max_path_length - 2 , src + 2 ) < 0 )
230
228
goto on_error ;
231
229
}
232
230
/* Absolute paths omitting the drive letter */
233
231
else if (src [0 ] == '\\' || src [0 ] == '/' ) {
234
- if (path__cwd (dest , WIN_GIT_PATH_MAX ) < 0 )
232
+ if (path__cwd (dest , ( int ) max_path_length ) < 0 )
235
233
goto on_error ;
236
234
237
235
if (!git_path_is_absolute (dest )) {
@@ -240,19 +238,19 @@ int git_win32_path_from_utf8(git_win32_path out, const char *src)
240
238
}
241
239
242
240
/* Skip the drive letter specification ("C:") */
243
- if (git__utf8_to_16 (dest + 2 , WIN_GIT_PATH_MAX - 2 , src ) < 0 )
241
+ if (git__utf8_to_16 (dest + 2 , max_path_length - 2 , src ) < 0 )
244
242
goto on_error ;
245
243
}
246
244
/* Relative paths */
247
245
else {
248
246
int cwd_len ;
249
247
250
- if ((cwd_len = git_win32_path__cwd (dest , WIN_GIT_PATH_MAX )) < 0 )
248
+ if ((cwd_len = git_win32_path__cwd (dest , max_path_length )) < 0 )
251
249
goto on_error ;
252
250
253
251
dest [cwd_len ++ ] = L'\\' ;
254
252
255
- if (git__utf8_to_16 (dest + cwd_len , WIN_GIT_PATH_MAX - cwd_len , src ) < 0 )
253
+ if (git__utf8_to_16 (dest + cwd_len , max_path_length - cwd_len , src ) < 0 )
256
254
goto on_error ;
257
255
}
258
256
@@ -298,16 +296,19 @@ char *git_win32_path_8dot3_name(const char *path)
298
296
wchar_t * start ;
299
297
char * shortname ;
300
298
int len , namelen = 1 ;
299
+ int max_path_utf16_length = git_win32_longpaths_support
300
+ ? GIT_WIN_PATH_UTF16
301
+ : GIT_WIN_SHORT_PATH_UTF16 ;
301
302
302
303
if (git_win32_path_from_utf8 (longpath , path ) < 0 )
303
304
return NULL ;
304
305
305
- len = GetShortPathNameW (longpath , shortpath , GIT_WIN_PATH_UTF16 );
306
+ len = GetShortPathNameW (longpath , shortpath , max_path_utf16_length );
306
307
307
308
while (len && shortpath [len - 1 ] == L'\\' )
308
309
shortpath [-- len ] = L'\0' ;
309
310
310
- if (len == 0 || len >= GIT_WIN_PATH_UTF16 )
311
+ if (len == 0 || len >= max_path_utf16_length )
311
312
return NULL ;
312
313
313
314
for (start = shortpath + (len - 1 );
@@ -343,6 +344,9 @@ int git_win32_path_readlink_w(git_win32_path dest, const git_win32_path path)
343
344
DWORD ioctl_ret ;
344
345
wchar_t * target ;
345
346
size_t target_len ;
347
+ size_t max_path_utf16_length = git_win32_longpaths_support
348
+ ? <
F438
span class=pl-c1>GIT_WIN_PATH_UTF16
349
+ : GIT_WIN_SHORT_PATH_UTF16 ;
346
350
347
351
int error = -1 ;
348
352
@@ -389,7 +393,7 @@ int git_win32_path_readlink_w(git_win32_path dest, const git_win32_path path)
389
393
390
394
/* Need one additional character in the target buffer
391
395
* for the terminating NULL. */
392
- if (GIT_WIN_PATH_UTF16 > target_len ) {
396
+ if (max_path_utf16_length > target_len ) {
393
397
wcscpy (dest , target );
394
398
error = (int )target_len ;
395
399
}
0 commit comments