]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 9.1.1943: Memory leak with :breakadd expr v9.1.1943
authorzeertzjq <zeertzjq@outlook.com>
Mon, 1 Dec 2025 19:43:05 +0000 (19:43 +0000)
committerChristian Brabandt <cb@256bit.org>
Mon, 1 Dec 2025 19:43:05 +0000 (19:43 +0000)
Problem:  Memory leak with :breakadd expr
Solution: Free debug_oldval and debug_newval before assigning to them.
          Verify the existing (though confusing) :breakadd expr behavior
          (zeertzjq).

It seems that :breakadd expr doesn't work as documented at all. This PR
only fixes the memory leak. The tests are for the existing behavior.

closes: #18844

Signed-off-by: zeertzjq <zeertzjq@outlook.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
src/debugger.c
src/testdir/test_debugger.vim
src/version.c

index 97d54c439404d2cd2aa8e54997f5826e7ddc393e..f553a8e6a6fc7cc4ff088383edcd733ac192f13f 100644 (file)
@@ -1111,8 +1111,10 @@ debuggy_find(
            {
                if (bp->dbg_val == NULL)
                {
+                   vim_free(debug_oldval);
                    debug_oldval = typval_tostring(NULL, TRUE);
                    bp->dbg_val = tv;
+                   vim_free(debug_newval);
                    debug_newval = typval_tostring(bp->dbg_val, TRUE);
                    line = TRUE;
                }
@@ -1129,10 +1131,12 @@ debuggy_find(
                        typval_T *v;
 
                        line = TRUE;
+                       vim_free(debug_oldval);
                        debug_oldval = typval_tostring(bp->dbg_val, TRUE);
                        // Need to evaluate again, typval_compare() overwrites
                        // "tv".
                        v = eval_expr_no_emsg(bp);
+                       vim_free(debug_newval);
                        debug_newval = typval_tostring(v, TRUE);
                        free_tv(bp->dbg_val);
                        bp->dbg_val = v;
@@ -1142,7 +1146,9 @@ debuggy_find(
            }
            else if (bp->dbg_val != NULL)
            {
+               vim_free(debug_oldval);
                debug_oldval = typval_tostring(bp->dbg_val, TRUE);
+               vim_free(debug_newval);
                debug_newval = typval_tostring(NULL, TRUE);
                free_tv(bp->dbg_val);
                bp->dbg_val = NULL;
index 264420998e58a3cd14a730751e0e8a86f3a7bef8..aa014c4342375329aeb521296e83ba3ba2401042 100644 (file)
@@ -361,34 +361,73 @@ func Test_Debugger_breakadd()
 endfunc
 
 " Test for expression breakpoint set using ":breakadd expr <expr>"
+" FIXME: This doesn't seem to work as documented. The breakpoint is not
+" triggered until the next function call.
 func Test_Debugger_breakadd_expr()
   CheckCWD
 
   let lines =<< trim END
+    func Foo()
+      eval 1
+      eval 2
+    endfunc
+
     let g:Xtest_var += 1
+    call Foo()
+    let g:Xtest_var += 1
+    call Foo()
   END
-  call writefile(lines, 'XdebugBreakExpr.vim', 'D')
+  call writefile(lines, 'XbreakExpr.vim', 'D')
 
   " Start Vim in a terminal
-  let buf = RunVimInTerminal('XdebugBreakExpr.vim', {})
+  let buf = RunVimInTerminal('XbreakExpr.vim', {})
   call s:RunDbgCmd(buf, ':let g:Xtest_var = 10')
   call s:RunDbgCmd(buf, ':breakadd expr g:Xtest_var')
-  call s:RunDbgCmd(buf, ':source %')
   let expected =<< trim eval END
     Oldval = "10"
     Newval = "11"
-    {fnamemodify('XdebugBreakExpr.vim', ':p')}
-    line 1: let g:Xtest_var += 1
+    {fnamemodify('XbreakExpr.vim', ':p')}[7]..function Foo
+    line 1: eval 1
   END
   call s:RunDbgCmd(buf, ':source %', expected)
-  call s:RunDbgCmd(buf, 'cont')
   let expected =<< trim eval END
     Oldval = "11"
     Newval = "12"
-    {fnamemodify('XdebugBreakExpr.vim', ':p')}
-    line 1: let g:Xtest_var += 1
+    {fnamemodify('XbreakExpr.vim', ':p')}[9]..function Foo
+    line 1: eval 1
+  END
+  call s:RunDbgCmd(buf, 'cont', expected)
+  call s:RunDbgCmd(buf, 'cont')
+
+  " Check the behavior without the g: prefix.
+  " FIXME: The Oldval and Newval don't look right here.
+  call s:RunDbgCmd(buf, ':breakdel *')
+  call s:RunDbgCmd(buf, ':breakadd expr Xtest_var')
+  let expected =<< trim eval END
+    Oldval = "13"
+    Newval = "(does not exist)"
+    {fnamemodify('XbreakExpr.vim', ':p')}[7]..function Foo
+    line 1: eval 1
   END
   call s:RunDbgCmd(buf, ':source %', expected)
+  let expected =<< trim eval END
+    {fnamemodify('XbreakExpr.vim', ':p')}[7]..function Foo
+    line 2: eval 2
+  END
+  call s:RunDbgCmd(buf, 'cont', expected)
+  let expected =<< trim eval END
+    Oldval = "14"
+    Newval = "(does not exist)"
+    {fnamemodify('XbreakExpr.vim', ':p')}[9]..function Foo
+    line 1: eval 1
+  END
+  call s:RunDbgCmd(buf, 'cont', expected)
+  let expected =<< trim eval END
+    {fnamemodify('XbreakExpr.vim', ':p')}[9]..function Foo
+    line 2: eval 2
+  END
+  call s:RunDbgCmd(buf, 'cont', expected)
+  call s:RunDbgCmd(buf, 'cont')
 
   call StopVimInTerminal(buf)
 endfunc
index 81e8cf3b7a215b9ef71519f2d9261faa726285a4..d347aac56c3d6aa1e59cdda03f708761c59a5002 100644 (file)
@@ -729,6 +729,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1943,
 /**/
     1942,
 /**/