Skip to content

fix: stop off-by-one read past the last line in semantic tokens - #387

Open
Booyaka101 wants to merge 1 commit into
pmizio:masterfrom
Booyaka101:fix-semantic-tokens-off-by-one
Open

fix: stop off-by-one read past the last line in semantic tokens#387
Booyaka101 wants to merge 1 commit into
pmizio:masterfrom
Booyaka101:fix-semantic-tokens-off-by-one

Conversation

@Booyaka101

Copy link
Copy Markdown

get_character_position_at_offset iterates line (a 0-indexed LSP line number) up to #lines_lengths, but indexes the 1-indexed lines_lengths as [line + 1]. On the final iteration that reads lines_lengths[#lines_lengths + 1] (nil) and errors:

.../protocol/text_document/semantic_tokens.lua:67: attempt to perform arithmetic on local 'current_line_length' (a nil value)

Iterating to #lines_lengths - 1 covers the last line (its index is #lines_lengths - 1) without the out-of-bounds read. In-range offsets are unaffected — they return earlier in the loop — and an offset past the end now returns nil, matching the documented LspPosition|nil return.

Closes #386

The position loop ran one index past the end of the line-length table,
reading a nil entry and erroring on the arithmetic. Stop at the last valid
line instead, which still covers it without the out-of-bounds read.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

crash in get_character_position_at_offset due to off-by-one loop counter

1 participant