targets.vim icon indicating copy to clipboard operation
targets.vim copied to clipboard

fix infinite loop for argument object not supported by %

Open vanaigr opened this issue 2 years ago • 0 comments

If the argument textobject is modified and o contains a character that is not supported by % (or the matchpairs do not contain that character):

'a': {'argument': [{'o': 'a', 'c': 'b', 's': 'c'}]}

then using this textobject can result in Vim freezing if the unsupported character is located inside of a pair of matching symbols. For example, pressing daa with the cursor before

(a)

would freeze the editor.

The issue is that the function for finding the boundary of the argument uses % on the found symbol, and if % doesn't know where to jump on that symbol, it would move the cursor to the beginning of the outer pair of matching items (opening parenthesis in this case) - before the cursor's last position, which leads to searchpos finding the same match again, creating an infinite loop.

The fix is to check whether the % has moved the cursor backwards and return it to its last position, so that the searchpos would find a next match (matching at cursor position is not set in a:flags2).

The iteration count check is not necessary, but it takes around 30 seconds for it to return an error on my laptop for the example above, and would prevent Vim from completely freezing if something else causes the loop to never exit.

vanaigr avatar Nov 06 '23 07:11 vanaigr