mirror of
https://github.com/fish-shell/fish-shell.git
synced 2026-05-25 14:51:15 -03:00
Fix tokenizer crash
This would crash from the highlighter for something like
`PATH={$PATH[echo " "`
The underlying cause is that we use "char_at" which panics on
overread.
So instead this implements try_char_at and then just returns None.
This commit is contained in:
@@ -769,16 +769,9 @@ pub fn quote_end(s: &wstr, mut pos: usize, quote: char) -> Option<usize> {
|
|||||||
loop {
|
loop {
|
||||||
pos += 1;
|
pos += 1;
|
||||||
|
|
||||||
if pos == s.len() {
|
let c = s.try_char_at(pos)?;
|
||||||
return None;
|
|
||||||
}
|
|
||||||
|
|
||||||
let c = s.char_at(pos);
|
|
||||||
if c == '\\' {
|
if c == '\\' {
|
||||||
pos += 1;
|
pos += 1;
|
||||||
if pos == s.len() {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
} else if c == quote ||
|
} else if c == quote ||
|
||||||
// Command substitutions also end a double quoted string. This is how we
|
// Command substitutions also end a double quoted string. This is how we
|
||||||
// support command substitutions inside double quotes.
|
// support command substitutions inside double quotes.
|
||||||
|
|||||||
@@ -222,6 +222,18 @@ fn char_at(&self, index: usize) -> char {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Return the char at an index.
|
||||||
|
/// If the index is equal to the length, return '\0'.
|
||||||
|
/// If the index exceeds the length, return None.
|
||||||
|
fn try_char_at(&self, index: usize) -> Option<char> {
|
||||||
|
let chars = self.as_char_slice();
|
||||||
|
match index {
|
||||||
|
_ if index == chars.len() => Some('\0'),
|
||||||
|
_ if index > chars.len() => None,
|
||||||
|
_ => Some(chars[index]),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// \return an iterator over substrings, split by a given char.
|
/// \return an iterator over substrings, split by a given char.
|
||||||
/// The split char is not included in the substrings.
|
/// The split char is not included in the substrings.
|
||||||
fn split(&self, c: char) -> WStrCharSplitIter {
|
fn split(&self, c: char) -> WStrCharSplitIter {
|
||||||
|
|||||||
@@ -432,3 +432,6 @@ echo 'begin
|
|||||||
echo 'multiline-\\
|
echo 'multiline-\\
|
||||||
-word' | $fish_indent --check
|
-word' | $fish_indent --check
|
||||||
echo $status #CHECK: 0
|
echo $status #CHECK: 0
|
||||||
|
|
||||||
|
echo 'PATH={$PATH[echo " "' | $fish_indent --ansi
|
||||||
|
# CHECK: PATH={$PATH[echo " "
|
||||||
|
|||||||
Reference in New Issue
Block a user