]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 8.2.5079: DirChanged autocommand may use freed memory v8.2.5079
authorBram Moolenaar <Bram@vim.org>
Sun, 12 Jun 2022 10:49:16 +0000 (11:49 +0100)
committerBram Moolenaar <Bram@vim.org>
Sun, 12 Jun 2022 10:49:16 +0000 (11:49 +0100)
Problem:    DirChanged autocommand may use freed memory. (Shane-XB Qian)
Solution:   Free the memory later. (closes #10555)

src/ex_docmd.c
src/testdir/test_autocmd.vim
src/version.c

index 634a1bcef5665a755e3191e66640a916ea508e17..9a06999e7cf01be0e1e15997a899ec0ae7cfe334 100644 (file)
@@ -7525,6 +7525,7 @@ changedir_func(
     int                dir_differs;
     char_u     *acmd_fname = NULL;
     char_u     **pp;
+    char_u     *tofree;
 
     if (new_dir == NULL || allbuf_locked())
        return FALSE;
@@ -7601,13 +7602,14 @@ changedir_func(
        pp = &curtab->tp_prevdir;
     else
        pp = &prev_dir;
-    vim_free(*pp);
+    tofree = *pp;  // new_dir may use this
     *pp = pdir;
 
     post_chdir(scope);
 
     if (dir_differs)
        apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE, curbuf);
+    vim_free(tofree);
     return TRUE;
 }
 
index 24e2ed9842b5776b4b52e92544d355eee82c3dd8..15aab48a69c031d4c53cf700caed7bce261d198e 100644 (file)
@@ -2107,6 +2107,13 @@ function Test_dirchanged_global()
   call assert_equal(expected, s:li)
   exe 'lcd ' .. fnameescape(s:dir_bar)
   call assert_equal(expected, s:li)
+
+  exe 'cd ' .. s:dir_foo
+  exe 'cd ' .. s:dir_bar
+  autocmd! test_dirchanged DirChanged global let g:result = expand("<afile>")
+  cd -
+  call assert_equal(s:dir_foo, g:result)
+
   call s:After_test_dirchanged()
 endfunc
 
index aec6f4b279ba6d16a8377bda02296f0959021345..47c24667e6794394506aeb107d67e0cc4cdc7b73 100644 (file)
@@ -734,6 +734,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    5079,
 /**/
     5078,
 /**/