patch 8.2.3757: an overlong highlight group name is silently truncated
Problem: An overlong highlight group name is silently truncated.
Solution: Give an error if the name is too long. (closes #9289)
diff --git a/src/errors.h b/src/errors.h
index a494de3..452ff45 100644
--- a/src/errors.h
+++ b/src/errors.h
@@ -756,3 +756,5 @@
INIT(= N_("E1247: Line number out of range"));
EXTERN char e_closure_called_from_invalid_context[]
INIT(= N_("E1248: Closure called from invalid context"));
+EXTERN char e_highlight_group_name_too_long[]
+ INIT(= N_("E1249: Highlight group name too long"));
diff --git a/src/highlight.c b/src/highlight.c
index 2deda68..9502428 100644
--- a/src/highlight.c
+++ b/src/highlight.c
@@ -18,6 +18,8 @@
#define SG_GUI 4 // gui has been set
#define SG_LINK 8 // link has been set
+#define MAX_SYN_NAME 200
+
/*
* The "term", "cterm" and "gui" arguments can be any combination of the
* following names, separated by commas (but no spaces!).
@@ -3328,12 +3330,12 @@
syn_name2id(char_u *name)
{
int i;
- char_u name_u[200];
+ char_u name_u[MAX_SYN_NAME + 1];
// Avoid using stricmp() too much, it's slow on some systems
// Avoid alloc()/free(), these are slow too. ID names over 200 chars
// don't deserve to be found!
- vim_strncpy(name_u, name, 199);
+ vim_strncpy(name_u, name, MAX_SYN_NAME);
vim_strup(name_u);
for (i = highlight_ga.ga_len; --i >= 0; )
if (HL_TABLE()[i].sg_name_u != NULL
@@ -3411,6 +3413,11 @@
int id;
char_u *name;
+ if (len > MAX_SYN_NAME)
+ {
+ emsg(_(e_highlight_group_name_too_long));
+ return 0;
+ }
name = vim_strnsave(pp, len);
if (name == NULL)
return 0;
diff --git a/src/testdir/test_highlight.vim b/src/testdir/test_highlight.vim
index 8e340bc..001cd39 100644
--- a/src/testdir/test_highlight.vim
+++ b/src/testdir/test_highlight.vim
@@ -740,6 +740,7 @@
call assert_fails('hi Xcomment ctermbg=fg', 'E419:')
call assert_fails('hi Xcomment ctermfg=bg', 'E420:')
call assert_fails('hi Xcomment ctermfg=ul', 'E453:')
+ call assert_fails('hi ' .. repeat('a', 201) .. ' ctermfg=black', 'E1249:')
endif
" Try using a very long terminal code. Define a dummy terminal code for this
diff --git a/src/version.c b/src/version.c
index c586acb..ccd3cf4 100644
--- a/src/version.c
+++ b/src/version.c
@@ -754,6 +754,8 @@
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
+ 3757,
+/**/
3756,
/**/
3755,