Skip to content

Commit 3c0f800

Browse files
kodareef5chrisbra
authored andcommitted
patch 9.2.0271: buffer underflow in vim_fgets()
Problem: buffer underflow in vim_fgets() Solution: Ensure size is always greater than 1 (Koda Reef) Signed-off-by: Koda Reef <[email protected]> Signed-off-by: Christian Brabandt <[email protected]>
1 parent 211ceea commit 3c0f800

4 files changed

Lines changed: 31 additions & 1 deletion

File tree

src/fileio.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3833,6 +3833,14 @@ vim_fgets(char_u *buf, int size, FILE *fp)
38333833
#define FGETS_SIZE 200
38343834
char tbuf[FGETS_SIZE];
38353835

3836+
// safety check
3837+
if (size < 2)
3838+
{
3839+
if (size == 1)
3840+
buf[0] = NUL;
3841+
return TRUE;
3842+
}
3843+
38363844
buf[size - 2] = NUL;
38373845
eof = fgets((char *)buf, size, fp);
38383846
if (buf[size - 2] != NUL && buf[size - 2] != '\n')

src/testdir/test_viminfo.vim

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,4 +1351,24 @@ func Test_viminfo_global_var()
13511351
let &viminfo = _viminfo
13521352
endfunc
13531353

1354+
func Test_viminfo_len_one()
1355+
let _viminfofile = &viminfofile
1356+
let &viminfofile=''
1357+
let viminfo_file = tempname()
1358+
call histadd('cmd', '" TEST')
1359+
defer delete(viminfo_file)
1360+
1361+
" Craft a viminfo entry with ^V1 length prefix (len == 1)
1362+
call writefile([
1363+
\ '*encoding=utf-8',
1364+
\ ':' .. "\x161" .. 'X',
1365+
\ ], viminfo_file, 'b')
1366+
1367+
" Should not crash or cause memory errors
1368+
exe 'rviminfo! ' .. viminfo_file
1369+
call assert_equal('" TEST', histget(':', -1))
1370+
1371+
let &viminfofile = _viminfofile
1372+
endfunc
1373+
13541374
" 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+
271,
737739
/**/
738740
270,
739741
/**/

src/viminfo.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ viminfo_readstring(
265265
if (virp->vir_line[off] == Ctrl_V && vim_isdigit(virp->vir_line[off + 1]))
266266
{
267267
len = atol((char *)virp->vir_line + off + 1);
268-
if (len > 0 && len < 1000000)
268+
if (len > 1 && len < 1000000)
269269
retval = lalloc(len, TRUE);
270270
if (retval == NULL)
271271
{

0 commit comments

Comments
 (0)