aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--runtime/doc/eval.txt4
-rw-r--r--src/nvim/eval.c12
-rw-r--r--src/nvim/strings.c9
-rw-r--r--src/nvim/testdir/test_functions.vim97
-rw-r--r--src/nvim/testdir/test_match.vim14
5 files changed, 112 insertions, 24 deletions
diff --git a/runtime/doc/eval.txt b/runtime/doc/eval.txt
index b3ab0a4500..4c0ee6cc66 100644
--- a/runtime/doc/eval.txt
+++ b/runtime/doc/eval.txt
@@ -2998,8 +2998,8 @@ count({comp}, {expr} [, {ic} [, {start}]]) *count()*
When {ic} is given and it's |TRUE| then case is ignored.
When {comp} is a string then the number of not overlapping
- occurences of {expr} is returned.
-
+ occurrences of {expr} is returned. Zero is returned when
+ {expr} is an empty string.
*cscope_connection()*
cscope_connection([{num} , {dbpath} [, {prepend}]])
diff --git a/src/nvim/eval.c b/src/nvim/eval.c
index 9765b04922..22cb544f54 100644
--- a/src/nvim/eval.c
+++ b/src/nvim/eval.c
@@ -7603,7 +7603,7 @@ static void f_count(typval_T *argvars, typval_T *rettv, FunPtr fptr)
const char_u *expr = (char_u *)tv_get_string_chk(&argvars[1]);
const char_u *p = argvars[0].vval.v_string;
- if (!error && expr != NULL && p != NULL) {
+ if (!error && expr != NULL && *expr != NUL && p != NULL) {
if (ic) {
const size_t len = STRLEN(expr);
@@ -12227,7 +12227,7 @@ static void find_some_match(typval_T *const argvars, typval_T *const rettv,
long start = 0;
long nth = 1;
colnr_T startcol = 0;
- int match = 0;
+ bool match = false;
list_T *l = NULL;
listitem_T *li = NULL;
long idx = 0;
@@ -12325,7 +12325,7 @@ static void find_some_match(typval_T *const argvars, typval_T *const rettv,
for (;; ) {
if (l != NULL) {
if (li == NULL) {
- match = FALSE;
+ match = false;
break;
}
xfree(tofree);
@@ -12351,7 +12351,7 @@ static void find_some_match(typval_T *const argvars, typval_T *const rettv,
startcol = (colnr_T)(regmatch.startp[0]
+ (*mb_ptr2len)(regmatch.startp[0]) - str);
if (startcol > (colnr_T)len || str + startcol <= regmatch.startp[0]) {
- match = FALSE;
+ match = false;
break;
}
}
@@ -12424,13 +12424,13 @@ static void find_some_match(typval_T *const argvars, typval_T *const rettv,
vim_regfree(regmatch.regprog);
}
- if (type == kSomeMatchStrPos && l == NULL) {
+theend:
+ if (type == kSomeMatchStrPos && l == NULL && rettv->vval.v_list != NULL) {
// matchstrpos() without a list: drop the second item
list_T *const ret_l = rettv->vval.v_list;
tv_list_item_remove(ret_l, TV_LIST_ITEM_NEXT(ret_l, tv_list_first(ret_l)));
}
-theend:
xfree(tofree);
p_cpo = save_cpo;
}
diff --git a/src/nvim/strings.c b/src/nvim/strings.c
index 3b075f8b70..f24de72743 100644
--- a/src/nvim/strings.c
+++ b/src/nvim/strings.c
@@ -344,14 +344,17 @@ char *strcase_save(const char *const orig, bool upper)
char *p = res;
while (*p != NUL) {
- int l;
-
int c = utf_ptr2char((const char_u *)p);
+ int l = utf_ptr2len((const char_u *)p);
+ if (c == 0) {
+ // overlong sequence, use only the first byte
+ c = *p;
+ l = 1;
+ }
int uc = upper ? mb_toupper(c) : mb_tolower(c);
// Reallocate string when byte count changes. This is rare,
// thus it's OK to do another malloc()/free().
- l = utf_ptr2len((const char_u *)p);
int newl = utf_char2len(uc);
if (newl != l) {
// TODO(philix): use xrealloc() in strup_save()
diff --git a/src/nvim/testdir/test_functions.vim b/src/nvim/testdir/test_functions.vim
index 3b16f2ce9f..6d0a6b9d5e 100644
--- a/src/nvim/testdir/test_functions.vim
+++ b/src/nvim/testdir/test_functions.vim
@@ -96,6 +96,30 @@ func Test_min()
" call assert_fails('call min(v:none)', 'E712:')
endfunc
+func Test_strwidth()
+ for aw in ['single', 'double']
+ exe 'set ambiwidth=' . aw
+ call assert_equal(0, strwidth(''))
+ call assert_equal(1, strwidth("\t"))
+ call assert_equal(3, strwidth('Vim'))
+ call assert_equal(4, strwidth(1234))
+ call assert_equal(5, strwidth(-1234))
+
+ if has('multi_byte')
+ call assert_equal(2, strwidth('😉'))
+ call assert_equal(17, strwidth('Eĥoŝanĝo ĉiuĵaŭde'))
+ call assert_equal((aw == 'single') ? 6 : 7, strwidth('Straße'))
+ endif
+
+ call assert_fails('call strwidth({->0})', 'E729:')
+ call assert_fails('call strwidth([])', 'E730:')
+ call assert_fails('call strwidth({})', 'E731:')
+ call assert_fails('call strwidth(1.2)', 'E806:')
+ endfor
+
+ set ambiwidth&
+endfunc
+
func Test_str2nr()
call assert_equal(0, str2nr(''))
call assert_equal(1, str2nr('1'))
@@ -215,6 +239,21 @@ func Test_setbufvar_options()
bwipe!
endfunc
+func Test_pathshorten()
+ call assert_equal('', pathshorten(''))
+ call assert_equal('foo', pathshorten('foo'))
+ call assert_equal('/foo', pathshorten('/foo'))
+ call assert_equal('f/', pathshorten('foo/'))
+ call assert_equal('f/bar', pathshorten('foo/bar'))
+ call assert_equal('f/b/foobar', pathshorten('foo/bar/foobar'))
+ call assert_equal('/f/b/foobar', pathshorten('/foo/bar/foobar'))
+ call assert_equal('.f/bar', pathshorten('.foo/bar'))
+ call assert_equal('~f/bar', pathshorten('~foo/bar'))
+ call assert_equal('~.f/bar', pathshorten('~.foo/bar'))
+ call assert_equal('.~f/bar', pathshorten('.~foo/bar'))
+ call assert_equal('~/f/bar', pathshorten('~/foo/bar'))
+endfunc
+
func Test_strpart()
call assert_equal('de', strpart('abcdefg', 3, 2))
call assert_equal('ab', strpart('abcdefg', -2, 4))
@@ -299,6 +338,11 @@ func Test_tolower()
" Ⱥ (U+023A) and Ⱦ (U+023E) are the *only* code points to increase
" in length (2 to 3 bytes) when lowercased. So let's test them.
call assert_equal("ⱥ ⱦ", tolower("Ⱥ Ⱦ"))
+
+ " This call to tolower with invalid utf8 sequence used to cause access to
+ " invalid memory.
+ call tolower("\xC0\x80\xC0")
+ call tolower("123\xC0\x80\xC0")
endfunc
func Test_toupper()
@@ -369,6 +413,11 @@ func Test_toupper()
call assert_equal("ZŹŻŽƵẐẔ", toupper("ZŹŻŽƵẐẔ"))
call assert_equal("Ⱥ Ⱦ", toupper("ⱥ ⱦ"))
+
+ " This call to toupper with invalid utf8 sequence used to cause access to
+ " invalid memory.
+ call toupper("\xC0\x80\xC0")
+ call toupper("123\xC0\x80\xC0")
endfunc
" Tests for the mode() function
@@ -573,10 +622,46 @@ func Test_strridx()
call assert_equal(-1, strridx('hello', 'hello world'))
endfunc
+func Test_match_func()
+ call assert_equal(4, match('testing', 'ing'))
+ call assert_equal(4, match('testing', 'ing', 2))
+ call assert_equal(-1, match('testing', 'ing', 5))
+ call assert_equal(-1, match('testing', 'ing', 8))
+ call assert_equal(1, match(['vim', 'testing', 'execute'], 'ing'))
+ call assert_equal(-1, match(['vim', 'testing', 'execute'], 'img'))
+endfunc
+
func Test_matchend()
call assert_equal(7, matchend('testing', 'ing'))
call assert_equal(7, matchend('testing', 'ing', 2))
call assert_equal(-1, matchend('testing', 'ing', 5))
+ call assert_equal(-1, matchend('testing', 'ing', 8))
+ call assert_equal(match(['vim', 'testing', 'execute'], 'ing'), matchend(['vim', 'testing', 'execute'], 'ing'))
+ call assert_equal(match(['vim', 'testing', 'execute'], 'img'), matchend(['vim', 'testing', 'execute'], 'img'))
+endfunc
+
+func Test_matchlist()
+ call assert_equal(['acd', 'a', '', 'c', 'd', '', '', '', '', ''], matchlist('acd', '\(a\)\?\(b\)\?\(c\)\?\(.*\)'))
+ call assert_equal(['d', '', '', '', 'd', '', '', '', '', ''], matchlist('acd', '\(a\)\?\(b\)\?\(c\)\?\(.*\)', 2))
+ call assert_equal([], matchlist('acd', '\(a\)\?\(b\)\?\(c\)\?\(.*\)', 4))
+endfunc
+
+func Test_matchstr()
+ call assert_equal('ing', matchstr('testing', 'ing'))
+ call assert_equal('ing', matchstr('testing', 'ing', 2))
+ call assert_equal('', matchstr('testing', 'ing', 5))
+ call assert_equal('', matchstr('testing', 'ing', 8))
+ call assert_equal('testing', matchstr(['vim', 'testing', 'execute'], 'ing'))
+ call assert_equal('', matchstr(['vim', 'testing', 'execute'], 'img'))
+endfunc
+
+func Test_matchstrpos()
+ call assert_equal(['ing', 4, 7], matchstrpos('testing', 'ing'))
+ call assert_equal(['ing', 4, 7], matchstrpos('testing', 'ing', 2))
+ call assert_equal(['', -1, -1], matchstrpos('testing', 'ing', 5))
+ call assert_equal(['', -1, -1], matchstrpos('testing', 'ing', 8))
+ call assert_equal(['ing', 1, 4, 7], matchstrpos(['vim', 'testing', 'execute'], 'ing'))
+ call assert_equal(['', -1, -1, -1], matchstrpos(['vim', 'testing', 'execute'], 'img'))
endfunc
func Test_nextnonblank_prevnonblank()
@@ -687,6 +772,7 @@ func Test_count()
call assert_equal(0, count("foo", "O"))
call assert_equal(2, count("foo", "O", 1))
call assert_equal(2, count("fooooo", "oo"))
+ call assert_equal(0, count("foo", ""))
endfunc
func Test_changenr()
@@ -770,6 +856,17 @@ func Test_col()
bw!
endfunc
+func Test_inputlist()
+ call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>1\<cr>", 'tx')
+ call assert_equal(1, c)
+ call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>2\<cr>", 'tx')
+ call assert_equal(2, c)
+ call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>3\<cr>", 'tx')
+ call assert_equal(3, c)
+
+ call assert_fails('call inputlist("")', 'E686:')
+endfunc
+
func Test_balloon_show()
if has('balloon_eval')
" This won't do anything but must not crash either.
diff --git a/src/nvim/testdir/test_match.vim b/src/nvim/testdir/test_match.vim
index 066bb2f6a1..e608a2e58b 100644
--- a/src/nvim/testdir/test_match.vim
+++ b/src/nvim/testdir/test_match.vim
@@ -1,5 +1,5 @@
" Test for :match, :2match, :3match, clearmatches(), getmatches(), matchadd(),
-" matchaddpos(), matcharg(), matchdelete(), matchstrpos() and setmatches().
+" matchaddpos(), matcharg(), matchdelete(), and setmatches().
function Test_match()
highlight MyGroup1 term=bold ctermbg=red guibg=red
@@ -150,18 +150,6 @@ function Test_match()
highlight MyGroup3 NONE
endfunc
-func Test_matchstrpos()
- call assert_equal(['ing', 4, 7], matchstrpos('testing', 'ing'))
-
- call assert_equal(['ing', 4, 7], matchstrpos('testing', 'ing', 2))
-
- call assert_equal(['', -1, -1], matchstrpos('testing', 'ing', 5))
-
- call assert_equal(['ing', 1, 4, 7], matchstrpos(['vim', 'testing', 'execute'], 'ing'))
-
- call assert_equal(['', -1, -1, -1], matchstrpos(['vim', 'testing', 'execute'], 'img'))
-endfunc
-
func Test_matchaddpos()
syntax on
set hlsearch