Skip to content

Commit 3f399dd

Browse files
authored
Merge pull request #1063 from Silvertree2010/fix/h886-slide-delete-with-an-invalid-neg
Reject invalid slide index params instead of using the selection
2 parents 8f04a99 + 65564cf commit 3f399dd

2 files changed

Lines changed: 32 additions & 5 deletions

File tree

‎crates/engine/src/cmd/mod.rs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,14 @@ pub(crate) fn str_param<'a>(p: &'a Value, key: &str) -> Option<&'a str> {
318318
pub(crate) fn usize_param(p: &Value, key: &str) -> Option<usize> {
319319
p.get(key).and_then(Value::as_u64).and_then(|v| usize::try_from(v).ok())
320320
}
321+
/// An index param: `Ok(None)` when absent (callers may fall back to the selection), `Err` when
322+
/// present but not a valid index (negative, non-integer), so a bad index never acts on the selection.
323+
pub(crate) fn opt_usize_param(p: &Value, key: &str, cmd: &str) -> Result<Option<usize>> {
324+
match p.get(key) {
325+
None | Some(Value::Null) => Ok(None),
326+
Some(v) => v.as_u64().and_then(|v| usize::try_from(v).ok()).map(Some).ok_or_else(|| bad(cmd, format!("`{key}` is not a valid index: {v}"))),
327+
}
328+
}
321329
/// A shape id param: `Ok(None)` when absent (callers may fall back to the selection), `Err` when
322330
/// present but not a valid id (negative, non-integer, beyond `u32`), so a bad id never edits the selection.
323331
pub(crate) fn id_param(p: &Value, key: &str, cmd: &str) -> Result<Option<ShapeId>> {

‎crates/engine/src/cmd/slide.rs‎

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,15 +179,15 @@ fn body_placeholder(shapes: &mut [deckcraft_model::Shape]) -> Option<&mut deckcr
179179

180180
fn index_of(s: &Session, p: &Value) -> Result<usize> {
181181
let st = s.doc()?;
182-
let i = usize_param(p, "index").unwrap_or(st.selection.slide);
182+
let i = opt_usize_param(p, "index", "slide")?.unwrap_or(st.selection.slide);
183183
if i >= st.doc.slides.len() {
184184
return Err(bad("slide", format!("no slide {i}")));
185185
}
186186
Ok(i)
187187
}
188188

189189
fn duplicate(s: &mut Session, p: &Value) -> Result<Value> {
190-
let indices: Vec<usize> = match usize_param(p, "index") {
190+
let indices: Vec<usize> = match opt_usize_param(p, "index", "slide.duplicate")? {
191191
Some(i) => vec![i],
192192
None => {
193193
let st = s.doc()?;
@@ -240,7 +240,7 @@ fn delete(s: &mut Session, p: &Value) -> Result<Value> {
240240
let st = s.doc()?;
241241
if let Some(a) = p.get("ids").and_then(Value::as_array) {
242242
a.iter().filter_map(Value::as_u64).filter_map(|v| u32::try_from(v).ok()).map(SlideId).collect()
243-
} else if let Some(i) = usize_param(p, "index") {
243+
} else if let Some(i) = opt_usize_param(p, "index", "slide.delete")? {
244244
st.doc.slides.get(i).map(|x| vec![x.id]).unwrap_or_default()
245245
} else if !st.selection.slides.is_empty() {
246246
st.selection.slides.clone()
@@ -266,7 +266,7 @@ fn delete(s: &mut Session, p: &Value) -> Result<Value> {
266266
}
267267

268268
fn move_slide(s: &mut Session, p: &Value) -> Result<Value> {
269-
let from = usize_param(p, "from").unwrap_or(s.doc()?.selection.slide);
269+
let from = opt_usize_param(p, "from", "slide.move")?.unwrap_or(s.doc()?.selection.slide);
270270
let to = usize_param(p, "to").ok_or_else(|| bad("slide.move", "missing `to`"))?;
271271
s.edit(|doc, sel| {
272272
if from >= doc.slides.len() {
@@ -531,7 +531,7 @@ fn rename(s: &mut Session, p: &Value) -> Result<Value> {
531531
}
532532

533533
fn section_add(s: &mut Session, p: &Value) -> Result<Value> {
534-
let at = usize_param(p, "at").unwrap_or(s.doc()?.selection.slide);
534+
let at = opt_usize_param(p, "at", "section.add")?.unwrap_or(s.doc()?.selection.slide);
535535
let name = str_param(p, "name").unwrap_or("Untitled Section").to_string();
536536
s.edit(|doc, _| {
537537
let at = at.min(doc.slides.len().saturating_sub(1));
@@ -648,6 +648,25 @@ mod tests {
648648

649649
use crate::Session;
650650

651+
#[test]
652+
fn invalid_index_params_are_rejected_not_defaulted_to_selection() {
653+
let mut s = Session::with_new();
654+
while s.doc().expect("doc").doc.slides.len() < 3 {
655+
s.execute("slide.new", &json!({})).expect("new slide");
656+
}
657+
for bad in [json!(-1), json!(1.5)] {
658+
for (cmd, p) in [
659+
("slide.delete", json!({"index": bad})),
660+
("slide.duplicate", json!({"index": bad})),
661+
("slide.move", json!({"from": bad, "to": 0})),
662+
("section.add", json!({"at": bad})),
663+
] {
664+
assert!(s.execute(cmd, &p).is_err(), "{cmd} must reject {p}");
665+
assert_eq!(s.doc().expect("doc").doc.slides.len(), 3, "{cmd} {p}");
666+
}
667+
}
668+
}
669+
651670
#[test]
652671
fn duplicate_keeps_section_order_in_step_with_slides() {
653672
let mut s = Session::with_new();

0 commit comments

Comments
 (0)