Patch 9.0.0871
Problem:    Using freed memory when clearing augroup at more prompt.
Solution:   Delay clearing augroup until it's safe. (closes #11441)
Files:      src/autocmd.c, src/testdir/test_autocmd.vim


*** ../vim-9.0.0870/src/autocmd.c       2022-10-15 11:47:54.213416782 +0100
--- src/autocmd.c       2022-11-13 17:47:09.695180821 +0000
***************
*** 296,304 ****
      if (ap->pat == NULL)              // pattern has been removed
        return;
  
      msg_putchar('\n');
      if (got_int)
!       return;
      if (event != last_event || ap->group != last_group)
      {
        if (ap->group != AUGROUP_DEFAULT)
--- 296,309 ----
      if (ap->pat == NULL)              // pattern has been removed
        return;
  
+     // Make sure no info referenced by "ap" is cleared, e.g. when a timer
+     // clears an augroup.  Jump to "theend" after this!
+     // "ap->pat" may be cleared anyway.
+     ++autocmd_busy;
+ 
      msg_putchar('\n');
      if (got_int)
!       goto theend;
      if (event != last_event || ap->group != last_group)
      {
        if (ap->group != AUGROUP_DEFAULT)
***************
*** 314,321 ****
        last_group = ap->group;
        msg_putchar('\n');
        if (got_int)
!           return;
      }
      msg_col = 4;
      msg_outtrans(ap->pat);
  
--- 319,330 ----
        last_group = ap->group;
        msg_putchar('\n');
        if (got_int)
!           goto theend;
      }
+ 
+     if (ap->pat == NULL)
+       goto theend;  // timer might have cleared the pattern or group
+ 
      msg_col = 4;
      msg_outtrans(ap->pat);
  
***************
*** 328,348 ****
            msg_putchar('\n');
        msg_col = 14;
        if (got_int)
!           return;
        msg_outtrans(ac->cmd);
  #ifdef FEAT_EVAL
        if (p_verbose > 0)
            last_set_msg(ac->script_ctx);
  #endif
        if (got_int)
!           return;
        if (ac->next != NULL)
        {
            msg_putchar('\n');
            if (got_int)
!               return;
        }
      }
  }
  
  /*
--- 337,360 ----
            msg_putchar('\n');
        msg_col = 14;
        if (got_int)
!           goto theend;
        msg_outtrans(ac->cmd);
  #ifdef FEAT_EVAL
        if (p_verbose > 0)
            last_set_msg(ac->script_ctx);
  #endif
        if (got_int)
!           goto theend;
        if (ac->next != NULL)
        {
            msg_putchar('\n');
            if (got_int)
!               goto theend;
        }
      }
+ 
+ theend:
+     --autocmd_busy;
  }
  
  /*
*** ../vim-9.0.0870/src/testdir/test_autocmd.vim        2022-10-18 
17:25:00.066265566 +0100
--- src/testdir/test_autocmd.vim        2022-11-13 17:51:32.659342283 +0000
***************
*** 62,67 ****
--- 62,68 ----
      set updatetime=20
      call timer_start(200, 'ExitInsertMode')
      call feedkeys('a', 'x!')
+     sleep 30m
      call assert_equal(1, g:triggered)
      unlet g:triggered
      au! CursorHoldI
***************
*** 2159,2164 ****
--- 2160,2186 ----
    unlet s:res
  endfunc
  
+ func Test_autocmd_user_clear_group()
+   CheckRunVimInTerminal
+ 
+   let lines =<< trim END
+     autocmd! User
+     for i in range(1, 999)
+       exe 'autocmd User ' .. 'Foo' .. i .. ' bar'
+     endfor
+     au CmdlineLeave : call timer_start(0, {-> execute('autocmd! User')})
+   END
+   call writefile(lines, 'XautoUser', 'D')
+   let buf = RunVimInTerminal('-S XautoUser', {'rows': 10})
+ 
+   " this was using freed memory
+   call term_sendkeys(buf, ":autocmd User\<CR>")
+   call TermWait(buf, 50)
+   call term_sendkeys(buf, "G")
+ 
+   call StopVimInTerminal(buf)
+ endfunc
+ 
  function s:Before_test_dirchanged()
    augroup test_dirchanged
      autocmd!
*** ../vim-9.0.0870/src/version.c       2022-11-13 12:54:46.718898500 +0000
--- src/version.c       2022-11-13 17:09:44.002038775 +0000
***************
*** 697,698 ****
--- 697,700 ----
  {   /* Add new patch number below this line */
+ /**/
+     871,
  /**/

-- 
ERROR 047: Keyboard not found.  Press RETURN to continue.

 /// Bram Moolenaar -- [email protected] -- http://www.Moolenaar.net   \\\
///                                                                      \\\
\\\        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ ///
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///

-- 
-- 
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups 
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/vim_dev/20221113175414.5F4CE1C0473%40moolenaar.net.

Raspunde prin e-mail lui