vim-patch:8.1.0804: crash when setting v:errmsg to empty list

Problem:    Crash when setting v:errmsg to empty list. (Jaon Franklin)
Solution:   Separate getting value and assigning result.
4b9e91f0ba
This commit is contained in:
Jan Edmund Lazo 2019-06-03 22:01:48 -04:00
parent 7ad621b6d9
commit 233a173226
2 changed files with 15 additions and 2 deletions

View File

@ -20082,9 +20082,15 @@ static void set_var(const char *name, const size_t name_len, typval_T *const tv,
// prevent changing the type.
if (ht == &vimvarht) {
if (v->di_tv.v_type == VAR_STRING) {
xfree(v->di_tv.vval.v_string);
XFREE_CLEAR(v->di_tv.vval.v_string);
if (copy || tv->v_type != VAR_STRING) {
v->di_tv.vval.v_string = (char_u *)xstrdup(tv_get_string(tv));
const char *const val = tv_get_string(tv);
// Careful: when assigning to v:errmsg and tv_get_string()
// causes an error message the variable will alrady be set.
if (v->di_tv.vval.v_string == NULL) {
v->di_tv.vval.v_string = (char_u *)xstrdup(val);
}
} else {
// Take over the string to avoid an extra alloc/free.
v->di_tv.vval.v_string = tv->vval.v_string;

View File

@ -92,3 +92,10 @@ func Test_nocatch_restore_silent_emsg()
let c5 = nr2char(screenchar(&lines, 5))
call assert_equal('wrong', c1 . c2 . c3 . c4 . c5)
endfunc
func Test_let_errmsg()
call assert_fails('let v:errmsg = []', 'E730:')
let v:errmsg = ''
call assert_fails('let v:errmsg = []', 'E730:')
let v:errmsg = ''
endfunc