Skip to content

Commit a474de6

Browse files
zeertzjqchrisbra
authored andcommitted
patch 9.1.1943: Memory leak with :breakadd expr
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>
1 parent cce452f commit a474de6

File tree

3 files changed

+55
-8
lines changed

3 files changed

+55
-8
lines changed

src/debugger.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,8 +1111,10 @@ debuggy_find(
11111111
{
11121112
if (bp->dbg_val == NULL)
11131113
{
1114+
vim_free(debug_oldval);
11141115
debug_oldval = typval_tostring(NULL, TRUE);
11151116
bp->dbg_val = tv;
1117+
vim_free(debug_newval);
11161118
debug_newval = typval_tostring(bp->dbg_val, TRUE);
11171119
line = TRUE;
11181120
}
@@ -1129,10 +1131,12 @@ debuggy_find(
11291131
typval_T *v;
11301132

11311133
line = TRUE;
1134+
vim_free(debug_oldval);
11321135
debug_oldval = typval_tostring(bp->dbg_val, TRUE);
11331136
// Need to evaluate again, typval_compare() overwrites
11341137
// "tv".
11351138
v = eval_expr_no_emsg(bp);
1139+
vim_free(debug_newval);
11361140
debug_newval = typval_tostring(v, TRUE);
11371141
free_tv(bp->dbg_val);
11381142
bp->dbg_val = v;
@@ -1142,7 +1146,9 @@ debuggy_find(
11421146
}
11431147
else if (bp->dbg_val != NULL)
11441148
{
1149+
vim_free(debug_oldval);
11451150
debug_oldval = typval_tostring(bp->dbg_val, TRUE);
1151+
vim_free(debug_newval);
11461152
debug_newval = typval_tostring(NULL, TRUE);
11471153
free_tv(bp->dbg_val);
11481154
bp->dbg_val = NULL;

src/testdir/test_debugger.vim

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -361,34 +361,73 @@ func Test_Debugger_breakadd()
361361
endfunc
362362

363363
" Test for expression breakpoint set using ":breakadd expr <expr>"
364+
" FIXME: This doesn't seem to work as documented. The breakpoint is not
365+
" triggered until the next function call.
364366
func Test_Debugger_breakadd_expr()
365367
CheckCWD
366368

367369
let lines =<< trim END
370+
func Foo()
371+
eval 1
372+
eval 2
373+
endfunc
374+
368375
let g:Xtest_var += 1
376+
call Foo()
377+
let g:Xtest_var += 1
378+
call Foo()
369379
END
370-
call writefile(lines, 'XdebugBreakExpr.vim', 'D')
380+
call writefile(lines, 'XbreakExpr.vim', 'D')
371381

372382
" Start Vim in a terminal
373-
let buf = RunVimInTerminal('XdebugBreakExpr.vim', {})
383+
let buf = RunVimInTerminal('XbreakExpr.vim', {})
374384
call s:RunDbgCmd(buf, ':let g:Xtest_var = 10')
375385
call s:RunDbgCmd(buf, ':breakadd expr g:Xtest_var')
376-
call s:RunDbgCmd(buf, ':source %')
377386
let expected =<< trim eval END
378387
Oldval = "10"
379388
Newval = "11"
380-
{fnamemodify('XdebugBreakExpr.vim', ':p')}
381-
line 1: let g:Xtest_var += 1
389+
{fnamemodify('XbreakExpr.vim', ':p')}[7]..function Foo
390+
line 1: eval 1
382391
END
383392
call s:RunDbgCmd(buf, ':source %', expected)
384-
call s:RunDbgCmd(buf, 'cont')
385393
let expected =<< trim eval END
386394
Oldval = "11"
387395
Newval = "12"
388-
{fnamemodify('XdebugBreakExpr.vim', ':p')}
389-
line 1: let g:Xtest_var += 1
396+
{fnamemodify('XbreakExpr.vim', ':p')}[9]..function Foo
397+
line 1: eval 1
398+
END
399+
call s:RunDbgCmd(buf, 'cont', expected)
400+
call s:RunDbgCmd(buf, 'cont')
401+
402+
" Check the behavior without the g: prefix.
403+
" FIXME: The Oldval and Newval don't look right here.
404+
call s:RunDbgCmd(buf, ':breakdel *')
405+
call s:RunDbgCmd(buf, ':breakadd expr Xtest_var')
406+
let expected =<< trim eval END
407+
Oldval = "13"
408+
Newval = "(does not exist)"
409+
{fnamemodify('XbreakExpr.vim', ':p')}[7]..function Foo
410+
line 1: eval 1
390411
END
391412
call s:RunDbgCmd(buf, ':source %', expected)
413+
let expected =<< trim eval END
414+
{fnamemodify('XbreakExpr.vim', ':p')}[7]..function Foo
415+
line 2: eval 2
416+
END
417+
call s:RunDbgCmd(buf, 'cont', expected)
418+
let expected =<< trim eval END
419+
Oldval = "14"
420+
Newval = "(does not exist)"
421+
{fnamemodify('XbreakExpr.vim', ':p')}[9]..function Foo
422+
line 1: eval 1
423+
END
424+
call s:RunDbgCmd(buf, 'cont', expected)
425+
let expected =<< trim eval END
426+
{fnamemodify('XbreakExpr.vim', ':p')}[9]..function Foo
427+
line 2: eval 2
428+
END
429+
call s:RunDbgCmd(buf, 'cont', expected)
430+
call s:RunDbgCmd(buf, 'cont')
392431

393432
call StopVimInTerminal(buf)
394433
endfunc

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,8 @@ static char *(features[]) =
729729

730730
static int included_patches[] =
731731
{ /* Add new patch number below this line */
732+
/**/
733+
1943,
732734
/**/
733735
1942,
734736
/**/

0 commit comments

Comments
 (0)