patch 8.2.4453: :helpgrep may free an option that was not allocated

Problem:    :helpgrep may free an option that was not allocated. (Yegappan
            Lakshmanan)
Solution:   Check if the value was allocated.
diff --git a/src/option.c b/src/option.c
index 91ea413..44a0520 100644
--- a/src/option.c
+++ b/src/option.c
@@ -4479,6 +4479,14 @@
     return (char_u *)NULL;
 }
 
+    int
+is_option_allocated(char *name)
+{
+    int idx = findoption((char_u *)name);
+
+    return idx >= 0 && (options[idx].flags & P_ALLOCED);
+}
+
 /*
  * Translate a string like "t_xx", "<t_xx>" or "<S-Tab>" to a key number.
  * When "has_lt" is true there is a '<' before "*arg_arg".
diff --git a/src/proto/option.pro b/src/proto/option.pro
index 11f342c..b58e79a 100644
--- a/src/proto/option.pro
+++ b/src/proto/option.pro
@@ -41,6 +41,7 @@
 char_u *get_term_code(char_u *tname);
 char_u *get_highlight_default(void);
 char_u *get_encoding_default(void);
+int is_option_allocated(char *name);
 int makeset(FILE *fd, int opt_flags, int local_only);
 int makefoldset(FILE *fd);
 void clear_termoptions(void);
diff --git a/src/quickfix.c b/src/quickfix.c
index 998ec6e..16790b0 100644
--- a/src/quickfix.c
+++ b/src/quickfix.c
@@ -8235,6 +8235,7 @@
 {
     regmatch_T	regmatch;
     char_u	*save_cpo;
+    int		save_cpo_allocated;
     qf_info_T	*qi = &ql_info;
     int		new_qi = FALSE;
     char_u	*au_name =  NULL;
@@ -8265,6 +8266,7 @@
 
     // Make 'cpoptions' empty, the 'l' flag should not be used here.
     save_cpo = p_cpo;
+    save_cpo_allocated = is_option_allocated("cpo");
     p_cpo = empty_option;
 
     incr_quickfix_busy();
@@ -8302,7 +8304,8 @@
 	// changed and restored, need to restore in the complicated way.
 	if (*p_cpo == NUL)
 	    set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
-	free_string_option(save_cpo);
+	if (save_cpo_allocated)
+	    free_string_option(save_cpo);
     }
 
     if (updated)
diff --git a/src/testdir/test_quickfix.vim b/src/testdir/test_quickfix.vim
index 7d2373b..ccfa340 100644
--- a/src/testdir/test_quickfix.vim
+++ b/src/testdir/test_quickfix.vim
@@ -745,6 +745,33 @@
   helpclose
 enddef
 
+func Test_helpgrep_restore_cpo_aucmd()
+  let save_cpo = &cpo
+  augroup QF_Test
+    au!
+    autocmd BufNew * set cpo=acd
+  augroup END
+
+  helpgrep quickfix
+  call assert_equal('acd', &cpo)
+  %bw!
+
+  set cpo&vim
+  augroup QF_Test
+    au!
+    autocmd BufReadPost * set cpo=
+  augroup END
+
+  helpgrep buffer
+  call assert_equal('', &cpo)
+
+  augroup QF_Test
+    au!
+  augroup END
+  %bw!
+  let &cpo = save_cpo
+endfunc
+
 def Test_vim9_cexpr()
   var text = 'somefile:95:error'
   cexpr text
diff --git a/src/version.c b/src/version.c
index 1786ceb..57f32cb 100644
--- a/src/version.c
+++ b/src/version.c
@@ -751,6 +751,8 @@
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    4453,
+/**/
     4452,
 /**/
     4451,