mirror of
https://github.com/storytold/pdfcraft.git
synced 2026-10-29 20:47:32 -06:00
Fix overflow panic for i64::MIN index in content_update/delete (#2979)
Found while testing crates/automation/src/content.rs:141 Co-authored-by: Claude Haiku 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Haiku 5.5
parent
70d7df548e
commit
17ddc8707d
@@ -138,7 +138,7 @@ impl Automation {
|
||||
.added()
|
||||
.iter()
|
||||
.filter(|x| x.page == page)
|
||||
.nth((index - 1).max(0) as usize)
|
||||
.nth(usize::try_from(index.saturating_sub(1)).unwrap_or(usize::MAX))
|
||||
.filter(|_| index >= 1)
|
||||
.ok_or_else(|| failed(format!("page {} has no added item {index} (see content_list)", page + 1)))?;
|
||||
Ok((page, index as usize - 1, it.content.clone(), ph))
|
||||
|
||||
@@ -3582,6 +3582,24 @@ fn adding_content_through_tools() {
|
||||
assert!(matches!(a.call("content_delete", &json!({ "doc": doc, "page": 1, "index": 5 })), Err(ToolError::Failed(_))));
|
||||
}
|
||||
|
||||
/// An out-of-range `index` (including `i64::MIN`, which overflowed `index - 1`) is a readable
|
||||
/// failure for `content_update` and `content_delete`, not a panic.
|
||||
#[test]
|
||||
fn content_index_out_of_range_is_an_error_not_a_panic() {
|
||||
let dir = workdir("content-index-range");
|
||||
let mut a = auto(&dir);
|
||||
let doc = ok(&mut a, "doc_open", json!({ "path": "a.pdf" }))["doc"].as_u64().unwrap();
|
||||
ok(&mut a, "page_add_text", json!({ "doc": doc, "page": 1, "text": "hi", "at": [10, 10] }));
|
||||
for index in [i64::MIN, i64::MIN + 1, -1, 0, i64::MAX] {
|
||||
for tool in ["content_delete", "content_update"] {
|
||||
let args = json!({ "doc": doc, "page": 1, "index": index, "text": "x" });
|
||||
let args = if tool == "content_delete" { json!({ "doc": doc, "page": 1, "index": index }) } else { args };
|
||||
assert!(matches!(a.call(tool, &args), Err(ToolError::Failed(_))), "{tool} index {index}");
|
||||
}
|
||||
}
|
||||
assert_eq!(ok(&mut a, "content_list", json!({ "doc": doc }))["count"], 1);
|
||||
}
|
||||
|
||||
/// Text styling on an added image is refused (#1331), as image transforms are on text, and the
|
||||
/// refusal leaves the content, the dirty flag and the undo and redo steps as they were.
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user