Skip to content

Commit a7432ae

Browse files
committed
parser: Avoid slicing when parsing bracketed paste
This is not a real bug or issue. I'm just modifying the parser for bracketed paste to avoid indexing into the input buffer. strip_prefix and strip_suffix are, if anything, more ergonomic for this exact same use-case. These additional test cases passed before and after the change.
1 parent c04c2c7 commit a7432ae

1 file changed

Lines changed: 18 additions & 5 deletions

File tree

src/parse.rs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -885,13 +885,15 @@ fn parse_cb(cb: u8) -> Result<(MouseEventKind, Modifiers)> {
885885

886886
fn parse_csi_bracketed_paste(buffer: &[u8]) -> Result<Option<Event>> {
887887
// CSI 2 0 0 ~ pasted text CSI 2 0 1 ~
888-
assert!(buffer.starts_with(b"\x1B[200~"));
888+
let buffer = buffer
889+
.strip_prefix(b"\x1b[200~")
890+
.expect("asserted by calling functions");
889891

890-
if !buffer.ends_with(b"\x1b[201~") {
891-
Ok(None)
892-
} else {
893-
let paste = String::from_utf8_lossy(&buffer[6..buffer.len() - 6]).to_string();
892+
if let Some(contents) = buffer.strip_suffix(b"\x1b[201~") {
893+
let paste = String::from_utf8_lossy(contents).to_string();
894894
Ok(Some(Event::Paste(paste)))
895+
} else {
896+
Ok(None)
895897
}
896898
}
897899

@@ -1323,4 +1325,15 @@ mod test {
13231325
}))
13241326
);
13251327
}
1328+
1329+
#[test]
1330+
fn parse_bracketed_paste() {
1331+
// Incomplete input is not considered a paste.
1332+
let event = parse_event(b"\x1b[200~", false).unwrap();
1333+
assert_eq!(event, None);
1334+
let event = parse_event(b"\x1b[200~Hello, world!\x1b[201~", false).unwrap();
1335+
assert_eq!(event, Some(Event::Paste("Hello, world!".to_string())));
1336+
let event = parse_event(b"\x1b[200~\x1b[201~", false).unwrap();
1337+
assert_eq!(event, Some(Event::Paste("".to_string())));
1338+
}
13261339
}

0 commit comments

Comments
 (0)