annotate src/testdir/test_scriptnames.vim @ 33811:06219b3bdaf3 v9.0.2121

patch 9.0.2121: [security]: use-after-free in ex_substitute Commit: https://github.com/vim/vim/commit/26c11c56888d01e298cd8044caf860f3c26f57bb Author: Christian Brabandt <cb@256bit.org> Date: Wed Nov 22 21:26:41 2023 +0100 patch 9.0.2121: [security]: use-after-free in ex_substitute Problem: [security]: use-after-free in ex_substitute Solution: always allocate memory closes: #13552 A recursive :substitute command could cause a heap-use-after free in Vim (CVE-2023-48706). The whole reproducible test is a bit tricky, I can only reproduce this reliably when no previous substitution command has been used yet (which is the reason, the test needs to run as first one in the test_substitute.vim file) and as a combination of the `:~` command together with a :s command that contains the special substitution atom `~\=` which will make use of a sub-replace special atom and calls a vim script function. There was a comment in the existing :s code, that already makes the `sub` variable allocate memory so that a recursive :s call won't be able to cause any issues here, so this was known as a potential problem already. But for the current test-case that one does not work, because the substitution does not start with `\=` but with `~\=` (and since there does not yet exist a previous substitution atom, Vim will simply increment the `sub` pointer (which then was not allocated dynamically) and later one happily use a sub-replace special expression (which could then free the `sub` var). The following commit fixes this, by making the sub var always using allocated memory, which also means we need to free the pointer whenever we leave the function. Since sub is now always an allocated variable, we also do no longer need the sub_copy variable anymore, since this one was used to indicated when sub pointed to allocated memory (and had therefore to be freed on exit) and when not. Github Security Advisory: https://github.com/vim/vim/security/advisories/GHSA-c8qm-x72m-q53q Signed-off-by: Christian Brabandt <cb@256bit.org>
author Christian Brabandt <cb@256bit.org>
date Wed, 22 Nov 2023 22:15:05 +0100
parents 730eebd56f48
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
15085
1783c0b6bc2e patch 8.1.0553: it is not easy to edit a script that was sourced
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1
29810
761631155a90 patch 9.0.0244: cannot easily get the list of sourced scripts
Bram Moolenaar <Bram@vim.org>
parents: 27209
diff changeset
2 " Test for the :scriptnames command
15085
1783c0b6bc2e patch 8.1.0553: it is not easy to edit a script that was sourced
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
3 func Test_scriptnames()
30769
ae10b91ac6b3 patch 9.0.0719: too many delete() calls in tests
Bram Moolenaar <Bram@vim.org>
parents: 29930
diff changeset
4 call writefile(['let did_load_script = 123'], 'Xscripting', 'D')
15085
1783c0b6bc2e patch 8.1.0553: it is not easy to edit a script that was sourced
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
5 source Xscripting
1783c0b6bc2e patch 8.1.0553: it is not easy to edit a script that was sourced
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
6 call assert_equal(123, g:did_load_script)
1783c0b6bc2e patch 8.1.0553: it is not easy to edit a script that was sourced
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
7
1783c0b6bc2e patch 8.1.0553: it is not easy to edit a script that was sourced
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
8 let scripts = split(execute('scriptnames'), "\n")
1783c0b6bc2e patch 8.1.0553: it is not easy to edit a script that was sourced
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
9 let last = scripts[-1]
1783c0b6bc2e patch 8.1.0553: it is not easy to edit a script that was sourced
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
10 call assert_match('\<Xscripting\>', last)
1783c0b6bc2e patch 8.1.0553: it is not easy to edit a script that was sourced
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
11 let lastnr = substitute(last, '\D*\(\d\+\):.*', '\1', '')
1783c0b6bc2e patch 8.1.0553: it is not easy to edit a script that was sourced
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
12 exe 'script ' . lastnr
1783c0b6bc2e patch 8.1.0553: it is not easy to edit a script that was sourced
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
13 call assert_equal('Xscripting', expand('%:t'))
1783c0b6bc2e patch 8.1.0553: it is not easy to edit a script that was sourced
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
14
1783c0b6bc2e patch 8.1.0553: it is not easy to edit a script that was sourced
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
15 call assert_fails('script ' . (lastnr + 1), 'E474:')
1783c0b6bc2e patch 8.1.0553: it is not easy to edit a script that was sourced
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
16 call assert_fails('script 0', 'E939:')
1783c0b6bc2e patch 8.1.0553: it is not easy to edit a script that was sourced
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
17
1783c0b6bc2e patch 8.1.0553: it is not easy to edit a script that was sourced
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
18 new
1783c0b6bc2e patch 8.1.0553: it is not easy to edit a script that was sourced
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
19 call setline(1, 'nothing')
1783c0b6bc2e patch 8.1.0553: it is not easy to edit a script that was sourced
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
20 call assert_fails('script ' . lastnr, 'E37:')
1783c0b6bc2e patch 8.1.0553: it is not easy to edit a script that was sourced
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
21 exe 'script! ' . lastnr
1783c0b6bc2e patch 8.1.0553: it is not easy to edit a script that was sourced
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
22 call assert_equal('Xscripting', expand('%:t'))
1783c0b6bc2e patch 8.1.0553: it is not easy to edit a script that was sourced
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
23
1783c0b6bc2e patch 8.1.0553: it is not easy to edit a script that was sourced
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
24 bwipe
27209
fa09602eae48 patch 8.2.4133: output of ":scriptnames" goes into the message history
Bram Moolenaar <Bram@vim.org>
parents: 21765
diff changeset
25
fa09602eae48 patch 8.2.4133: output of ":scriptnames" goes into the message history
Bram Moolenaar <Bram@vim.org>
parents: 21765
diff changeset
26 let msgs = execute('messages')
fa09602eae48 patch 8.2.4133: output of ":scriptnames" goes into the message history
Bram Moolenaar <Bram@vim.org>
parents: 21765
diff changeset
27 scriptnames
fa09602eae48 patch 8.2.4133: output of ":scriptnames" goes into the message history
Bram Moolenaar <Bram@vim.org>
parents: 21765
diff changeset
28 call assert_equal(msgs, execute('messages'))
15085
1783c0b6bc2e patch 8.1.0553: it is not easy to edit a script that was sourced
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
29 endfunc
21765
08940efa6b4e patch 8.2.1432: various inconsistencies in test files
Bram Moolenaar <Bram@vim.org>
parents: 15085
diff changeset
30
29810
761631155a90 patch 9.0.0244: cannot easily get the list of sourced scripts
Bram Moolenaar <Bram@vim.org>
parents: 27209
diff changeset
31 " Test for the getscriptinfo() function
761631155a90 patch 9.0.0244: cannot easily get the list of sourced scripts
Bram Moolenaar <Bram@vim.org>
parents: 27209
diff changeset
32 func Test_getscriptinfo()
29861
8dca33bca038 patch 9.0.0269: getscriptinfo() does not include the version
Bram Moolenaar <Bram@vim.org>
parents: 29810
diff changeset
33 let lines =<< trim END
29930
cd573d7bc30d patch 9.0.0303: it is not easy to get information about a script
Bram Moolenaar <Bram@vim.org>
parents: 29861
diff changeset
34 scriptversion 3
29861
8dca33bca038 patch 9.0.0269: getscriptinfo() does not include the version
Bram Moolenaar <Bram@vim.org>
parents: 29810
diff changeset
35 let g:loaded_script_id = expand("<SID>")
8dca33bca038 patch 9.0.0269: getscriptinfo() does not include the version
Bram Moolenaar <Bram@vim.org>
parents: 29810
diff changeset
36 let s:XscriptVar = [1, #{v: 2}]
29930
cd573d7bc30d patch 9.0.0303: it is not easy to get information about a script
Bram Moolenaar <Bram@vim.org>
parents: 29861
diff changeset
37 func s:XgetScriptVar()
cd573d7bc30d patch 9.0.0303: it is not easy to get information about a script
Bram Moolenaar <Bram@vim.org>
parents: 29861
diff changeset
38 return s:XscriptVar
cd573d7bc30d patch 9.0.0303: it is not easy to get information about a script
Bram Moolenaar <Bram@vim.org>
parents: 29861
diff changeset
39 endfunc
cd573d7bc30d patch 9.0.0303: it is not easy to get information about a script
Bram Moolenaar <Bram@vim.org>
parents: 29861
diff changeset
40 func s:Xscript_legacy_func1()
29861
8dca33bca038 patch 9.0.0269: getscriptinfo() does not include the version
Bram Moolenaar <Bram@vim.org>
parents: 29810
diff changeset
41 endfunc
29930
cd573d7bc30d patch 9.0.0303: it is not easy to get information about a script
Bram Moolenaar <Bram@vim.org>
parents: 29861
diff changeset
42 def s:Xscript_def_func1()
cd573d7bc30d patch 9.0.0303: it is not easy to get information about a script
Bram Moolenaar <Bram@vim.org>
parents: 29861
diff changeset
43 enddef
cd573d7bc30d patch 9.0.0303: it is not easy to get information about a script
Bram Moolenaar <Bram@vim.org>
parents: 29861
diff changeset
44 func Xscript_legacy_func2()
cd573d7bc30d patch 9.0.0303: it is not easy to get information about a script
Bram Moolenaar <Bram@vim.org>
parents: 29861
diff changeset
45 endfunc
cd573d7bc30d patch 9.0.0303: it is not easy to get information about a script
Bram Moolenaar <Bram@vim.org>
parents: 29861
diff changeset
46 def Xscript_def_func2()
cd573d7bc30d patch 9.0.0303: it is not easy to get information about a script
Bram Moolenaar <Bram@vim.org>
parents: 29861
diff changeset
47 enddef
29861
8dca33bca038 patch 9.0.0269: getscriptinfo() does not include the version
Bram Moolenaar <Bram@vim.org>
parents: 29810
diff changeset
48 END
30769
ae10b91ac6b3 patch 9.0.0719: too many delete() calls in tests
Bram Moolenaar <Bram@vim.org>
parents: 29930
diff changeset
49 call writefile(lines, 'X22script91', 'D')
29861
8dca33bca038 patch 9.0.0269: getscriptinfo() does not include the version
Bram Moolenaar <Bram@vim.org>
parents: 29810
diff changeset
50 source X22script91
29810
761631155a90 patch 9.0.0244: cannot easily get the list of sourced scripts
Bram Moolenaar <Bram@vim.org>
parents: 27209
diff changeset
51 let l = getscriptinfo()
29861
8dca33bca038 patch 9.0.0269: getscriptinfo() does not include the version
Bram Moolenaar <Bram@vim.org>
parents: 29810
diff changeset
52 call assert_match('X22script91$', l[-1].name)
29810
761631155a90 patch 9.0.0244: cannot easily get the list of sourced scripts
Bram Moolenaar <Bram@vim.org>
parents: 27209
diff changeset
53 call assert_equal(g:loaded_script_id, $"<SNR>{l[-1].sid}_")
29930
cd573d7bc30d patch 9.0.0303: it is not easy to get information about a script
Bram Moolenaar <Bram@vim.org>
parents: 29861
diff changeset
54 call assert_equal(3, l[-1].version)
cd573d7bc30d patch 9.0.0303: it is not easy to get information about a script
Bram Moolenaar <Bram@vim.org>
parents: 29861
diff changeset
55 call assert_equal(0, has_key(l[-1], 'variables'))
cd573d7bc30d patch 9.0.0303: it is not easy to get information about a script
Bram Moolenaar <Bram@vim.org>
parents: 29861
diff changeset
56 call assert_equal(0, has_key(l[-1], 'functions'))
29861
8dca33bca038 patch 9.0.0269: getscriptinfo() does not include the version
Bram Moolenaar <Bram@vim.org>
parents: 29810
diff changeset
57
29930
cd573d7bc30d patch 9.0.0303: it is not easy to get information about a script
Bram Moolenaar <Bram@vim.org>
parents: 29861
diff changeset
58 " Get script information using script name
cd573d7bc30d patch 9.0.0303: it is not easy to get information about a script
Bram Moolenaar <Bram@vim.org>
parents: 29861
diff changeset
59 let l = getscriptinfo(#{name: '22script91'})
29861
8dca33bca038 patch 9.0.0269: getscriptinfo() does not include the version
Bram Moolenaar <Bram@vim.org>
parents: 29810
diff changeset
60 call assert_equal(1, len(l))
8dca33bca038 patch 9.0.0269: getscriptinfo() does not include the version
Bram Moolenaar <Bram@vim.org>
parents: 29810
diff changeset
61 call assert_match('22script91$', l[0].name)
29930
cd573d7bc30d patch 9.0.0303: it is not easy to get information about a script
Bram Moolenaar <Bram@vim.org>
parents: 29861
diff changeset
62 let sid = l[0].sid
cd573d7bc30d patch 9.0.0303: it is not easy to get information about a script
Bram Moolenaar <Bram@vim.org>
parents: 29861
diff changeset
63
cd573d7bc30d patch 9.0.0303: it is not easy to get information about a script
Bram Moolenaar <Bram@vim.org>
parents: 29861
diff changeset
64 " Get script information using script-ID
cd573d7bc30d patch 9.0.0303: it is not easy to get information about a script
Bram Moolenaar <Bram@vim.org>
parents: 29861
diff changeset
65 let l = getscriptinfo({'sid': sid})
cd573d7bc30d patch 9.0.0303: it is not easy to get information about a script
Bram Moolenaar <Bram@vim.org>
parents: 29861
diff changeset
66 call assert_equal(#{XscriptVar: [1, {'v': 2}]}, l[0].variables)
cd573d7bc30d patch 9.0.0303: it is not easy to get information about a script
Bram Moolenaar <Bram@vim.org>
parents: 29861
diff changeset
67 let funcs = ['Xscript_legacy_func2',
cd573d7bc30d patch 9.0.0303: it is not easy to get information about a script
Bram Moolenaar <Bram@vim.org>
parents: 29861
diff changeset
68 \ $"<SNR>{sid}_Xscript_legacy_func1",
cd573d7bc30d patch 9.0.0303: it is not easy to get information about a script
Bram Moolenaar <Bram@vim.org>
parents: 29861
diff changeset
69 \ $"<SNR>{sid}_Xscript_def_func1",
cd573d7bc30d patch 9.0.0303: it is not easy to get information about a script
Bram Moolenaar <Bram@vim.org>
parents: 29861
diff changeset
70 \ 'Xscript_def_func2',
cd573d7bc30d patch 9.0.0303: it is not easy to get information about a script
Bram Moolenaar <Bram@vim.org>
parents: 29861
diff changeset
71 \ $"<SNR>{sid}_XgetScriptVar"]
cd573d7bc30d patch 9.0.0303: it is not easy to get information about a script
Bram Moolenaar <Bram@vim.org>
parents: 29861
diff changeset
72 for f in funcs
cd573d7bc30d patch 9.0.0303: it is not easy to get information about a script
Bram Moolenaar <Bram@vim.org>
parents: 29861
diff changeset
73 call assert_true(index(l[0].functions, f) != -1)
cd573d7bc30d patch 9.0.0303: it is not easy to get information about a script
Bram Moolenaar <Bram@vim.org>
parents: 29861
diff changeset
74 endfor
cd573d7bc30d patch 9.0.0303: it is not easy to get information about a script
Bram Moolenaar <Bram@vim.org>
parents: 29861
diff changeset
75
cd573d7bc30d patch 9.0.0303: it is not easy to get information about a script
Bram Moolenaar <Bram@vim.org>
parents: 29861
diff changeset
76 " Verify that a script-local variable cannot be modified using the dict
cd573d7bc30d patch 9.0.0303: it is not easy to get information about a script
Bram Moolenaar <Bram@vim.org>
parents: 29861
diff changeset
77 " returned by getscriptinfo()
cd573d7bc30d patch 9.0.0303: it is not easy to get information about a script
Bram Moolenaar <Bram@vim.org>
parents: 29861
diff changeset
78 let l[0].variables.XscriptVar = ['n']
cd573d7bc30d patch 9.0.0303: it is not easy to get information about a script
Bram Moolenaar <Bram@vim.org>
parents: 29861
diff changeset
79 let funcname = $"<SNR>{sid}_XgetScriptVar"
cd573d7bc30d patch 9.0.0303: it is not easy to get information about a script
Bram Moolenaar <Bram@vim.org>
parents: 29861
diff changeset
80 call assert_equal([1, {'v': 2}], call(funcname, []))
29861
8dca33bca038 patch 9.0.0269: getscriptinfo() does not include the version
Bram Moolenaar <Bram@vim.org>
parents: 29810
diff changeset
81
8dca33bca038 patch 9.0.0269: getscriptinfo() does not include the version
Bram Moolenaar <Bram@vim.org>
parents: 29810
diff changeset
82 let l = getscriptinfo({'name': 'foobar'})
8dca33bca038 patch 9.0.0269: getscriptinfo() does not include the version
Bram Moolenaar <Bram@vim.org>
parents: 29810
diff changeset
83 call assert_equal(0, len(l))
8dca33bca038 patch 9.0.0269: getscriptinfo() does not include the version
Bram Moolenaar <Bram@vim.org>
parents: 29810
diff changeset
84 let l = getscriptinfo({'name': ''})
8dca33bca038 patch 9.0.0269: getscriptinfo() does not include the version
Bram Moolenaar <Bram@vim.org>
parents: 29810
diff changeset
85 call assert_true(len(l) > 1)
8dca33bca038 patch 9.0.0269: getscriptinfo() does not include the version
Bram Moolenaar <Bram@vim.org>
parents: 29810
diff changeset
86
8dca33bca038 patch 9.0.0269: getscriptinfo() does not include the version
Bram Moolenaar <Bram@vim.org>
parents: 29810
diff changeset
87 call assert_fails("echo getscriptinfo({'name': []})", 'E730:')
8dca33bca038 patch 9.0.0269: getscriptinfo() does not include the version
Bram Moolenaar <Bram@vim.org>
parents: 29810
diff changeset
88 call assert_fails("echo getscriptinfo({'name': '\\@'})", 'E866:')
8dca33bca038 patch 9.0.0269: getscriptinfo() does not include the version
Bram Moolenaar <Bram@vim.org>
parents: 29810
diff changeset
89 let l = getscriptinfo({'name': test_null_string()})
8dca33bca038 patch 9.0.0269: getscriptinfo() does not include the version
Bram Moolenaar <Bram@vim.org>
parents: 29810
diff changeset
90 call assert_true(len(l) > 1)
8dca33bca038 patch 9.0.0269: getscriptinfo() does not include the version
Bram Moolenaar <Bram@vim.org>
parents: 29810
diff changeset
91 call assert_fails("echo getscriptinfo('foobar')", 'E1206:')
8dca33bca038 patch 9.0.0269: getscriptinfo() does not include the version
Bram Moolenaar <Bram@vim.org>
parents: 29810
diff changeset
92
29930
cd573d7bc30d patch 9.0.0303: it is not easy to get information about a script
Bram Moolenaar <Bram@vim.org>
parents: 29861
diff changeset
93 call assert_fails("echo getscriptinfo({'sid': []})", 'E745:')
32200
730eebd56f48 patch 9.0.1431: getscriptinfo() loops even when specific SID is given
Bram Moolenaar <Bram@vim.org>
parents: 30769
diff changeset
94 call assert_fails("echo getscriptinfo({'sid': {}})", 'E728:')
730eebd56f48 patch 9.0.1431: getscriptinfo() loops even when specific SID is given
Bram Moolenaar <Bram@vim.org>
parents: 30769
diff changeset
95 call assert_fails("echo getscriptinfo({'sid': 0})", 'E475:')
730eebd56f48 patch 9.0.1431: getscriptinfo() loops even when specific SID is given
Bram Moolenaar <Bram@vim.org>
parents: 30769
diff changeset
96 call assert_fails("echo getscriptinfo({'sid': -1})", 'E475:')
730eebd56f48 patch 9.0.1431: getscriptinfo() loops even when specific SID is given
Bram Moolenaar <Bram@vim.org>
parents: 30769
diff changeset
97 call assert_fails("echo getscriptinfo({'sid': -999})", 'E475:')
730eebd56f48 patch 9.0.1431: getscriptinfo() loops even when specific SID is given
Bram Moolenaar <Bram@vim.org>
parents: 30769
diff changeset
98
730eebd56f48 patch 9.0.1431: getscriptinfo() loops even when specific SID is given
Bram Moolenaar <Bram@vim.org>
parents: 30769
diff changeset
99 echo getscriptinfo({'sid': '1'})
730eebd56f48 patch 9.0.1431: getscriptinfo() loops even when specific SID is given
Bram Moolenaar <Bram@vim.org>
parents: 30769
diff changeset
100 call assert_fails("vim9cmd echo getscriptinfo({'sid': '1'})", 'E1030:')
730eebd56f48 patch 9.0.1431: getscriptinfo() loops even when specific SID is given
Bram Moolenaar <Bram@vim.org>
parents: 30769
diff changeset
101
730eebd56f48 patch 9.0.1431: getscriptinfo() loops even when specific SID is given
Bram Moolenaar <Bram@vim.org>
parents: 30769
diff changeset
102 let max_sid = max(map(getscriptinfo(), { k, v -> v.sid }))
730eebd56f48 patch 9.0.1431: getscriptinfo() loops even when specific SID is given
Bram Moolenaar <Bram@vim.org>
parents: 30769
diff changeset
103 call assert_equal([], getscriptinfo({'sid': max_sid + 1}))
29810
761631155a90 patch 9.0.0244: cannot easily get the list of sourced scripts
Bram Moolenaar <Bram@vim.org>
parents: 27209
diff changeset
104 endfunc
761631155a90 patch 9.0.0244: cannot easily get the list of sourced scripts
Bram Moolenaar <Bram@vim.org>
parents: 27209
diff changeset
105
21765
08940efa6b4e patch 8.2.1432: various inconsistencies in test files
Bram Moolenaar <Bram@vim.org>
parents: 15085
diff changeset
106 " vim: shiftwidth=2 sts=2 expandtab