Skip to content

Commit 2813f38

Browse files
committed
patch 8.2.5072: using uninitialized value and freed memory in spell command
Problem: Using uninitialized value and freed memory in spell command. Solution: Initialize "attr". Check for empty line early.
1 parent f5465ff commit 2813f38

3 files changed

Lines changed: 24 additions & 3 deletions

File tree

src/spell.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1275,7 +1275,7 @@ spell_move_to(
12751275
char_u *line;
12761276
char_u *p;
12771277
char_u *endp;
1278-
hlf_T attr;
1278+
hlf_T attr = 0;
12791279
int len;
12801280
#ifdef FEAT_SYN_HL
12811281
int has_syntax = syntax_present(wp);
@@ -1308,6 +1308,8 @@ spell_move_to(
13081308

13091309
while (!got_int)
13101310
{
1311+
int empty_line;
1312+
13111313
line = ml_get_buf(wp->w_buffer, lnum, FALSE);
13121314

13131315
len = (int)STRLEN(line);
@@ -1340,7 +1342,9 @@ spell_move_to(
13401342
}
13411343

13421344
// Copy the line into "buf" and append the start of the next line if
1343-
// possible.
1345+
// possible. Note: this ml_get_buf() may make "line" invalid, check
1346+
// for empty line first.
1347+
empty_line = *skipwhite(line) == NUL;
13441348
STRCPY(buf, line);
13451349
if (lnum < wp->w_buffer->b_ml.ml_line_count)
13461350
spell_cat_line(buf + STRLEN(buf),
@@ -1487,7 +1491,7 @@ spell_move_to(
14871491
--capcol;
14881492

14891493
// But after empty line check first word in next line
1490-
if (*skipwhite(line) == NUL)
1494+
if (empty_line)
14911495
capcol = 0;
14921496
}
14931497

src/testdir/test_spell_utf8.vim

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -802,5 +802,20 @@ func Test_word_index()
802802
call delete('Xtmpfile')
803803
endfunc
804804

805+
func Test_check_empty_line()
806+
" This was using freed memory
807+
enew
808+
spellgood!
809+
norm z=
810+
norm yy
811+
sil! norm P]svc
812+
norm P]s
813+
814+
" set 'encoding' to clear the wordt list
815+
set enc=latin1
816+
set enc=utf-8
817+
bwipe!
818+
endfunc
819+
805820

806821
" vim: shiftwidth=2 sts=2 expandtab

src/version.c

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

735735
static int included_patches[] =
736736
{ /* Add new patch number below this line */
737+
/**/
738+
5072,
737739
/**/
738740
5071,
739741
/**/

0 commit comments

Comments
 (0)