Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle character boundary in search mode #18928

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

roife
Copy link
Member

@roife roife commented Jan 12, 2025

fix #18918

I also used Clippy to scan and check usages of slicing in the project, and found that only here could cause a panic.

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jan 12, 2025
@ChayimFriedman2
Copy link
Contributor

There is another slicing that looks like it may panic in line 305 (let prefix = &candidate[..query.len()];).

But do you know why it panics? We use match_indices(), it should return a correct index.

@@ -320,7 +320,7 @@ impl SearchMode {
};
match m {
Some((index, _)) => {
name = &name[index + 1..];
name = &name[ceil_char_boundary(name, index + 1)..];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given we want to discard the string up to (and including, hence the + 1) the query_char, I think we can just do

let mut chars = name[index..].chars();
chars.next();
chars.as_str()

instead?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or

name = name[index..].strip_prefix(|_: char| true).unwrap_or_default()

@Veykril
Copy link
Member

Veykril commented Jan 15, 2025

But do you know why it panics? We use match_indices(), it should return a correct index.

We panic because we slice one byte after the character, so if the character is multi byte we slice into it instead of beyond it.

There is another slicing that looks like it may panic in line 305 (let prefix = &candidate[..query.len()];).

That one shouldn't cause issues, given the searcher in that mode does a prefix search and so the prefix should match fully (it would be a bug if not)

@ChayimFriedman2
Copy link
Contributor

There is another slicing that looks like it may panic in line 305 (let prefix = &candidate[..query.len()];).

That one shouldn't cause issues, given the searcher in that mode does a prefix search and so the prefix should match fully (it would be a bug if not)

But it might not match.

@Veykril
Copy link
Member

Veykril commented Jan 15, 2025

Then we shouldn't even reach that far, the fst automaton should only give us prefix matches, the only thing that can differ at that point is the ascii casing which will not cause issues here. (if we were doing general casing changes it might)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug Report: Panic When Using Japanese Function Names in Rust Analyzer(boundary issue)
4 participants