diff --git a/crates/engine/src/cmd/modify/select_then.rs b/crates/engine/src/cmd/modify/select_then.rs index 91109fe38..2e62c0f1e 100644 --- a/crates/engine/src/cmd/modify/select_then.rs +++ b/crates/engine/src/cmd/modify/select_then.rs @@ -125,19 +125,26 @@ impl SelectThen { let Some(base) = self.pts.first().copied() else { return Ok(Step::Continue) }; let placed = match (self.copy_array, i) { (CopyArray::Count, Input::Enter) => return self.copy_done(s), - (CopyArray::Count, Input::Text(t)) => { - // MAXARRAY caps the copies (items after the first, times the objects), as it - // caps ARRAY's. - let per_item = u64::try_from(self.objs.len()).unwrap_or(u64::MAX).max(1); - let max = u16::try_from((s.settings.maxarray / per_item).saturating_add(1)).unwrap_or(MAX_COPY_ITEMS).min(MAX_COPY_ITEMS); - let msg = match max { - MAX_COPY_ITEMS => format!("Requires an integer between 2 and {MAX_COPY_ITEMS}."), - 0 | 1 => "The selection is too large to array (MAXARRAY limits the size of the array).".to_string(), - _ => format!("Requires an integer between 2 and {max} (MAXARRAY limits the size of the array)."), - }; - // A whole number as typed: AutoCAD 2024 takes +3 and 03 here, and refuses 4.0, 1e1, - // 8/2, 1' and 3" (measured). - let n = t.trim().parse::().ok().filter(|v| (2..=max).contains(v)).ok_or(EngineError::Other(msg))?; + (CopyArray::Count, i) => { + // A whole number as typed, from 2 to 32767 whatever the selection: AutoCAD 2024 takes + // +3 and 03 here, refuses 4.0, 1e1, 8/2, 1', 3", 50002 and a point, and doesn't + // apply MAXARRAY (with MAXARRAY 100000, four lines and 26000 items made 104000 + // lines; measured). + let n = match i { + Input::Text(t) => t.trim().parse::().ok().filter(|v| (2..=MAX_COPY_ITEMS).contains(v)), + _ => None, + } + .ok_or_else(|| EngineError::Other(format!("Requires an integer between 2 and {MAX_COPY_ITEMS}.")))?; + // Not AutoCAD's: the copies (items after the first, times the objects) stay within + // ARRAY's own limit, whatever MAXARRAY says, so a large selection can't run out of + // memory. + let copies = u64::from(n - 1).saturating_mul(u64::try_from(self.objs.len()).unwrap_or(u64::MAX)); + if copies > cadcraft_doc::MAX_ARRAY_ITEMS { + return Err(EngineError::Other(format!( + "The array would create {copies} objects; the limit is {}. Use fewer items or select fewer objects.", + cadcraft_doc::MAX_ARRAY_ITEMS + ))); + } self.copy_array = CopyArray::Items { n, fit: false }; return Ok(Step::Continue); } diff --git a/crates/engine/tests/it/copy_modes.rs b/crates/engine/tests/it/copy_modes.rs index ec607affc..16ab8fb9e 100644 --- a/crates/engine/tests/it/copy_modes.rs +++ b/crates/engine/tests/it/copy_modes.rs @@ -166,15 +166,19 @@ fn array_takes_2_to_32767_items() { let mut s = with_line(1); copy(&mut s, &["0,0", "A"]); assert_eq!(s.current_prompt().unwrap().display(), "Enter number of items to array:"); - for bad in ["1", "0", "2.5", "32768", "-3", "1'", "8/2", "x", "4.0", "1e1", "3\""] { + // A point from LISP is refused too (AutoCAD 2024 with '(1 2): the same message). + for bad in ["1", "0", "2.5", "32768", "-3", "1'", "8/2", "x", "4.0", "1e1", "3\"", "(list 1 2)"] { + let logged = s.log.len(); s.cmdline(bad).unwrap(); - assert_eq!(s.log.last().map(String::as_str), Some("Requires an integer between 2 and 32767."), "{bad}"); + let said: Vec<&str> = s.log.iter().skip(logged).map(String::as_str).filter(|l| l.starts_with("Requires")).collect(); + assert_eq!(said, ["Requires an integer between 2 and 32767."], "{bad}"); assert_eq!(s.current_prompt().unwrap().display(), "Enter number of items to array:", "{bad}"); } s.cmdline("32767").unwrap(); assert_eq!(s.current_prompt().unwrap().display(), "Specify second point or [Fit]:"); - // A sign or a leading zero is still a whole number (AutoCAD 2024: three items each). - for three in ["+3", "03"] { + // A sign or a leading zero is still a whole number, and so is the LISP real 3.0 (AutoCAD 2024: + // three items each). + for three in ["+3", "03", "(+ 1.5 1.5)"] { let mut s = with_line(1); copy(&mut s, &["0,0", "A", three, "5,0"]); assert_eq!(starts(&s), [(0.0, 0.0), (5.0, 0.0), (10.0, 0.0)], "{three}"); @@ -187,26 +191,43 @@ fn array_takes_2_to_32767_items() { } #[test] -fn maxarray_caps_the_items_times_the_objects() { - // Two lines and MAXARRAY 100 (its least): 50 copies of each at most, so 51 items. +fn maxarray_doesnt_limit_the_items() { + // AutoCAD 2024 with MAXARRAY 100000: four lines and 26000 items made 104000 lines (measured). + // Here MAXARRAY is 100 (its least): two lines and 60 items, and 101 lines and 2 items, go past + // it and are still made. let mut s = with_line(1); s.execute("line", &json!({ "points": [[0, 5], [1, 5]] })).unwrap(); s.execute("setvar", &json!({ "name": "MAXARRAY", "value": 100 })).unwrap(); - copy(&mut s, &["0,0", "A", "52"]); - assert_eq!(s.log.last().map(String::as_str), Some("Requires an integer between 2 and 51 (MAXARRAY limits the size of the array).")); - for t in ["51", "10,0"] { - s.cmdline(t).unwrap(); - } + copy(&mut s, &["0,0", "A", "60", "10,0"]); assert!(s.current_prompt().is_none()); - assert_eq!(starts(&s).len(), 102); - // More objects than MAXARRAY: no array at all. + assert_eq!(starts(&s).len(), 120); let mut s = with_line(1); s.execute("setvar", &json!({ "name": "MAXARRAY", "value": 100 })).unwrap(); for y in 1..=100 { s.execute("line", &json!({ "points": [[0, y], [1, y]] })).unwrap(); } - copy(&mut s, &["0,0", "A", "2"]); - assert_eq!(s.log.last().map(String::as_str), Some("The selection is too large to array (MAXARRAY limits the size of the array).")); + copy(&mut s, &["0,0", "A", "2", "10,0"]); + assert!(s.current_prompt().is_none()); + assert_eq!(starts(&s).len(), 202); +} + +#[test] +fn the_copies_stay_within_ten_million() { + // Not AutoCAD's limit, ARRAY's: 400 lines times 25000 more items is exactly 10 million copies + // and is taken; one item more is refused before anything is made. + let mut s = with_line(1); + for y in 1..400 { + s.execute("line", &json!({ "points": [[0, y], [1, y]] })).unwrap(); + } + copy(&mut s, &["0,0", "A", "25002"]); + assert_eq!( + s.log.last().map(String::as_str), + Some("The array would create 10000400 objects; the limit is 10000000. Use fewer items or select fewer objects.") + ); + assert_eq!(s.current_prompt().unwrap().display(), "Enter number of items to array:"); + assert_eq!(starts(&s).len(), 400); + s.cmdline("25001").unwrap(); + assert_eq!(s.current_prompt().unwrap().display(), "Specify second point or [Fit]:"); } #[test]