view src/testdir/test_gn.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 381bd66762ea
children
line wrap: on
line source

" Test for gn command

func Test_gn_command()
  noautocmd new
  " replace a single char by itself quoted:
  call setline('.', 'abc x def x ghi x jkl')
  let @/ = 'x'
  exe "norm! cgn'x'\<esc>.."
  call assert_equal("abc 'x' def 'x' ghi 'x' jkl", getline('.'))
  sil! %d_

  " simple search match
  call setline('.', 'foobar')
  let @/ = 'foobar'
  exe "norm! gncsearchmatch"
  call assert_equal('searchmatch', getline('.'))
  sil! %d _

  " replace a multi-line match
  call setline('.', ['', 'one', 'two'])
  let @/ = 'one\_s*two\_s'
  exe "norm! gnceins\<CR>zwei"
  call assert_equal(['','eins','zwei'], getline(1,'$'))
  sil! %d _

  " test count argument
  call setline('.', ['', 'abcdx | abcdx | abcdx'])
  let @/ = '[a]bcdx'
  exe "norm! 2gnd"
  call assert_equal(['','abcdx |  | abcdx'], getline(1,'$'))
  sil! %d _

  " join lines
  call setline('.', ['join ', 'lines'])
  let @/ = '$'
  exe "norm! 0gnd"
  call assert_equal(['join lines'], getline(1,'$'))
  sil! %d _

  " zero-width match
  call setline('.', ['', 'zero width pattern'])
  let @/ = '\>\zs'
  exe "norm! 0gnd"
  call assert_equal(['', 'zerowidth pattern'], getline(1,'$'))
  sil! %d _

  " delete first and last chars
  call setline('.', ['delete first and last chars'])
  let @/ = '^'
  exe "norm! 0gnd$"
  let @/ = '\zs'
  exe "norm! gnd"
  call assert_equal(['elete first and last char'], getline(1,'$'))
  sil! %d _

  " using visual mode
  call setline('.', ['', 'uniquepattern uniquepattern'])
  exe "norm! /[u]niquepattern/s\<cr>vlgnd"
  call assert_equal(['', ' uniquepattern'], getline(1,'$'))
  sil! %d _

  " backwards search
  call setline('.', ['my very excellent mother just served us nachos'])
  let @/ = 'mother'
  exe "norm! $cgNmongoose"
  call assert_equal(['my very excellent mongoose just served us nachos'], getline(1,'$'))
  sil! %d _

  " search for single char
  call setline('.', ['','for (i=0; i<=10; i++)'])
  let @/ = 'i'
  exe "norm! cgnj"
  call assert_equal(['','for (j=0; i<=10; i++)'], getline(1,'$'))
  sil! %d _

  " search hex char
  call setline('.', ['','Y'])
  set noignorecase
  let @/ = '\%x59'
  exe "norm! gnd"
  call assert_equal(['',''], getline(1,'$'))
  sil! %d _

  " test repeating gdn
  call setline('.', ['', '1', 'Johnny', '2', 'Johnny', '3'])
  let @/ = 'Johnny'
  exe "norm! dgn."
  call assert_equal(['','1', '', '2', '', '3'], getline(1,'$'))
  sil! %d _

  " test repeating gUgn
  call setline('.', ['', '1', 'Depp', '2', 'Depp', '3'])
  let @/ = 'Depp'
  exe "norm! gUgn."
  call assert_equal(['', '1', 'DEPP', '2', 'DEPP', '3'], getline(1,'$'))
  sil! %d _

  " test using look-ahead assertions
  call setline('.', ['a:10', '', 'a:1', '', 'a:20'])
  let @/ = 'a:0\@!\zs\d\+'
  exe "norm! 2nygno\<esc>p"
  call assert_equal(['a:10', '', 'a:1', '1', '', 'a:20'], getline(1,'$'))
  sil! %d _

  " test using nowrapscan
  set nowrapscan
  call setline(1, 'foo bar baz')
  exe "norm! /bar/e\<cr>"
  exe "norm! gnd"
  call assert_equal(['foo  baz'], getline(1,'$'))
  sil! %d_

  " search upwards with nowrapscan set
  call setline('.', ['foo', 'bar', 'foo', 'baz'])
  set nowrapscan
  let @/ = 'foo'
  $
  norm! dgN
  call assert_equal(['foo', 'bar', '', 'baz'], getline(1,'$'))
  sil! %d_

  " search using the \zs atom
  call setline(1, [' nnoremap', '', 'nnoremap'])
  set wrapscan&vim
  let @/ = '\_s\zsnnoremap'
  $
  norm! cgnmatch
  call assert_equal([' nnoremap', '', 'match'], getline(1,'$'))
  sil! %d_

  " make sure it works correctly for one-char wide search items
  call setline('.', ['abcdefghi'])
  let @/ = 'a'
  exe "norm! 0fhvhhgNgU"
  call assert_equal(['ABCDEFGHi'], getline(1,'$'))
  call setline('.', ['abcdefghi'])
  let @/ = 'b'
  " this gn wraps around the end of the file
  exe "norm! 0fhvhhgngU"
  call assert_equal(['aBCDEFGHi'], getline(1,'$'))
  sil! %d _
  call setline('.', ['abcdefghi'])
  let @/ = 'f'
  exe "norm! 0vllgngU"
  call assert_equal(['ABCDEFghi'], getline(1,'$'))
  sil! %d _
  call setline('.', ['12345678'])
  let @/ = '5'
  norm! gg0f7vhhhhgnd
  call assert_equal(['12348'], getline(1,'$'))
  sil! %d _
  call setline('.', ['12345678'])
  let @/ = '5'
  norm! gg0f2vf7gNd
  call assert_equal(['1678'], getline(1,'$'))
  sil! %d _
  set wrapscan&vim

  " Without 'wrapscan', in visual mode, running gn without a match should fail
  " but the visual mode should be kept.
  set nowrapscan
  call setline('.', 'one two')
  let @/ = 'one'
  call assert_beeps('normal 0wvlgn')
  exe "normal y"
  call assert_equal('tw', @")

  " with exclusive selection, run gn and gN
  set selection=exclusive
  normal 0gny
  call assert_equal('one', @")
  normal 0wgNy
  call assert_equal('one', @")
  set selection&
endfunc

func Test_gN_repeat()
  new
  call setline(1, 'this list is a list with a list of a list.')
  /list
  normal $gNgNgNx
  call assert_equal('list with a list of a list', @")
  bwipe!
endfunc

func Test_gN_then_gn()
  new

  call setline(1, 'this list is a list with a list of a last.')
  /l.st
  normal $gNgNgnx
  call assert_equal('last', @")

  call setline(1, 'this list is a list with a lust of a last.')
  /l.st
  normal $gNgNgNgnx
  call assert_equal('lust of a last', @")

  bwipe!
endfunc

func Test_gn_multi_line()
  new
  call setline(1, [
        \ 'func Tm1()',
        \ ' echo "one"',
        \ 'endfunc',
        \ 'func Tm2()',
        \ ' echo "two"',
        \ 'endfunc',
        \ 'func Tm3()',
        \ ' echo "three"',
        \ 'endfunc',
        \])
  /\v^func Tm\d\(\)\n.*\zs".*"\ze$
  normal jgnrx
  call assert_equal(' echo xxxxx', getline(5))
  bwipe!
endfunc

" vim: shiftwidth=2 sts=2 expandtab