aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/nvim/eval.c10
-rw-r--r--src/nvim/testdir/test_eval_stuff.vim7
2 files changed, 15 insertions, 2 deletions
diff --git a/src/nvim/eval.c b/src/nvim/eval.c
index a4606f76f3..998d0568ce 100644
--- a/src/nvim/eval.c
+++ b/src/nvim/eval.c
@@ -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;
diff --git a/src/nvim/testdir/test_eval_stuff.vim b/src/nvim/testdir/test_eval_stuff.vim
index 610ef65f8e..ff8f2e5fc7 100644
--- a/src/nvim/testdir/test_eval_stuff.vim
+++ b/src/nvim/testdir/test_eval_stuff.vim
@@ -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