diff --git a/crates/engine/src/cmd/file/open.rs b/crates/engine/src/cmd/file/open.rs index e4c3e0fb7..ea9a0d90a 100644 --- a/crates/engine/src/cmd/file/open.rs +++ b/crates/engine/src/cmd/file/open.rs @@ -30,8 +30,9 @@ pub(super) fn open_recent(s: &mut Session, p: &Value) -> Result { allow(&path, Access::Read)?; Some(path) } - (None, Some(i)) => { - Some(s.prefs.recent_files.iter().filter(|r| listed(r)).nth(i.max(0.0) as usize).cloned().ok_or_else(|| bad(CMD, "no such recent file"))?) + (None, Some(_)) => { + let i = req_index(CMD, p, "index")?; + Some(s.prefs.recent_files.iter().filter(|r| listed(r)).nth(i).cloned().ok_or_else(|| bad(CMD, "no such recent file"))?) } _ => None, }; diff --git a/crates/engine/src/menu_gaps_tests.rs b/crates/engine/src/menu_gaps_tests.rs index 057c5119a..fa32f5d8f 100644 --- a/crates/engine/src/menu_gaps_tests.rs +++ b/crates/engine/src/menu_gaps_tests.rs @@ -84,3 +84,20 @@ fn outlined_layers_draw_as_outlines_and_the_rest_normally() { assert_eq!(outlined.pixel(20, 20).unwrap(), empty, "only its outline"); assert_eq!(outlined.pixel(80, 20).unwrap(), plain.pixel(80, 20).unwrap(), "the other layer draws normally"); } + +#[test] +fn open_recent_rejects_negative_and_fractional_index() { + let dir = std::env::temp_dir().join(format!("figcraft-recent-bad-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let a = dir.join("a.figcraft").to_string_lossy().to_string(); + let b = dir.join("b.figcraft").to_string_lossy().to_string(); + let mut s = Session::new(); + s.execute("file.save", &json!({"path": a})).unwrap(); + s.execute("file.saveAs", &json!({"path": b})).unwrap(); + let recent = s.prefs.recent_files.clone(); + for i in [json!(-1), json!(1.5), json!(-0.5)] { + assert!(s.execute("file.openRecent", &json!({"index": i})).is_err(), "index {i} must be an error"); + } + assert_eq!(s.prefs.recent_files, recent); + let _ = std::fs::remove_dir_all(&dir); +}