Skip to content

Commit 07399e7

Browse files
committed
patch 8.2.1521: reading past end of buffer when reading spellfile
Problem: Reading past end of buffer when reading spellfile. (Yegappan Lakshmanan) Solution: Store the byte length and check for it.
1 parent b3ea36c commit 07399e7

3 files changed

Lines changed: 14 additions & 7 deletions

File tree

src/spell.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ struct slang_S
6666
int sl_add; // TRUE if it's a .add file.
6767

6868
char_u *sl_fbyts; // case-folded word bytes
69+
long sl_fbyts_len; // length of sl_fbyts
6970
idx_T *sl_fidxs; // case-folded word indexes
7071
char_u *sl_kbyts; // keep-case word bytes
7172
idx_T *sl_kidxs; // keep-case word indexes

src/spellfile.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ static int read_compound(FILE *fd, slang_T *slang, int len);
315315
static int set_sofo(slang_T *lp, char_u *from, char_u *to);
316316
static void set_sal_first(slang_T *lp);
317317
static int *mb_str2wide(char_u *s);
318-
static int spell_read_tree(FILE *fd, char_u **bytsp, idx_T **idxsp, int prefixtree, int prefixcnt);
318+
static int spell_read_tree(FILE *fd, char_u **bytsp, long *bytsp_len, idx_T **idxsp, int prefixtree, int prefixcnt);
319319
static idx_T read_tree_node(FILE *fd, char_u *byts, idx_T *idxs, int maxidx, idx_T startidx, int prefixtree, int maxprefcondnr);
320320
static void set_spell_charflags(char_u *flags, int cnt, char_u *upp);
321321
static int set_spell_chartab(char_u *fol, char_u *low, char_u *upp);
@@ -553,17 +553,18 @@ spell_load_file(
553553
}
554554

555555
// <LWORDTREE>
556-
res = spell_read_tree(fd, &lp->sl_fbyts, &lp->sl_fidxs, FALSE, 0);
556+
res = spell_read_tree(fd, &lp->sl_fbyts, &lp->sl_fbyts_len,
557+
&lp->sl_fidxs, FALSE, 0);
557558
if (res != 0)
558559
goto someerror;
559560

560561
// <KWORDTREE>
561-
res = spell_read_tree(fd, &lp->sl_kbyts, &lp->sl_kidxs, FALSE, 0);
562+
res = spell_read_tree(fd, &lp->sl_kbyts, NULL, &lp->sl_kidxs, FALSE, 0);
562563
if (res != 0)
563564
goto someerror;
564565

565566
// <PREFIXTREE>
566-
res = spell_read_tree(fd, &lp->sl_pbyts, &lp->sl_pidxs, TRUE,
567+
res = spell_read_tree(fd, &lp->sl_pbyts, NULL, &lp->sl_pidxs, TRUE,
567568
lp->sl_prefixcnt);
568569
if (res != 0)
569570
goto someerror;
@@ -737,7 +738,7 @@ suggest_load_files(void)
737738
* <SUGWORDTREE>: <wordtree>
738739
* Read the trie with the soundfolded words.
739740
*/
740-
if (spell_read_tree(fd, &slang->sl_sbyts, &slang->sl_sidxs,
741+
if (spell_read_tree(fd, &slang->sl_sbyts, NULL, &slang->sl_sidxs,
741742
FALSE, 0) != 0)
742743
{
743744
someerror:
@@ -1572,6 +1573,7 @@ mb_str2wide(char_u *s)
15721573
spell_read_tree(
15731574
FILE *fd,
15741575
char_u **bytsp,
1576+
long *bytsp_len,
15751577
idx_T **idxsp,
15761578
int prefixtree, // TRUE for the prefix tree
15771579
int prefixcnt) // when "prefixtree" is TRUE: prefix count
@@ -1596,6 +1598,8 @@ spell_read_tree(
15961598
if (bp == NULL)
15971599
return SP_OTHERERROR;
15981600
*bytsp = bp;
1601+
if (bytsp_len != NULL)
1602+
*bytsp_len = len;
15991603

16001604
// Allocate the index array.
16011605
ip = lalloc_clear(len * sizeof(int), TRUE);
@@ -5609,8 +5613,8 @@ sug_filltree(spellinfo_T *spin, slang_T *slang)
56095613
spin->si_blocks_cnt = 0;
56105614

56115615
// Skip over any other NUL bytes (same word with different
5612-
// flags).
5613-
while (byts[n + 1] == 0)
5616+
// flags). But don't go over the end.
5617+
while (n + 1 < slang->sl_fbyts_len && byts[n + 1] == 0)
56145618
{
56155619
++n;
56165620
++curi[depth];

src/version.c

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

755755
static int included_patches[] =
756756
{ /* Add new patch number below this line */
757+
/**/
758+
1521,
757759
/**/
758760
1520,
759761
/**/

0 commit comments

Comments
 (0)