[vim/vim] Fix: memory leak in `add_mark` in `src/mark.c` (PR #19827)

2 views
Skip to first unread message

Huihui Huang

unread,
Mar 25, 2026, 11:15:04 PM (2 days ago) Mar 25
to vim/vim, Subscribed

Problem

In add_mark() located in src/mark.c, lpos is allocated via list_alloc() at line 1475. After populating it with position data, a chain of dict_add_* calls is evaluated with short-circuit || at lines 1484-1486:

lpos = list_alloc();
if (lpos == NULL)
    return FAIL;

list_append_number(lpos, bufnr);
list_append_number(lpos, pos->lnum);
list_append_number(lpos, pos->col < MAXCOL ? pos->col + 1 : MAXCOL);
list_append_number(lpos, pos->coladd);

if (dict_add_string(d, "mark", mname) == FAIL
        || dict_add_list(d, "pos", lpos) == FAIL
        || (fname != NULL && dict_add_string(d, "file", fname) == FAIL))
    return FAIL;       // lpos is leaked

If dict_add_string(d, "mark", mname) fails, dict_add_list(d, "pos", lpos) is never called due to short-circuit evaluation. lpos has no owner and is leaked on return FAIL.

When dict_add_list is called and succeeds, it increments lpos->lv_refcount to 1, transferring ownership to the dict. If dict_add_list itself fails, its internal dictitem_free cleans up and decrements the refcount, freeing the list. So the leak only occurs when dict_add_list is never reached.

Solution

On the failure path, check whether lpos was transferred to the dict by inspecting its lv_refcount. If it is still 0, it was never added and must be freed explicitly. The fix is included in the commit.


You can view, comment on, or merge this pull request online at:

  https://github.com/vim/vim/pull/19827

Commit Summary

  • b919323 Fix: memory leak in add_mark() in src/mark.c

File Changes

(1 file)

Patch Links:


Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/19827@github.com>

Christian Brabandt

unread,
Mar 26, 2026, 4:59:41 PM (13 hours ago) Mar 26
to vim/vim, Subscribed
chrisbra left a comment (vim/vim#19827)

thanks


Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/19827/c4138164733@github.com>

Christian Brabandt

unread,
Mar 26, 2026, 5:03:08 PM (13 hours ago) Mar 26
to vim/vim, Subscribed

Closed #19827 via b901456.


Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/19827/issue_event/23949693481@github.com>

Reply all
Reply to author
Forward
0 new messages