[vim/vim] Fix: memory leak in `check_for_cryptkey` in `src/fileio.c` (PR #19821)

1 view
Skip to first unread message

Huihui Huang

unread,
Mar 25, 2026, 8:31:10 AM (22 hours ago) Mar 25
to vim/vim, Subscribed

Problem

In check_for_cryptkey() located in src/fileio.c, when the encryption header is invalid (*sizep < header_len), the function returns NULL at line 3078 without freeing cryptkey:

cryptkey = crypt_get_key(newfile, FALSE);
...
if (cryptkey != NULL)
{
    int header_len;

    header_len = crypt_get_header_len(method);
    if (*sizep < header_len)
        // invalid header, buffer can't be encrypted
        return NULL;          // cryptkey is leaked

When newfile is FALSE, crypt_get_key() returns a freshly allocated key (not stored in curbuf->b_p_key). On this early return, that allocation is leaked.

Solution

Free cryptkey before returning NULL when the header is invalid, but only when it's not pointing to curbuf->b_p_key. This matches the same guard used at line 3064 for the empty-key case. 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/19821

Commit Summary

  • 886a7ef Fix: memory leak in check_for_cryptkey in src/fileio.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/19821@github.com>

Christian Brabandt

unread,
Mar 25, 2026, 3:11:12 PM (15 hours ago) Mar 25
to vim/vim, Subscribed
chrisbra left a comment (vim/vim#19821)

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/19821/c4129080489@github.com>

Christian Brabandt

unread,
Mar 25, 2026, 3:16:07 PM (15 hours ago) Mar 25
to vim/vim, Subscribed

Closed #19821 via a4c0119.


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/19821/issue_event/23904132764@github.com>

Reply all
Reply to author
Forward
0 new messages