comparison src/terminal.c @ 13990:017c5462ed5e v8.1.0013

patch 8.1.0013: using freed memory when changing terminal cursor color commit https://github.com/vim/vim/commit/4f7fd56b423245786f223a370c6fbfc12e3f37eb Author: Bram Moolenaar <Bram@vim.org> Date: Mon May 21 14:55:28 2018 +0200 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 https://github.com/vim/vim/issues/2938, closes #2941)
author Christian Brabandt <cb@256bit.org>
date Mon, 21 May 2018 15:00:07 +0200
parents dc648848f8be
children 411dd50f1daa
comparison
equal deleted inserted replaced
13989:12d747cf6cb2 13990:017c5462ed5e
169 static int term_default_cterm_fg = -1; 169 static int term_default_cterm_fg = -1;
170 static int term_default_cterm_bg = -1; 170 static int term_default_cterm_bg = -1;
171 171
172 /* Store the last set and the desired cursor properties, so that we only update 172 /* Store the last set and the desired cursor properties, so that we only update
173 * them when needed. Doing it unnecessary may result in flicker. */ 173 * them when needed. Doing it unnecessary may result in flicker. */
174 static char_u *last_set_cursor_color = (char_u *)""; 174 static char_u *last_set_cursor_color = NULL;
175 static char_u *desired_cursor_color = (char_u *)""; 175 static char_u *desired_cursor_color = NULL;
176 static int last_set_cursor_shape = -1; 176 static int last_set_cursor_shape = -1;
177 static int desired_cursor_shape = -1; 177 static int desired_cursor_shape = -1;
178 static int last_set_cursor_blink = -1; 178 static int last_set_cursor_blink = -1;
179 static int desired_cursor_blink = -1; 179 static int desired_cursor_blink = -1;
180 180
181 181
182 /************************************** 182 /**************************************
183 * 1. Generic code for all systems. 183 * 1. Generic code for all systems.
184 */ 184 */
185
186 static void
187 cursor_color_copy(char_u** to_color, char_u* from_color)
188 {
189 vim_free(*to_color);
190 *to_color = (from_color == NULL) ? NULL : vim_strsave(from_color);
191 }
192
193 static int
194 cursor_color_equal(char_u *lhs_color, char_u *rhs_color)
195 {
196 if (lhs_color != NULL && rhs_color != NULL)
197 return STRCMP(lhs_color, rhs_color) == 0;
198 return lhs_color == NULL && rhs_color == NULL;
199 }
200
201 static char_u *
202 cursor_color_get(char_u *color)
203 {
204 return (color == NULL) ? (char_u *)"" : color;
205 }
206
185 207
186 /* 208 /*
187 * Parse 'termwinsize' and set "rows" and "cols" for the terminal size in the 209 * Parse 'termwinsize' and set "rows" and "cols" for the terminal size in the
188 * current window. 210 * current window.
189 * Sets "rows" and/or "cols" to zero when it should follow the window size. 211 * Sets "rows" and/or "cols" to zero when it should follow the window size.
821 vim_free(term->tl_eof_chars); 843 vim_free(term->tl_eof_chars);
822 #ifdef WIN3264 844 #ifdef WIN3264
823 if (term->tl_out_fd != NULL) 845 if (term->tl_out_fd != NULL)
824 fclose(term->tl_out_fd); 846 fclose(term->tl_out_fd);
825 #endif 847 #endif
826 if (desired_cursor_color == term->tl_cursor_color)
827 desired_cursor_color = (char_u *)"";
828 vim_free(term->tl_cursor_color); 848 vim_free(term->tl_cursor_color);
829 vim_free(term); 849 vim_free(term);
830 buf->b_term = NULL; 850 buf->b_term = NULL;
831 if (in_terminal_loop == term) 851 if (in_terminal_loop == term)
832 in_terminal_loop = NULL; 852 in_terminal_loop = NULL;
1952 #endif 1972 #endif
1953 1973
1954 static void 1974 static void
1955 may_output_cursor_props(void) 1975 may_output_cursor_props(void)
1956 { 1976 {
1957 if (STRCMP(last_set_cursor_color, desired_cursor_color) != 0 1977 if (!cursor_color_equal(last_set_cursor_color, desired_cursor_color)
1958 || last_set_cursor_shape != desired_cursor_shape 1978 || last_set_cursor_shape != desired_cursor_shape
1959 || last_set_cursor_blink != desired_cursor_blink) 1979 || last_set_cursor_blink != desired_cursor_blink)
1960 { 1980 {
1961 last_set_cursor_color = desired_cursor_color; 1981 cursor_color_copy(&last_set_cursor_color, desired_cursor_color);
1962 last_set_cursor_shape = desired_cursor_shape; 1982 last_set_cursor_shape = desired_cursor_shape;
1963 last_set_cursor_blink = desired_cursor_blink; 1983 last_set_cursor_blink = desired_cursor_blink;
1964 term_cursor_color(desired_cursor_color); 1984 term_cursor_color(cursor_color_get(desired_cursor_color));
1965 if (desired_cursor_shape == -1 || desired_cursor_blink == -1) 1985 if (desired_cursor_shape == -1 || desired_cursor_blink == -1)
1966 /* this will restore the initial cursor style, if possible */ 1986 /* this will restore the initial cursor style, if possible */
1967 ui_cursor_shape_forced(TRUE); 1987 ui_cursor_shape_forced(TRUE);
1968 else 1988 else
1969 term_cursor_shape(desired_cursor_shape, desired_cursor_blink); 1989 term_cursor_shape(desired_cursor_shape, desired_cursor_blink);
1982 if (gui.in_use) 2002 if (gui.in_use)
1983 return; 2003 return;
1984 #endif 2004 #endif
1985 if (in_terminal_loop == term) 2005 if (in_terminal_loop == term)
1986 { 2006 {
1987 if (term->tl_cursor_color != NULL) 2007 cursor_color_copy(&desired_cursor_color, term->tl_cursor_color);
1988 desired_cursor_color = term->tl_cursor_color;
1989 else
1990 desired_cursor_color = (char_u *)"";
1991 desired_cursor_shape = term->tl_cursor_shape; 2008 desired_cursor_shape = term->tl_cursor_shape;
1992 desired_cursor_blink = term->tl_cursor_blink; 2009 desired_cursor_blink = term->tl_cursor_blink;
1993 may_output_cursor_props(); 2010 may_output_cursor_props();
1994 } 2011 }
1995 } 2012 }
2002 { 2019 {
2003 #ifdef FEAT_GUI 2020 #ifdef FEAT_GUI
2004 if (gui.in_use) 2021 if (gui.in_use)
2005 return; 2022 return;
2006 #endif 2023 #endif
2007 desired_cursor_color = (char_u *)""; 2024 cursor_color_copy(&desired_cursor_color, NULL);
2008 desired_cursor_shape = -1; 2025 desired_cursor_shape = -1;
2009 desired_cursor_blink = -1; 2026 desired_cursor_blink = -1;
2010 may_output_cursor_props(); 2027 may_output_cursor_props();
2011 } 2028 }
2012 2029
2622 term->tl_cursor_shape = value->number; 2639 term->tl_cursor_shape = value->number;
2623 may_set_cursor_props(term); 2640 may_set_cursor_props(term);
2624 break; 2641 break;
2625 2642
2626 case VTERM_PROP_CURSORCOLOR: 2643 case VTERM_PROP_CURSORCOLOR:
2627 if (desired_cursor_color == term->tl_cursor_color) 2644 cursor_color_copy(&term->tl_cursor_color, (char_u*)value->string);
2628 desired_cursor_color = (char_u *)"";
2629 vim_free(term->tl_cursor_color);
2630 if (*value->string == NUL)
2631 term->tl_cursor_color = NULL;
2632 else
2633 term->tl_cursor_color = vim_strsave((char_u *)value->string);
2634 may_set_cursor_props(term); 2645 may_set_cursor_props(term);
2635 break; 2646 break;
2636 2647
2637 case VTERM_PROP_ALTSCREEN: 2648 case VTERM_PROP_ALTSCREEN:
2638 /* TODO: do anything else? */ 2649 /* TODO: do anything else? */
4709 { 4720 {
4710 dict_add_nr_str(d, "visible", term->tl_cursor_visible, NULL); 4721 dict_add_nr_str(d, "visible", term->tl_cursor_visible, NULL);
4711 dict_add_nr_str(d, "blink", blink_state_is_inverted() 4722 dict_add_nr_str(d, "blink", blink_state_is_inverted()
4712 ? !term->tl_cursor_blink : term->tl_cursor_blink, NULL); 4723 ? !term->tl_cursor_blink : term->tl_cursor_blink, NULL);
4713 dict_add_nr_str(d, "shape", term->tl_cursor_shape, NULL); 4724 dict_add_nr_str(d, "shape", term->tl_cursor_shape, NULL);
4714 dict_add_nr_str(d, "color", 0L, term->tl_cursor_color == NULL 4725 dict_add_nr_str(d, "color", 0L, cursor_color_get(term->tl_cursor_color));
4715 ? (char_u *)"" : term->tl_cursor_color);
4716 list_append_dict(l, d); 4726 list_append_dict(l, d);
4717 } 4727 }
4718 } 4728 }
4719 4729
4720 /* 4730 /*