Skip to content

Instantly share code, notes, and snippets.

@ichizok
Last active September 25, 2017 07:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ichizok/14c26966880fb7fd83f2a0b16c932b37 to your computer and use it in GitHub Desktop.
Save ichizok/14c26966880fb7fd83f2a0b16c932b37 to your computer and use it in GitHub Desktop.
diff --git a/src/syntax.c b/src/syntax.c
index e0736cb63..87d374955 100644
--- a/src/syntax.c
+++ b/src/syntax.c
@@ -7354,6 +7354,19 @@ lookup_color(int idx, int foreground, int *boldp)
return color;
}
+ static void
+update_str_arg(char_u **str, char_u *arg)
+{
+ if (*str == NULL || STRCMP(*str, arg) != 0)
+ {
+ char_u *p = *str;
+
+ /* Avoid allocating same address as freed. */
+ *str = arg != NULL ? vim_strsave(arg) : NULL;
+ vim_free(p);
+ }
+}
+
/*
* Handle the ":highlight .." command.
* When using ":hi clear" this is called recursively for each group with
@@ -7785,8 +7798,7 @@ do_highlight(
else if (!gui.shell_created)
{
/* GUI not started yet, always accept the name. */
- vim_free(HL_TABLE()[idx].sg_font_name);
- HL_TABLE()[idx].sg_font_name = vim_strsave(arg);
+ update_str_arg(&HL_TABLE()[idx].sg_font_name, arg);
}
else
{
@@ -7813,8 +7825,7 @@ do_highlight(
/* New fontset was accepted. Free the old one, if there
* was one. */
gui_mch_free_fontset(temp_sg_fontset);
- vim_free(HL_TABLE()[idx].sg_font_name);
- HL_TABLE()[idx].sg_font_name = vim_strsave(arg);
+ update_str_arg(&HL_TABLE()[idx].sg_font_name, arg);
}
else
HL_TABLE()[idx].sg_fontset = temp_sg_fontset;
@@ -7824,8 +7835,7 @@ do_highlight(
/* New font was accepted. Free the old one, if there was
* one. */
gui_mch_free_font(temp_sg_font);
- vim_free(HL_TABLE()[idx].sg_font_name);
- HL_TABLE()[idx].sg_font_name = vim_strsave(arg);
+ update_str_arg(&HL_TABLE()[idx].sg_font_name, arg);
}
else
HL_TABLE()[idx].sg_font = temp_sg_font;
@@ -7982,11 +7992,8 @@ do_highlight(
{
HL_TABLE()[idx].sg_gui_fg = i;
# endif
- vim_free(HL_TABLE()[idx].sg_gui_fg_name);
- if (STRCMP(arg, "NONE") != 0)
- HL_TABLE()[idx].sg_gui_fg_name = vim_strsave(arg);
- else
- HL_TABLE()[idx].sg_gui_fg_name = NULL;
+ update_str_arg(&HL_TABLE()[idx].sg_gui_fg_name,
+ STRCMP(arg, "NONE") != 0 ? arg : NULL);
# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
# ifdef FEAT_GUI_X11
if (is_menu_group)
@@ -8019,11 +8026,8 @@ do_highlight(
{
HL_TABLE()[idx].sg_gui_bg = i;
# endif
- vim_free(HL_TABLE()[idx].sg_gui_bg_name);
- if (STRCMP(arg, "NONE") != 0)
- HL_TABLE()[idx].sg_gui_bg_name = vim_strsave(arg);
- else
- HL_TABLE()[idx].sg_gui_bg_name = NULL;
+ update_str_arg(&HL_TABLE()[idx].sg_gui_bg_name,
+ STRCMP(arg, "NONE") != 0 ? arg : NULL);
# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
# ifdef FEAT_GUI_X11
if (is_menu_group)
@@ -8055,11 +8059,8 @@ do_highlight(
{
HL_TABLE()[idx].sg_gui_sp = i;
# endif
- vim_free(HL_TABLE()[idx].sg_gui_sp_name);
- if (STRCMP(arg, "NONE") != 0)
- HL_TABLE()[idx].sg_gui_sp_name = vim_strsave(arg);
- else
- HL_TABLE()[idx].sg_gui_sp_name = NULL;
+ update_str_arg(&HL_TABLE()[idx].sg_gui_sp_name,
+ STRCMP(arg, "NONE") != 0 ? arg : NULL);
# ifdef FEAT_GUI
}
# endif
@@ -8136,17 +8137,11 @@ do_highlight(
if (STRCMP(buf, "NONE") == 0) /* resetting the value */
p = NULL;
else
- p = vim_strsave(buf);
+ p = buf;
if (key[2] == 'A')
- {
- vim_free(HL_TABLE()[idx].sg_start);
- HL_TABLE()[idx].sg_start = p;
- }
+ update_str_arg(&HL_TABLE()[idx].sg_start, p);
else
- {
- vim_free(HL_TABLE()[idx].sg_stop);
- HL_TABLE()[idx].sg_stop = p;
- }
+ update_str_arg(&HL_TABLE()[idx].sg_stop, p);
}
else
{
@@ -8495,10 +8490,7 @@ hl_set_font_name(char_u *font_name)
id = syn_name2id((char_u *)"Normal");
if (id > 0)
- {
- vim_free(HL_TABLE()[id - 1].sg_font_name);
- HL_TABLE()[id - 1].sg_font_name = vim_strsave(font_name);
- }
+ update_str_arg(&HL_TABLE()[id - 1].sg_font_name, font_name);
}
/*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment