mirror of
https://github.com/neovim/neovim.git
synced 2025-09-06 19:38:20 +00:00
vim-patch:8.2.3822: leaking memory in map() and filter(), no string in Vim9
Problem: Leaking memory in map() and filter(), cannot use a string argument
in Vim9 script.
Solution: Fix the leak, adjust the argument check, also run the tests as
Vim9 script. (Yegappan Lakshmanan, closes vim/vim#9354)
2d877599ee
Co-authored-by: Bram Moolenaar <Bram@vim.org>
This commit is contained in:
@@ -5042,7 +5042,7 @@ static void filter_map(typval_T *argvars, typval_T *rettv, filtermap_T filtermap
|
|||||||
: N_("filter() argument")));
|
: N_("filter() argument")));
|
||||||
|
|
||||||
// map() and filter() return the first argument, also on failure.
|
// map() and filter() return the first argument, also on failure.
|
||||||
if (filtermap != FILTERMAP_MAPNEW) {
|
if (filtermap != FILTERMAP_MAPNEW && argvars[0].v_type != VAR_STRING) {
|
||||||
tv_copy(&argvars[0], rettv);
|
tv_copy(&argvars[0], rettv);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,5 +1,7 @@
|
|||||||
" Test filter() and map()
|
" Test filter() and map()
|
||||||
|
|
||||||
|
source vim9.vim
|
||||||
|
|
||||||
" list with expression string
|
" list with expression string
|
||||||
func Test_filter_map_list_expr_string()
|
func Test_filter_map_list_expr_string()
|
||||||
" filter()
|
" filter()
|
||||||
@@ -136,68 +138,88 @@ func Test_mapnew_blob()
|
|||||||
call assert_equal(0z129956, bout)
|
call assert_equal(0z129956, bout)
|
||||||
endfunc
|
endfunc
|
||||||
|
|
||||||
|
" Test for using map(), filter() and mapnew() with a string
|
||||||
func Test_filter_map_string()
|
func Test_filter_map_string()
|
||||||
let s = "abc"
|
|
||||||
|
|
||||||
" filter()
|
" filter()
|
||||||
call filter(s, '"b" != v:val')
|
let lines =<< trim END
|
||||||
call assert_equal(s, s)
|
VAR s = "abc"
|
||||||
call assert_equal('ac', filter('abc', '"b" != v:val'))
|
call filter(s, '"b" != v:val')
|
||||||
call assert_equal('あいうえお', filter('あxいxうxえxお', '"x" != v:val'))
|
call assert_equal(s, s)
|
||||||
call assert_equal('あa😊💕💕b💕', filter('あxax😊x💕💕b💕x', '"x" != v:val'))
|
call assert_equal('ac', filter('abc', '"b" != v:val'))
|
||||||
call assert_equal('xxxx', filter('あxax😊x💕💕b💕x', '"x" == v:val'))
|
call assert_equal('あいうえお', filter('あxいxうxえxお', '"x" != v:val'))
|
||||||
let t = "%),:;>?]}’”†‡…‰,‱‼⁇⁈⁉℃℉,、。〉》」,』】〕〗〙〛,!),.:,;?,]}"
|
call assert_equal('あa😊💕💕b💕', filter('あxax😊x💕💕b💕x', '"x" != v:val'))
|
||||||
let u = "%):;>?]}’”†‡…‰‱‼⁇⁈⁉℃℉、。〉》」』】〕〗〙〛!),.:;?]}"
|
call assert_equal('xxxx', filter('あxax😊x💕💕b💕x', '"x" == v:val'))
|
||||||
call assert_equal(u, filter(t, '"," != v:val'))
|
VAR t = "%),:;>?]}’”†‡…‰,‱‼⁇⁈⁉℃℉,、。〉》」,』】〕〗〙〛,!),.:,;?,]}"
|
||||||
call assert_equal('', filter('abc', '0'))
|
VAR u = "%):;>?]}’”†‡…‰‱‼⁇⁈⁉℃℉、。〉》」』】〕〗〙〛!),.:;?]}"
|
||||||
call assert_equal('ac', filter('abc', { i, x -> "b" != x }))
|
call assert_equal(u, filter(t, '"," != v:val'))
|
||||||
call assert_equal('あいうえお', filter('あxいxうxえxお', { i, x -> "x" != x }))
|
call assert_equal('', filter('abc', '0'))
|
||||||
call assert_equal('', filter('abc', { i, x -> v:false }))
|
call assert_equal('ac', filter('abc', LSTART i, x LMIDDLE "b" != x LEND))
|
||||||
|
call assert_equal('あいうえお', filter('あxいxうxえxお', LSTART i, x LMIDDLE "x" != x LEND))
|
||||||
|
call assert_equal('', filter('abc', LSTART i, x LMIDDLE v:false LEND))
|
||||||
|
call assert_equal('', filter('', "v:val == 'a'"))
|
||||||
|
call assert_equal('', filter(v:_null_string, "v:val == 'a'"))
|
||||||
|
END
|
||||||
|
call CheckLegacyAndVim9Success(lines)
|
||||||
|
|
||||||
" map()
|
" map()
|
||||||
call map(s, 'nr2char(char2nr(v:val) + 2)')
|
let lines =<< trim END
|
||||||
call assert_equal(s, s)
|
VAR s = "abc"
|
||||||
call assert_equal('cde', map('abc', 'nr2char(char2nr(v:val) + 2)'))
|
call map(s, 'nr2char(char2nr(v:val) + 2)')
|
||||||
call assert_equal('[あ][i][う][え][お]', map('あiうえお', '"[" .. v:val .. "]"'))
|
call assert_equal(s, s)
|
||||||
call assert_equal('[あ][a][😊][,][‱][‼][⁇][⁈][⁉][💕][b][💕][c][💕]', map('あa😊,‱‼⁇⁈⁉💕b💕c💕', '"[" .. v:val .. "]"'))
|
call assert_equal('cde', map('abc', 'nr2char(char2nr(v:val) + 2)'))
|
||||||
call assert_equal('', map('abc', '""'))
|
call assert_equal('[あ][i][う][え][お]', map('あiうえお', '"[" .. v:val .. "]"'))
|
||||||
call assert_equal('cde', map('abc', { i, x -> nr2char(char2nr(x) + 2) }))
|
call assert_equal('[あ][a][😊][,][‱][‼][⁇][⁈][⁉][💕][b][💕][c][💕]', map('あa😊,‱‼⁇⁈⁉💕b💕c💕', '"[" .. v:val .. "]"'))
|
||||||
call assert_equal('[あ][i][う][え][お]', map('あiうえお', { i, x -> '[' .. x .. ']' }))
|
call assert_equal('', map('abc', '""'))
|
||||||
call assert_equal('', map('abc', { i, x -> '' }))
|
call assert_equal('cde', map('abc', LSTART i, x LMIDDLE nr2char(char2nr(x) + 2) LEND))
|
||||||
|
call assert_equal('[あ][i][う][え][お]', map('あiうえお', LSTART i, x LMIDDLE '[' .. x .. ']' LEND))
|
||||||
|
call assert_equal('', map('abc', LSTART i, x LMIDDLE '' LEND))
|
||||||
|
call assert_equal('', map('', "v:val == 'a'"))
|
||||||
|
call assert_equal('', map(v:_null_string, "v:val == 'a'"))
|
||||||
|
END
|
||||||
|
call CheckLegacyAndVim9Success(lines)
|
||||||
|
|
||||||
" mapnew()
|
" mapnew()
|
||||||
call mapnew(s, 'nr2char(char2nr(v:val) + 2)')
|
let lines =<< trim END
|
||||||
call assert_equal(s, s)
|
VAR s = "abc"
|
||||||
call assert_equal('cde', mapnew('abc', 'nr2char(char2nr(v:val) + 2)'))
|
call mapnew(s, 'nr2char(char2nr(v:val) + 2)')
|
||||||
call assert_equal('[あ][i][う][え][お]', mapnew('あiうえお', '"[" .. v:val .. "]"'))
|
call assert_equal(s, s)
|
||||||
call assert_equal('[あ][a][😊][,][‱][‼][⁇][⁈][⁉][💕][b][💕][c][💕]', mapnew('あa😊,‱‼⁇⁈⁉💕b💕c💕', '"[" .. v:val .. "]"'))
|
call assert_equal('cde', mapnew('abc', 'nr2char(char2nr(v:val) + 2)'))
|
||||||
call assert_equal('', mapnew('abc', '""'))
|
call assert_equal('[あ][i][う][え][お]', mapnew('あiうえお', '"[" .. v:val .. "]"'))
|
||||||
call assert_equal('cde', mapnew('abc', { i, x -> nr2char(char2nr(x) + 2) }))
|
call assert_equal('[あ][a][😊][,][‱][‼][⁇][⁈][⁉][💕][b][💕][c][💕]', mapnew('あa😊,‱‼⁇⁈⁉💕b💕c💕', '"[" .. v:val .. "]"'))
|
||||||
call assert_equal('[あ][i][う][え][お]', mapnew('あiうえお', { i, x -> '[' .. x .. ']' }))
|
call assert_equal('', mapnew('abc', '""'))
|
||||||
call assert_equal('', mapnew('abc', { i, x -> '' }))
|
call assert_equal('cde', mapnew('abc', LSTART i, x LMIDDLE nr2char(char2nr(x) + 2) LEND))
|
||||||
|
call assert_equal('[あ][i][う][え][お]', mapnew('あiうえお', LSTART i, x LMIDDLE '[' .. x .. ']' LEND))
|
||||||
|
call assert_equal('', mapnew('abc', LSTART i, x LMIDDLE '' LEND))
|
||||||
|
call assert_equal('', mapnew('', "v:val == 'a'"))
|
||||||
|
call assert_equal('', mapnew(v:_null_string, "v:val == 'a'"))
|
||||||
|
END
|
||||||
|
call CheckLegacyAndVim9Success(lines)
|
||||||
|
|
||||||
" map() and filter()
|
let lines =<< trim END
|
||||||
call assert_equal('[あ][⁈][a][😊][⁉][💕][💕][b][💕]', map(filter('あx⁈ax😊x⁉💕💕b💕x', '"x" != v:val'), '"[" .. v:val .. "]"'))
|
#" map() and filter()
|
||||||
|
call assert_equal('[あ][⁈][a][😊][⁉][💕][💕][b][💕]', map(filter('あx⁈ax😊x⁉💕💕b💕x', '"x" != v:val'), '"[" .. v:val .. "]"'))
|
||||||
|
|
||||||
" patterns-composing(\Z)
|
#" patterns-composing(\Z)
|
||||||
call assert_equal('ॠॠ', filter('ऊॠॡ,ऊॠॡ', {i,x -> x =~ '\Z' .. nr2char(0x0960) }))
|
call assert_equal('ॠॠ', filter('ऊॠॡ,ऊॠॡ', LSTART i, x LMIDDLE x =~ '\Z' .. nr2char(0x0960) LEND))
|
||||||
call assert_equal('àà', filter('càt,càt', {i,x -> x =~ '\Za' }))
|
call assert_equal('àà', filter('càt,càt', LSTART i, x LMIDDLE x =~ '\Za' LEND))
|
||||||
call assert_equal('ÅÅ', filter('Åström,Åström', {i,x -> x =~ '\Z' .. nr2char(0xc5) }))
|
call assert_equal('ÅÅ', filter('Åström,Åström', LSTART i, x LMIDDLE x =~ '\Z' .. nr2char(0xc5) LEND))
|
||||||
call assert_equal('öö', filter('Åström,Åström', {i,x -> x =~ '\Z' .. nr2char(0xf6) }))
|
call assert_equal('öö', filter('Åström,Åström', LSTART i, x LMIDDLE x =~ '\Z' .. nr2char(0xf6) LEND))
|
||||||
call assert_equal('ऊ@ॡ', map('ऊॠॡ', {i,x -> x =~ '\Z' .. nr2char(0x0960) ? '@' : x }))
|
call assert_equal('ऊ@ॡ', map('ऊॠॡ', LSTART i, x LMIDDLE x =~ '\Z' .. nr2char(0x0960) ? '@' : x LEND))
|
||||||
call assert_equal('c@t', map('càt', {i,x -> x =~ '\Za' ? '@' : x }))
|
call assert_equal('c@t', map('càt', LSTART i, x LMIDDLE x =~ '\Za' ? '@' : x LEND))
|
||||||
call assert_equal('@ström', map('Åström', {i,x -> x =~ '\Z' .. nr2char(0xc5) ? '@' : x }))
|
call assert_equal('@ström', map('Åström', LSTART i, x LMIDDLE x =~ '\Z' .. nr2char(0xc5) ? '@' : x LEND))
|
||||||
call assert_equal('Åstr@m', map('Åström', {i,x -> x =~ '\Z' .. nr2char(0xf6) ? '@' : x }))
|
call assert_equal('Åstr@m', map('Åström', LSTART i, x LMIDDLE x =~ '\Z' .. nr2char(0xf6) ? '@' : x LEND))
|
||||||
|
|
||||||
" patterns-composing(\%C)
|
#" patterns-composing(\%C)
|
||||||
call assert_equal('ॠॠ', filter('ऊॠॡ,ऊॠॡ', {i,x -> x =~ nr2char(0x0960) .. '\%C' }))
|
call assert_equal('ॠॠ', filter('ऊॠॡ,ऊॠॡ', LSTART i, x LMIDDLE x =~ nr2char(0x0960) .. '\%C' LEND))
|
||||||
call assert_equal('àà', filter('càt,càt', {i,x -> x =~ 'a' .. '\%C' }))
|
call assert_equal('àà', filter('càt,càt', LSTART i, x LMIDDLE x =~ 'a' .. '\%C' LEND))
|
||||||
call assert_equal('ÅÅ', filter('Åström,Åström', {i,x -> x =~ nr2char(0xc5) .. '\%C' }))
|
call assert_equal('ÅÅ', filter('Åström,Åström', LSTART i, x LMIDDLE x =~ nr2char(0xc5) .. '\%C' LEND))
|
||||||
call assert_equal('öö', filter('Åström,Åström', {i,x -> x =~ nr2char(0xf6) .. '\%C' }))
|
call assert_equal('öö', filter('Åström,Åström', LSTART i, x LMIDDLE x =~ nr2char(0xf6) .. '\%C' LEND))
|
||||||
call assert_equal('ऊ@ॡ', map('ऊॠॡ', {i,x -> x =~ nr2char(0x0960) .. '\%C' ? '@' : x }))
|
call assert_equal('ऊ@ॡ', map('ऊॠॡ', LSTART i, x LMIDDLE x =~ nr2char(0x0960) .. '\%C' ? '@' : x LEND))
|
||||||
call assert_equal('c@t', map('càt', {i,x -> x =~ 'a' .. '\%C' ? '@' : x }))
|
call assert_equal('c@t', map('càt', LSTART i, x LMIDDLE x =~ 'a' .. '\%C' ? '@' : x LEND))
|
||||||
call assert_equal('@ström', map('Åström', {i,x -> x =~ nr2char(0xc5) .. '\%C' ? '@' : x }))
|
call assert_equal('@ström', map('Åström', LSTART i, x LMIDDLE x =~ nr2char(0xc5) .. '\%C' ? '@' : x LEND))
|
||||||
call assert_equal('Åstr@m', map('Åström', {i,x -> x =~ nr2char(0xf6) .. '\%C' ? '@' : x }))
|
call assert_equal('Åstr@m', map('Åström', LSTART i, x LMIDDLE x =~ nr2char(0xf6) .. '\%C' ? '@' : x LEND))
|
||||||
|
END
|
||||||
|
call CheckLegacyAndVim9Success(lines)
|
||||||
endfunc
|
endfunc
|
||||||
|
|
||||||
" vim: shiftwidth=2 sts=2 expandtab
|
" vim: shiftwidth=2 sts=2 expandtab
|
||||||
|
Reference in New Issue
Block a user