Skip to content

A fast file manager for Windows and macOS that never freezes: tabs, dual panes, archives as folders, Quick Look-style previews. A File Explorer and Finder alternative written in Rust.

Dialog pipe reader: test that garbage is an error, never a panic

gelenmedyac6456c1b58da
diff --git a/crates/rf-dialog/src/proto.rs b/crates/rf-dialog/src/proto.rs
index 73ef0eb5..a4dc90c5 100644
--- a/crates/rf-dialog/src/proto.rs
+++ b/crates/rf-dialog/src/proto.rs
@@ -197,6 +197,35 @@ mod tests {
assert_eq!(r.folder_label, "");
}
+ /// The DLL lives inside other programs, where a panic ends the program: whatever comes down the pipe (cut short, a wild length,
+ /// bytes that are not JSON, JSON of the wrong shape) is an error, never a panic.
+ #[test]
+ fn garbage_on_the_pipe_is_an_error_not_a_panic() {
+ let mut seed = 0x2545_f491_4f6c_dd1du64;
+ let mut next = move || {
+ seed ^= seed << 13;
+ seed ^= seed >> 7;
+ seed ^= seed << 17;
+ seed
+ };
+ for round in 0..2000 {
+ let n = (next() % 64) as usize;
+ let mut bytes: Vec<u8> = (0..n).map(|_| next() as u8).collect();
+ if round % 3 == 0 && bytes.len() >= 4 {
+ // a believable length in front of the garbage
+ let len = (bytes.len() - 4) as u32;
+ bytes[..4].copy_from_slice(&len.to_le_bytes());
+ }
+ let _ = super::read_msg::<Request>(&mut bytes.as_slice());
+ let _ = super::read_msg::<Response>(&mut bytes.as_slice());
+ }
+ let mut wrong_shape = Vec::new();
+ super::write_msg(&mut wrong_shape, &serde_json::json!({ "mode": 7, "paths": "x" })).unwrap();
+ assert!(super::read_msg::<Response>(&mut wrong_shape.as_slice()).is_err());
+ let huge = (u32::MAX).to_le_bytes();
+ assert!(super::read_msg::<Request>(&mut huge.as_slice()).is_err());
+ }
+
#[test]
fn round_trip() {
let r = Request { mode: Mode::Save, title: "Kaydet".into(), filters: vec![Filter { name: "Metin".into(), spec: "*.txt".into() }], filter_index: 1, ..Default::default() };