Blame SOURCES/0001-patch-8.2.4919-can-add-invalid-bytes-with-spellgood.patch

3e779a
diff --git a/src/globals.h b/src/globals.h
3e779a
index d5320d7..968ba33 100644
3e779a
--- a/src/globals.h
3e779a
+++ b/src/globals.h
3e779a
@@ -1657,6 +1657,11 @@ EXTERN int *eval_lavars_used INIT(= NULL);
3e779a
 EXTERN int ctrl_break_was_pressed INIT(= FALSE);
3e779a
 #endif
3e779a
 
3e779a
+#ifdef FEAT_SPELL
3e779a
+EXTERN char e_illegal_character_in_word[]
3e779a
+	INIT(= N_("E1280: Illegal character in word"));
3e779a
+#endif
3e779a
+
3e779a
 /*
3e779a
  * Optional Farsi support.  Include it here, so EXTERN and INIT are defined.
3e779a
  */
3e779a
diff --git a/src/mbyte.c b/src/mbyte.c
3e779a
index 6d21f11..a7531f1 100644
3e779a
--- a/src/mbyte.c
3e779a
+++ b/src/mbyte.c
3e779a
@@ -4034,7 +4034,7 @@ theend:
3e779a
     convert_setup(&vimconv, NULL, NULL);
3e779a
 }
3e779a
 
3e779a
-#if defined(FEAT_GUI_GTK) || defined(PROTO)
3e779a
+#if defined(FEAT_GUI_GTK) || defined(FEAT_SPELL) || defined(PROTO)
3e779a
 /*
3e779a
  * Return TRUE if string "s" is a valid utf-8 string.
3e779a
  * When "end" is NULL stop at the first NUL.
3e779a
diff --git a/src/spellfile.c b/src/spellfile.c
3e779a
index 496e07f..92997ef 100644
3e779a
--- a/src/spellfile.c
3e779a
+++ b/src/spellfile.c
3e779a
@@ -4441,6 +4441,10 @@ store_word(
3e779a
     int		res = OK;
3e779a
     char_u	*p;
3e779a
 
3e779a
+    // Avoid adding illegal bytes to the word tree.
3e779a
+    if (enc_utf8 && !utf_valid_string(word, NULL))
3e779a
+	return FAIL;
3e779a
+
3e779a
     (void)spell_casefold(word, len, foldword, MAXWLEN);
3e779a
     for (p = pfxlist; res == OK; ++p)
3e779a
     {
3e779a
@@ -6251,6 +6255,12 @@ spell_add_word(
3e779a
     int		i;
3e779a
     char_u	*spf;
3e779a
 
3e779a
+    if (enc_utf8 && !utf_valid_string(word, NULL))
3e779a
+    {
3e779a
+	EMSG(_(e_illegal_character_in_word));
3e779a
+	return;
3e779a
+    }
3e779a
+
3e779a
     if (idx == 0)	    /* use internal wordlist */
3e779a
     {
3e779a
 	if (int_wordlist == NULL)