From cb1e9a5ce6124f9c3861da2b530995f61e7bb8be Mon Sep 17 00:00:00 2001 From: Bryan Hitchcock Date: Tue, 25 Jan 2022 18:04:03 -0800 Subject: [PATCH 1/2] Fix buffer overflow for completion example --- examples/completion.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/examples/completion.rs b/examples/completion.rs index f2d1dd36..1c58146e 100644 --- a/examples/completion.rs +++ b/examples/completion.rs @@ -29,10 +29,9 @@ impl Default for MyCompletion { impl Completion for MyCompletion { /// Simple completion implementation based on substring fn get(&self, input: &str) -> Option { - let s = input.to_string(); - let ss: Vec<&String> = self.options.iter().filter(|x| s == x[..s.len()]).collect(); - if ss.len() == 1 { - Some(ss[0].to_string()) + let matches = self.options.iter().filter(|x| x.starts_with(input)).collect::>(); + if matches.len() == 1 { + Some(matches[0].to_string()) } else { None } From e4ffea89020af9cf748feefb8a64a30db5092643 Mon Sep 17 00:00:00 2001 From: Bryan Hitchcock Date: Tue, 25 Jan 2022 18:15:38 -0800 Subject: [PATCH 2/2] fix formatting and improve naming --- examples/completion.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/examples/completion.rs b/examples/completion.rs index 1c58146e..76d790be 100644 --- a/examples/completion.rs +++ b/examples/completion.rs @@ -29,7 +29,12 @@ impl Default for MyCompletion { impl Completion for MyCompletion { /// Simple completion implementation based on substring fn get(&self, input: &str) -> Option { - let matches = self.options.iter().filter(|x| x.starts_with(input)).collect::>(); + let matches = self + .options + .iter() + .filter(|option| option.starts_with(input)) + .collect::>(); + if matches.len() == 1 { Some(matches[0].to_string()) } else {