Add bounds check to prevent panic in token_at_offset (#1367)

TexLab will panic if it receives a completion request with an invalid offset. This PR prevents the panic by checking if the offset is in range before passing it to `token_at_offset`.
This commit is contained in:
Henry Chu 2025-03-23 16:07:51 +08:00 committed by GitHub
parent 1f08c3a2c8
commit c45c857851
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -138,6 +138,10 @@ fn find_command_name_ast<L: rowan::Language>(
kind: L::Kind,
offset: TextSize,
) -> Option<Span> {
// `token_at_offset` will panic if offset is not within the range of `root`.
if !root.text_range().contains(offset) {
return None;
}
let token = root
.token_at_offset(offset)
.filter(|token| token.text_range().start() != offset)