commit ff62d761938e72129f6ba7ed2c2d56455444ebe7
parent b201cbfc61916a831d4a36f58284a5c1a3e006e1
Author: zeertzjq <zeertzjq@outlook.com>
Date: Tue, 20 Dec 2022 05:24:03 +0800
vim-patch:9.0.1079: leaking memory when defining a user command fails
Problem: Leaking memory when defining a user command fails.
Solution: Free "compl_arg" when needed. (closes vim/vim#11726)
https://github.com/vim/vim/commit/33e543038b84af7557ab9ecff500fc4ab98dd2a3
Diffstat:
2 files changed, 12 insertions(+), 2 deletions(-)
diff --git a/src/nvim/testdir/test_usercommands.vim b/src/nvim/testdir/test_usercommands.vim
@@ -323,6 +323,11 @@ func Test_CmdErrors()
call assert_fails('com DoCmd :', 'E174:')
comclear
call assert_fails('delcom DoCmd', 'E184:')
+
+ " These used to leak memory
+ call assert_fails('com! -complete=custom,CustomComplete _ :', 'E182:')
+ call assert_fails('com! -complete=custom,CustomComplete docmd :', 'E183:')
+ call assert_fails('com! -complete=custom,CustomComplete -xxx DoCmd :', 'E181:')
endfunc
func CustomComplete(A, L, P)
diff --git a/src/nvim/usercmd.c b/src/nvim/usercmd.c
@@ -943,7 +943,7 @@ void ex_command(exarg_T *eap)
end = skiptowhite(p);
if (uc_scan_attr(p, (size_t)(end - p), &argt, &def, &flags, &compl, (char_u **)&compl_arg,
&addr_type_arg) == FAIL) {
- return;
+ goto theend;
}
p = skipwhite(end);
}
@@ -953,7 +953,7 @@ void ex_command(exarg_T *eap)
end = uc_validate_name(name);
if (!end) {
emsg(_("E182: Invalid command name"));
- return;
+ goto theend;
}
name_len = (size_t)(end - name);
@@ -971,7 +971,12 @@ void ex_command(exarg_T *eap)
} else {
uc_add_command(name, name_len, p, argt, def, flags, compl, compl_arg, LUA_NOREF, LUA_NOREF,
addr_type_arg, LUA_NOREF, eap->forceit);
+
+ return; // success
}
+
+theend:
+ xfree(compl_arg);
}
/// ":comclear"