1
0
forked from aniani/vim

patch 8.1.0013: using freed memory when changing terminal cursor color

Problem:    Using freed memory when changing terminal cursor color.
Solution:   Make a copy of the color. (Dominique Pelle, closes #2938,
            closes #2941)
This commit is contained in:
Bram Moolenaar
2018-05-21 14:55:28 +02:00
parent 1f131aee0e
commit 4f7fd56b42
2 changed files with 33 additions and 21 deletions

View File

@@ -171,8 +171,8 @@ static int term_default_cterm_bg = -1;
/* Store the last set and the desired cursor properties, so that we only update /* Store the last set and the desired cursor properties, so that we only update
* them when needed. Doing it unnecessary may result in flicker. */ * them when needed. Doing it unnecessary may result in flicker. */
static char_u *last_set_cursor_color = (char_u *)""; static char_u *last_set_cursor_color = NULL;
static char_u *desired_cursor_color = (char_u *)""; static char_u *desired_cursor_color = NULL;
static int last_set_cursor_shape = -1; static int last_set_cursor_shape = -1;
static int desired_cursor_shape = -1; static int desired_cursor_shape = -1;
static int last_set_cursor_blink = -1; static int last_set_cursor_blink = -1;
@@ -183,6 +183,28 @@ static int desired_cursor_blink = -1;
* 1. Generic code for all systems. * 1. Generic code for all systems.
*/ */
static void
cursor_color_copy(char_u** to_color, char_u* from_color)
{
vim_free(*to_color);
*to_color = (from_color == NULL) ? NULL : vim_strsave(from_color);
}
static int
cursor_color_equal(char_u *lhs_color, char_u *rhs_color)
{
if (lhs_color != NULL && rhs_color != NULL)
return STRCMP(lhs_color, rhs_color) == 0;
return lhs_color == NULL && rhs_color == NULL;
}
static char_u *
cursor_color_get(char_u *color)
{
return (color == NULL) ? (char_u *)"" : color;
}
/* /*
* Parse 'termwinsize' and set "rows" and "cols" for the terminal size in the * Parse 'termwinsize' and set "rows" and "cols" for the terminal size in the
* current window. * current window.
@@ -823,8 +845,6 @@ free_terminal(buf_T *buf)
if (term->tl_out_fd != NULL) if (term->tl_out_fd != NULL)
fclose(term->tl_out_fd); fclose(term->tl_out_fd);
#endif #endif
if (desired_cursor_color == term->tl_cursor_color)
desired_cursor_color = (char_u *)"";
vim_free(term->tl_cursor_color); vim_free(term->tl_cursor_color);
vim_free(term); vim_free(term);
buf->b_term = NULL; buf->b_term = NULL;
@@ -1954,14 +1974,14 @@ term_get_cursor_shape(guicolor_T *fg, guicolor_T *bg)
static void static void
may_output_cursor_props(void) may_output_cursor_props(void)
{ {
if (STRCMP(last_set_cursor_color, desired_cursor_color) != 0 if (!cursor_color_equal(last_set_cursor_color, desired_cursor_color)
|| last_set_cursor_shape != desired_cursor_shape || last_set_cursor_shape != desired_cursor_shape
|| last_set_cursor_blink != desired_cursor_blink) || last_set_cursor_blink != desired_cursor_blink)
{ {
last_set_cursor_color = desired_cursor_color; cursor_color_copy(&last_set_cursor_color, desired_cursor_color);
last_set_cursor_shape = desired_cursor_shape; last_set_cursor_shape = desired_cursor_shape;
last_set_cursor_blink = desired_cursor_blink; last_set_cursor_blink = desired_cursor_blink;
term_cursor_color(desired_cursor_color); term_cursor_color(cursor_color_get(desired_cursor_color));
if (desired_cursor_shape == -1 || desired_cursor_blink == -1) if (desired_cursor_shape == -1 || desired_cursor_blink == -1)
/* this will restore the initial cursor style, if possible */ /* this will restore the initial cursor style, if possible */
ui_cursor_shape_forced(TRUE); ui_cursor_shape_forced(TRUE);
@@ -1984,10 +2004,7 @@ may_set_cursor_props(term_T *term)
#endif #endif
if (in_terminal_loop == term) if (in_terminal_loop == term)
{ {
if (term->tl_cursor_color != NULL) cursor_color_copy(&desired_cursor_color, term->tl_cursor_color);
desired_cursor_color = term->tl_cursor_color;
else
desired_cursor_color = (char_u *)"";
desired_cursor_shape = term->tl_cursor_shape; desired_cursor_shape = term->tl_cursor_shape;
desired_cursor_blink = term->tl_cursor_blink; desired_cursor_blink = term->tl_cursor_blink;
may_output_cursor_props(); may_output_cursor_props();
@@ -2004,7 +2021,7 @@ prepare_restore_cursor_props(void)
if (gui.in_use) if (gui.in_use)
return; return;
#endif #endif
desired_cursor_color = (char_u *)""; cursor_color_copy(&desired_cursor_color, NULL);
desired_cursor_shape = -1; desired_cursor_shape = -1;
desired_cursor_blink = -1; desired_cursor_blink = -1;
may_output_cursor_props(); may_output_cursor_props();
@@ -2624,13 +2641,7 @@ handle_settermprop(
break; break;
case VTERM_PROP_CURSORCOLOR: case VTERM_PROP_CURSORCOLOR:
if (desired_cursor_color == term->tl_cursor_color) cursor_color_copy(&term->tl_cursor_color, (char_u*)value->string);
desired_cursor_color = (char_u *)"";
vim_free(term->tl_cursor_color);
if (*value->string == NUL)
term->tl_cursor_color = NULL;
else
term->tl_cursor_color = vim_strsave((char_u *)value->string);
may_set_cursor_props(term); may_set_cursor_props(term);
break; break;
@@ -4711,8 +4722,7 @@ f_term_getcursor(typval_T *argvars, typval_T *rettv)
dict_add_nr_str(d, "blink", blink_state_is_inverted() dict_add_nr_str(d, "blink", blink_state_is_inverted()
? !term->tl_cursor_blink : term->tl_cursor_blink, NULL); ? !term->tl_cursor_blink : term->tl_cursor_blink, NULL);
dict_add_nr_str(d, "shape", term->tl_cursor_shape, NULL); dict_add_nr_str(d, "shape", term->tl_cursor_shape, NULL);
dict_add_nr_str(d, "color", 0L, term->tl_cursor_color == NULL dict_add_nr_str(d, "color", 0L, cursor_color_get(term->tl_cursor_color));
? (char_u *)"" : term->tl_cursor_color);
list_append_dict(l, d); list_append_dict(l, d);
} }
} }

View File

@@ -761,6 +761,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 */
/**/
13,
/**/ /**/
12, 12,
/**/ /**/