Skip to content

Commit

Permalink
patch 8.2.2515: memory access error when truncating an empty message
Browse files Browse the repository at this point in the history
Problem:    Memory access error when truncating an empty message.
Solution:   Check for an empty string. (Dominique Pellé, closes #7841)
  • Loading branch information
brammool committed Feb 14, 2021
1 parent 2379f87 commit 6281815
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/message.c
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,13 @@ trunc_string(
int i;
int n;

if (*s == NUL)
{
if (buflen > 0)
*buf = NUL;
return;
}

if (room_in < 3)
room = 0;
half = room / 2;
Expand Down
9 changes: 9 additions & 0 deletions src/message_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ test_trunc_string(void)
char_u *buf; /*allocated every time to find uninit errors */
char_u *s;

// Should not write anything to destination if buflen is 0.
trunc_string((char_u *)"", NULL, 1, 0);

// Truncating an empty string does nothing.
buf = alloc(1);
trunc_string((char_u *)"", buf, 1, 1);
assert(buf[0] == NUL);
vim_free(buf);

// in place
buf = alloc(40);
STRCPY(buf, "text");
Expand Down
2 changes: 2 additions & 0 deletions src/version.c
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,8 @@ static char *(features[]) =

static int included_patches[] =
{ /* Add new patch number below this line */
/**/
2515,
/**/
2514,
/**/
Expand Down

0 comments on commit 6281815

Please sign in to comment.