mirror of
https://github.com/vim/vim.git
synced 2025-09-29 04:34:16 -04:00
patch 9.0.0220: invalid memory access with for loop over NULL string
Problem: Invalid memory access with for loop over NULL string. Solution: Make sure mb_ptr2len() consistently returns zero for NUL.
This commit is contained in:
@@ -1035,7 +1035,8 @@ EXTERN vimconv_T output_conv; // type of output conversion
|
|||||||
* (DBCS).
|
* (DBCS).
|
||||||
* The value is set in mb_init();
|
* The value is set in mb_init();
|
||||||
*/
|
*/
|
||||||
// length of char in bytes, including following composing chars
|
// Length of char in bytes, including any following composing chars.
|
||||||
|
// NUL has length zero.
|
||||||
EXTERN int (*mb_ptr2len)(char_u *p) INIT(= latin_ptr2len);
|
EXTERN int (*mb_ptr2len)(char_u *p) INIT(= latin_ptr2len);
|
||||||
|
|
||||||
// idem, with limit on string length
|
// idem, with limit on string length
|
||||||
|
21
src/mbyte.c
21
src/mbyte.c
@@ -1077,24 +1077,28 @@ dbcs_char2bytes(int c, char_u *buf)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* mb_ptr2len() function pointer.
|
* Get byte length of character at "*p". Returns zero when "*p" is NUL.
|
||||||
* Get byte length of character at "*p" but stop at a NUL.
|
* Used for mb_ptr2len() when 'encoding' latin.
|
||||||
* For UTF-8 this includes following composing characters.
|
|
||||||
* Returns 0 when *p is NUL.
|
|
||||||
*/
|
*/
|
||||||
int
|
int
|
||||||
latin_ptr2len(char_u *p)
|
latin_ptr2len(char_u *p)
|
||||||
{
|
{
|
||||||
return MB_BYTE2LEN(*p);
|
return *p == NUL ? 0 : 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Get byte length of character at "*p". Returns zero when "*p" is NUL.
|
||||||
|
* Used for mb_ptr2len() when 'encoding' DBCS.
|
||||||
|
*/
|
||||||
static int
|
static int
|
||||||
dbcs_ptr2len(
|
dbcs_ptr2len(char_u *p)
|
||||||
char_u *p)
|
|
||||||
{
|
{
|
||||||
int len;
|
int len;
|
||||||
|
|
||||||
// Check if second byte is not missing.
|
if (*p == NUL)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
// if the second byte is missing the length is 1
|
||||||
len = MB_BYTE2LEN(*p);
|
len = MB_BYTE2LEN(*p);
|
||||||
if (len == 2 && p[1] == NUL)
|
if (len == 2 && p[1] == NUL)
|
||||||
len = 1;
|
len = 1;
|
||||||
@@ -2105,6 +2109,7 @@ utf_ptr2len_len(char_u *p, int size)
|
|||||||
/*
|
/*
|
||||||
* Return the number of bytes the UTF-8 encoding of the character at "p" takes.
|
* Return the number of bytes the UTF-8 encoding of the character at "p" takes.
|
||||||
* This includes following composing characters.
|
* This includes following composing characters.
|
||||||
|
* Returns zero for NUL.
|
||||||
*/
|
*/
|
||||||
int
|
int
|
||||||
utfc_ptr2len(char_u *p)
|
utfc_ptr2len(char_u *p)
|
||||||
|
@@ -75,6 +75,18 @@ func Test_for_invalid()
|
|||||||
redraw
|
redraw
|
||||||
endfunc
|
endfunc
|
||||||
|
|
||||||
|
func Test_for_over_null_string()
|
||||||
|
let save_enc = &enc
|
||||||
|
set enc=iso8859
|
||||||
|
let cnt = 0
|
||||||
|
for c in test_null_string()
|
||||||
|
let cnt += 1
|
||||||
|
endfor
|
||||||
|
call assert_equal(0, cnt)
|
||||||
|
|
||||||
|
let &enc = save_enc
|
||||||
|
endfunc
|
||||||
|
|
||||||
func Test_readfile_binary()
|
func Test_readfile_binary()
|
||||||
new
|
new
|
||||||
call setline(1, ['one', 'two', 'three'])
|
call setline(1, ['one', 'two', 'three'])
|
||||||
|
@@ -735,6 +735,8 @@ static char *(features[]) =
|
|||||||
|
|
||||||
static int included_patches[] =
|
static int included_patches[] =
|
||||||
{ /* Add new patch number below this line */
|
{ /* Add new patch number below this line */
|
||||||
|
/**/
|
||||||
|
220,
|
||||||
/**/
|
/**/
|
||||||
219,
|
219,
|
||||||
/**/
|
/**/
|
||||||
|
Reference in New Issue
Block a user