Skip to content

🐞 Bug: Index out of bounds from vim.treesitter.get_node_text in parsers/markdown.lua on nvim 0.12.2 #494

@antonich-blip

Description

@antonich-blip

Environment

  • Neovim: 0.12.2
  • markview.nvim: v28.1.0 (latest tag, same code on main)
  • OS: Linux (NixOS)

Problem

When markview's async render fires on a recently-edited markdown buffer, the iterated
tree-sitter capture node's range occasionally extends past the current buffer end. On nvim
0.12, nvim_buf_get_text performs strict bounds checking and throws Index out of bounds
instead of returning silently as it did on 0.11.

Traceback

vim.schedule callback: .../treesitter.lua:212: Index out of bounds
stack traceback:
[C]: in function 'nvim_buf_get_text'
.../runtime/lua/vim/treesitter.lua:212: in function 'get_node_text'
.../markview.nvim/lua/markview/parsers/markdown.lua:977: in function 'parse'
.../markview.nvim/lua/markview/parser.lua:193: in function 'fn'
.../runtime/lua/vim/treesitter/languagetree.lua:717: in function 'for_each_tree'
.../markview.nvim/lua/markview/parser.lua:183: in function 'parse'
.../markview.nvim/lua/markview/actions.lua:189: in function 'render'
.../markview.nvim/lua/markview/autocmds.lua:327: in function ''
vim/_core/editor.lua: in function <vim/_core/editor.lua:0>

Offending code

lua/markview/parsers/markdown.lua:977 (also present on main):

local capture_text = vim.treesitter.get_node_text(capture_node, buffer);
local r_start, c_start, r_end, c_end = capture_node:range();

Suggested fix

Wrap in pcall, or validate the node range against the current buffer size before the call:
local ok, capture_text = pcall(vim.treesitter.get_node_text, capture_node, buffer)
if not ok or capture_text == nil then
    goto continue
end
This preserves the existing capture_text == nil guard path.

Reproduction

Happens intermittently when editing a markdown buffer quickly (typing, deleting paragraphs)
 with markview attached. The render is scheduled via vim.schedule (autocmds.lua:327), and
by the time it runs the TSTree parser has been invalidated relative to the buffer.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions