Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 61 additions & 3 deletions crates/edit/src/vt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,14 +244,22 @@ impl<'input> Stream<'_, 'input> {
0x40..=0x7e => {
self.parser.state = State::Ground;
self.parser.csi.final_byte = c as char;
if self.parser.csi.param_count != 0
|| self.parser.csi.params[0] != 0
// `param_count` is already at the limit if the sequence
// had more parameters than fit into `params`.
if self.parser.csi.param_count < self.parser.csi.params.len()
&& (self.parser.csi.param_count != 0
|| self.parser.csi.params[0] != 0)
{
self.parser.csi.param_count += 1;
}
return Some(Token::Csi(&self.parser.csi));
}
b';' => self.parser.csi.param_count += 1,
b';' => {
// Excess parameters are skipped above, so don't count past the end.
if self.parser.csi.param_count < self.parser.csi.params.len() {
self.parser.csi.param_count += 1;
}
}
b'<'..=b'?' => self.parser.csi.private_byte = c as char,
_ => {}
}
Expand Down Expand Up @@ -344,3 +352,53 @@ impl<'input> Stream<'_, 'input> {
None
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_csi_too_many_params() {
let mut parser = Parser::new();

// 40 parameters don't fit into `Csi::params`. The excess ones must be dropped,
// and the parser must still be usable for the next sequence afterwards.
let input = format!("\x1b[<{}m\x1b[1;5A", "1;".repeat(39) + "1");
let mut stream = parser.parse(&input);

match stream.next() {
Some(Token::Csi(csi)) => {
assert_eq!(csi.final_byte, 'm');
assert_eq!(csi.private_byte, '<');
assert_eq!(csi.param_count, csi.params.len());
assert!(csi.params[..csi.param_count].iter().all(|&p| p == 1));
}
_ => panic!("expected a CSI token"),
}

match stream.next() {
Some(Token::Csi(csi)) => {
assert_eq!(csi.final_byte, 'A');
assert_eq!(&csi.params[..csi.param_count], &[1, 5]);
}
_ => panic!("expected a CSI token"),
}

assert!(stream.next().is_none());
}

#[test]
fn test_csi_too_many_empty_params() {
let mut parser = Parser::new();
let input = format!("\x1b[{}m", ";".repeat(40));
let mut stream = parser.parse(&input);

match stream.next() {
Some(Token::Csi(csi)) => {
assert_eq!(csi.final_byte, 'm');
assert_eq!(csi.param_count, csi.params.len());
}
_ => panic!("expected a CSI token"),
}
}
}