Skip to content

Commit 15618fa

Browse files
committed
patch 8.0.0493: crash with cd command with very long argument
Problem: Crash with cd command with very long argument. Solution: Check for running out of space. (Dominique pending, closes #1576)
1 parent 81b9d0b commit 15618fa

5 files changed

Lines changed: 58 additions & 12 deletions

File tree

src/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2096,6 +2096,7 @@ test_arglist \
20962096
test_backspace_opt \
20972097
test_breakindent \
20982098
test_bufwintabinfo \
2099+
test_cd \
20992100
test_cdo \
21002101
test_changedtick \
21012102
test_channel \

src/misc2.c

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4637,13 +4637,23 @@ vim_findfile(void *search_ctx_arg)
46374637
if (!vim_isAbsName(stackp->ffs_fix_path)
46384638
&& search_ctx->ffsc_start_dir)
46394639
{
4640-
STRCPY(file_path, search_ctx->ffsc_start_dir);
4641-
add_pathsep(file_path);
4640+
if (STRLEN(search_ctx->ffsc_start_dir) + 1 < MAXPATHL)
4641+
{
4642+
STRCPY(file_path, search_ctx->ffsc_start_dir);
4643+
add_pathsep(file_path);
4644+
}
4645+
else
4646+
goto fail;
46424647
}
46434648

46444649
/* append the fix part of the search path */
4645-
STRCAT(file_path, stackp->ffs_fix_path);
4646-
add_pathsep(file_path);
4650+
if (STRLEN(file_path) + STRLEN(stackp->ffs_fix_path) + 1 < MAXPATHL)
4651+
{
4652+
STRCAT(file_path, stackp->ffs_fix_path);
4653+
add_pathsep(file_path);
4654+
}
4655+
else
4656+
goto fail;
46474657

46484658
#ifdef FEAT_PATH_EXTRA
46494659
rest_of_wildcards = stackp->ffs_wc_path;
@@ -4660,7 +4670,10 @@ vim_findfile(void *search_ctx_arg)
46604670
if (*p > 0)
46614671
{
46624672
(*p)--;
4663-
file_path[len++] = '*';
4673+
if (len + 1 < MAXPATHL)
4674+
file_path[len++] = '*';
4675+
else
4676+
goto fail;
46644677
}
46654678

46664679
if (*p == 0)
@@ -4688,7 +4701,10 @@ vim_findfile(void *search_ctx_arg)
46884701
*/
46894702
while (*rest_of_wildcards
46904703
&& !vim_ispathsep(*rest_of_wildcards))
4691-
file_path[len++] = *rest_of_wildcards++;
4704+
if (len + 1 < MAXPATHL)
4705+
file_path[len++] = *rest_of_wildcards++;
4706+
else
4707+
goto fail;
46924708

46934709
file_path[len] = NUL;
46944710
if (vim_ispathsep(*rest_of_wildcards))
@@ -4749,9 +4765,15 @@ vim_findfile(void *search_ctx_arg)
47494765

47504766
/* prepare the filename to be checked for existence
47514767
* below */
4752-
STRCPY(file_path, stackp->ffs_filearray[i]);
4753-
add_pathsep(file_path);
4754-
STRCAT(file_path, search_ctx->ffsc_file_to_search);
4768+
if (STRLEN(stackp->ffs_filearray[i]) + 1
4769+
+ STRLEN(search_ctx->ffsc_file_to_search) < MAXPATHL)
4770+
{
4771+
STRCPY(file_path, stackp->ffs_filearray[i]);
4772+
add_pathsep(file_path);
4773+
STRCAT(file_path, search_ctx->ffsc_file_to_search);
4774+
}
4775+
else
4776+
goto fail;
47554777

47564778
/*
47574779
* Try without extra suffix and then with suffixes
@@ -4924,9 +4946,15 @@ vim_findfile(void *search_ctx_arg)
49244946
if (*search_ctx->ffsc_start_dir == 0)
49254947
break;
49264948

4927-
STRCPY(file_path, search_ctx->ffsc_start_dir);
4928-
add_pathsep(file_path);
4929-
STRCAT(file_path, search_ctx->ffsc_fix_path);
4949+
if (STRLEN(search_ctx->ffsc_start_dir) + 1
4950+
+ STRLEN(search_ctx->ffsc_fix_path) < MAXPATHL)
4951+
{
4952+
STRCPY(file_path, search_ctx->ffsc_start_dir);
4953+
add_pathsep(file_path);
4954+
STRCAT(file_path, search_ctx->ffsc_fix_path);
4955+
}
4956+
else
4957+
goto fail;
49304958

49314959
/* create a new stack entry */
49324960
sptr = ff_create_stack_element(file_path,
@@ -4940,6 +4968,7 @@ vim_findfile(void *search_ctx_arg)
49404968
}
49414969
#endif
49424970

4971+
fail:
49434972
vim_free(file_path);
49444973
return NULL;
49454974
}

src/testdir/test_alot.vim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
set belloff=all
55
source test_assign.vim
6+
source test_cd.vim
67
source test_changedtick.vim
78
source test_cursor_func.vim
89
source test_delete.vim

src/testdir/test_cd.vim

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
" Test for :cd
2+
3+
func Test_cd_large_path()
4+
" This used to crash with a heap write overflow.
5+
call assert_fails('cd ' . repeat('x', 5000), 'E472:')
6+
endfunc
7+
8+
func Test_cd_up_and_down()
9+
let path = getcwd()
10+
cd ..
11+
exe 'cd ' . path
12+
call assert_equal(path, getcwd())
13+
endfunc

src/version.c

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

765765
static int included_patches[] =
766766
{ /* Add new patch number below this line */
767+
/**/
768+
493,
767769
/**/
768770
492,
769771
/**/

0 commit comments

Comments
 (0)