Skip to content

Commit 5e59ea5

Browse files
committed
patch 9.0.0021: invalid memory access when adding word to spell word list
Problem: Invalid memory access when adding word with a control character to the internal spell word list. Solution: Disallow adding a word with control characters or a trailing slash.
1 parent f12129f commit 5e59ea5

3 files changed

Lines changed: 36 additions & 2 deletions

File tree

src/spellfile.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4366,6 +4366,23 @@ wordtree_alloc(spellinfo_T *spin)
43664366
return (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
43674367
}
43684368

4369+
/*
4370+
* Return TRUE if "word" contains valid word characters.
4371+
* Control characters and trailing '/' are invalid. Space is OK.
4372+
*/
4373+
static int
4374+
valid_spell_word(char_u *word)
4375+
{
4376+
char_u *p;
4377+
4378+
if (enc_utf8 && !utf_valid_string(word, NULL))
4379+
return FALSE;
4380+
for (p = word; *p != NUL; p += mb_ptr2len(p))
4381+
if (*p < ' ' || (p[0] == '/' && p[1] == NUL))
4382+
return FALSE;
4383+
return TRUE;
4384+
}
4385+
43694386
/*
43704387
* Store a word in the tree(s).
43714388
* Always store it in the case-folded tree. For a keep-case word this is
@@ -4391,7 +4408,7 @@ store_word(
43914408
char_u *p;
43924409

43934410
// Avoid adding illegal bytes to the word tree.
4394-
if (enc_utf8 && !utf_valid_string(word, NULL))
4411+
if (!valid_spell_word(word))
43954412
return FAIL;
43964413

43974414
(void)spell_casefold(curwin, word, len, foldword, MAXWLEN);
@@ -6194,7 +6211,7 @@ spell_add_word(
61946211
int i;
61956212
char_u *spf;
61966213

6197-
if (enc_utf8 && !utf_valid_string(word, NULL))
6214+
if (!valid_spell_word(word))
61986215
{
61996216
emsg(_(e_illegal_character_in_word));
62006217
return;

src/testdir/test_spell.vim

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,21 @@ func Test_spellsuggest_too_deep()
854854
bwipe!
855855
endfunc
856856

857+
func Test_spell_good_word_invalid()
858+
" This was adding a word with a 0x02 byte, which causes havoc.
859+
enew
860+
norm o0
861+
sil! norm rzzWs00/
862+
2
863+
sil! norm VzGprzzW
864+
sil! norm z=
865+
866+
bwipe!
867+
" clear the internal word list
868+
set enc=latin1
869+
set enc=utf-8
870+
endfunc
871+
857872
func LoadAffAndDic(aff_contents, dic_contents)
858873
set enc=latin1
859874
set spellfile=

src/version.c

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

736736
static int included_patches[] =
737737
{ /* Add new patch number below this line */
738+
/**/
739+
21,
738740
/**/
739741
20,
740742
/**/

0 commit comments

Comments
 (0)