Skip to content

Commit 8662189

Browse files
committed
patch 8.1.1095: MS-Windows: executable() fails on very long filename
Problem: MS-Windows: executable() fails on very long filename. Solution: (Ken Takata, closes #4015)
1 parent 5209334 commit 8662189

3 files changed

Lines changed: 21 additions & 5 deletions

File tree

src/os_win32.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3299,14 +3299,17 @@ mch_writable(char_u *name)
32993299
int
33003300
mch_can_exe(char_u *name, char_u **path, int use_path)
33013301
{
3302-
char_u buf[_MAX_PATH];
3302+
// WinNT and later can use _MAX_PATH wide characters for a pathname, which
3303+
// means that the maximum pathname is _MAX_PATH * 3 bytes when 'enc' is
3304+
// UTF-8.
3305+
char_u buf[_MAX_PATH * 3];
33033306
int len = (int)STRLEN(name);
33043307
char_u *p, *saved;
33053308

3306-
if (len >= _MAX_PATH) /* safety check */
3309+
if (len >= sizeof(buf)) // safety check
33073310
return FALSE;
33083311

3309-
/* Ty using the name directly when a Unix-shell like 'shell'. */
3312+
// Try using the name directly when a Unix-shell like 'shell'.
33103313
if (strstr((char *)gettail(p_sh), "sh") != NULL)
33113314
if (executable_exists((char *)name, path, use_path))
33123315
return TRUE;
@@ -3339,7 +3342,7 @@ mch_can_exe(char_u *name, char_u **path, int use_path)
33393342
}
33403343
vim_free(saved);
33413344

3342-
vim_strncpy(buf, name, _MAX_PATH - 1);
3345+
vim_strncpy(buf, name, sizeof(buf) - 1);
33433346
p = mch_getenv("PATHEXT");
33443347
if (p == NULL)
33453348
p = (char_u *)".com;.exe;.bat;.cmd";
@@ -3354,7 +3357,7 @@ mch_can_exe(char_u *name, char_u **path, int use_path)
33543357
++p;
33553358
}
33563359
else
3357-
copy_option_part(&p, buf + len, _MAX_PATH - len, ";");
3360+
copy_option_part(&p, buf + len, sizeof(buf) - len, ";");
33583361
if (executable_exists((char *)buf, path, use_path))
33593362
return TRUE;
33603363
}

src/testdir/test_functions.vim

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -944,6 +944,17 @@ func Test_Executable()
944944
endif
945945
endfunc
946946

947+
func Test_executable_longname()
948+
if !has('win32')
949+
return
950+
endif
951+
952+
let fname = 'X' . repeat('', 200) . '.bat'
953+
call writefile([], fname)
954+
call assert_equal(1, executable(fname))
955+
call delete(fname)
956+
endfunc
957+
947958
func Test_hostname()
948959
let hostname_vim = hostname()
949960
if has('unix')

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,8 @@ static char *(features[]) =
771771

772772
static int included_patches[] =
773773
{ /* Add new patch number below this line */
774+
/**/
775+
1095,
774776
/**/
775777
1094,
776778
/**/

0 commit comments

Comments
 (0)