Reject negative and fractional index in file.openRecent

Found while testing crates/engine/src/cmd/file/open.rs:33 (open_recent, `.nth(i.max(0.0) as usize)`)

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
NetterSilverandClaude Haiku 5.5 committed 2026-11-08 19:22:04 +00:00
1 parent 8a7ebc2484
commit 0ff3a3682e
2 files changed
+21 -1

No files matched your search

+4 -1
View File
@@ -31,7 +31,10 @@ pub(super) fn open_recent(s: &mut Session, p: &Value) -> Result<Value> {
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"))?)
if !(i >= 0.0 && i.fract() == 0.0) {
return Err(bad(CMD, "no such recent file"));
}
Some(s.prefs.recent_files.iter().filter(|r| listed(r)).nth(i as usize).cloned().ok_or_else(|| bad(CMD, "no such recent file"))?)
}
_ => None,
};
+17
View File
@@ -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);
}