Merge pull request #608 from Silvertree2010/fix/h1871-file-close-with-a-negative-index

Reject negative or fractional index in file.close
This commit is contained in:
skylar taylor-barrick
2026-10-15 18:28:25 -06:00
committed by GitHub
2 changed files with 14 additions and 1 deletions
+1 -1
View File
@@ -1280,7 +1280,7 @@ fn append_pages(doc: &mut Document, other: &Document) -> Result<Vec<NodeId>> {
}
fn close(s: &mut Session, p: &Value) -> Result<Value> {
let i = num(p, "index").map(|x| x as usize).unwrap_or(s.active);
let i = if get(p, "index").is_none() { s.active } else { req_index("file.close", p, "index")? };
let Some(ds) = s.docs.get(i) else { return Err(bad("file.close", "no such document")) };
// A branch tab's edits are in its main file already (which then has unsaved changes).
if ds.branch_of.is_none() && ds.is_dirty() && !boolean(p, "force").unwrap_or(false) {
+13
View File
@@ -49,3 +49,16 @@ fn an_intact_file_opens_without_a_message() {
assert!(s.message.is_none());
let _ = std::fs::remove_dir_all(path.parent().unwrap());
}
#[test]
fn file_close_rejects_a_bad_index_instead_of_closing_another_document() {
let mut s = Session::new();
s.execute("file.new", &json!({})).unwrap();
s.execute("file.new", &json!({})).unwrap();
let docs = s.docs.len();
for index in [json!(-1), json!(1.5), json!(99)] {
let r = s.execute("file.close", &json!({"index": index, "force": true}));
assert!(r.is_err(), "index {index} must be rejected, got {r:?}");
assert_eq!(s.docs.len(), docs, "index {index} closed a document");
}
}