robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
3b00ddb601
master
1" Tests for the C omni-completion plugin (runtime/autoload/ccomplete.vim). 2 3func s: WriteTags ( lines ) 4" Mark unsorted so lookup is a linear scan regardless of entry order. 5let tagsfile = tempname () 6call writefile ([ "!_TAG_FILE_SORTED\t0\t/0/" ] + a: lines , tagsfile ) 7return tagsfile 8endfunc 9 10" A crafted typeref field is interpolated into the :vimgrep pattern in 11" StructMembers(). Without escaping, "/" closes the pattern and "|" starts a 12" new Ex command, so the field runs as an Ex command during completion. 13func Test_ccomplete_no_exec_via_typeref () 14unlet ! g: ccomplete_injected 15let tagsfile = s: WriteTags ([ 16 \"myvar\tmain.c\t/^x$/;\"\tv\ttyperef:x/|let g:ccomplete_injected = 1|\"" , 17 \]) 18 19let save_tags = & tags 20let & tags = tagsfile 21 22new 23call ccomplete#Complete ( 1 , '' ) 24call ccomplete#Complete ( 0 , 'myvar.x' ) 25 26call assert_false ( exists ( 'g:ccomplete_injected' ), 27 \'typeref field was executed as an Ex command during omni-completion' ) 28 29bwipe ! 30let & tags = save_tags 31unlet ! g: ccomplete_injected 32endfunc 33 34" Escaping "/" and "\" is not enough: with no "/" in the payload, an unclosed 35" "[" makes vimgrep's pattern skipping fail, and the command parser then treats 36" the first "|" as a command separator. The typeref must be matched literally. 37func Test_ccomplete_no_exec_via_typeref_bracket () 38CheckUnix 39let sentinel = tempname () 40call delete ( sentinel ) 41let tagsfile = s: WriteTags ([ 42 \"myvar\tmain.c\t/^x$/;\"\tv\ttyperef:struct:[|call system('touch " .. sentinel .. "')|####" , 43 \]) 44 45let save_tags = & tags 46let & tags = tagsfile 47 48new 49call ccomplete#Complete ( 1 , '' ) 50call ccomplete#Complete ( 0 , 'myvar.x' ) 51 52call assert_false ( filereadable ( sentinel ), 53 \'typeref field was executed as an Ex command during omni-completion' ) 54 55bwipe ! 56let & tags = save_tags 57call delete ( sentinel ) 58endfunc 59 60" A legitimate typeref must still drive struct-member completion: escaping the 61" field value must not break the normal path. 62func Test_ccomplete_typeref_completion_still_works () 63let tagsfile = s: WriteTags ([ 64 \"myvar\tmain.c\t/^x$/;\"\tv\ttyperef:struct:mystruct" , 65 \"alpha\tmain.c\t/^x$/;\"\tm\tstruct:mystruct" , 66 \"beta\tmain.c\t/^x$/;\"\tm\tstruct:mystruct" , 67 \]) 68 69let save_tags = & tags 70let & tags = tagsfile 71 72new 73call ccomplete#Complete ( 1 , '' ) 74let items = ccomplete#Complete ( 0 , 'myvar.' ) 75 76call assert_equal ( type ([]), type ( items ), 77 \'ccomplete#Complete did not return a list' ) 78let names = map ( copy ( items ), 'v:val.word' ) 79call assert_true ( index ( names , 'alpha' ) >= 0 , 80 \'struct member "alpha" missing from completion: ' . string ( names )) 81call assert_true ( index ( names , 'beta' ) >= 0 , 82 \'struct member "beta" missing from completion: ' . string ( names )) 83 84bwipe ! 85let & tags = save_tags 86endfunc 87 88" The tags file names from tagfiles() are executed 89" via a single vimgrep command and trailing | does 90" not cause code execution 91func Test_ccomplete_no_exec_via_tagfile_name () 92CheckUnix 93let dir = "Xcc|&titlestring\t=\t'INJECTED'|ls\t" 94call mkdir ( dir , 'pR' ) 95let tagsfile = dir .. '/tags' 96call writefile ([ "!_TAG_FILE_SORTED\t0\t/0/" , 97 \"myvar\tmain.c\t/^x$/;\"\tv\ttyperef:struct:mystruct" , 98 \"alpha\tmain.c\t/^x$/;\"\tm\tstruct:mystruct" , 99 \], tagsfile ) 100 101let save_tags = & tags 102let save_title = & titlestring 103let & tags = fnamemodify ( tagsfile , ':p' ) 104set titlestring = orig 105 106new 107call ccomplete#Complete ( 1 , '' ) 108call ccomplete#Complete ( 0 , 'myvar.x' ) 109 110call assert_equal ( 'orig' , & titlestring , 111 \'tags file name was executed as an Ex command during omni-completion' ) 112 113bwipe ! 114let & tags = save_tags 115let & titlestring = save_title 116endfunc 117 118" vim: shiftwidth=2 sts=2 expandtab