Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion crates/engine/src/cmd/modify/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ pub(super) fn run_move(s: &mut Session, p: &Value) -> Result<Value> {
pub(super) fn run_copy(s: &mut Session, p: &Value) -> Result<Value> {
let hs = targets(s, p)?;
let d = delta_of("copy", p)?;
let n = p.get("count").and_then(Value::as_u64).unwrap_or(1).clamp(1, 10_000);
let n = match p.get("count") {
None => 1,
Some(v) => v.as_u64().filter(|n| (1..=10_000).contains(n)).ok_or_else(|| bad("copy", "`count` must be an integer from 1 to 10000"))?,
};
let mut out = Vec::new();
for k in 1..=n {
out.extend(transform_entities(s, &hs, &Mat3::translate(d * k as f64), true)?.into_iter().map(|h| h.hex()));
Expand Down
24 changes: 24 additions & 0 deletions crates/engine/tests/it/copy_count.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//! JSON COPY refuses a `count` that is not an integer from 1 to 10000 instead of clamping it.

use cadcraft_engine::Session;
use serde_json::{Value, json};

fn entities(s: &Session) -> usize {
s.doc().map(|d| d.model.iter().count()).unwrap_or(0)
}

#[test]
fn invalid_copy_count_is_refused() {
let mut s = Session::new();
let r = s.execute("line", &json!({"points": [[0, 0], [10, 0]]})).unwrap();
let h = r["handles"][0].clone();
let before = entities(&s);
let bad: [Value; 7] = [json!(0), json!(-3), json!(2.5), json!("abc"), json!(null), json!(10001), json!(50000)];
for c in bad {
let r = s.execute("copy", &json!({"handles": [h], "delta": [0, 5], "count": c}));
assert!(r.is_err(), "count {c} should be refused");
assert_eq!(entities(&s), before, "count {c} changed the drawing");
}
s.execute("copy", &json!({"handles": [h], "delta": [0, 5], "count": 3})).unwrap();
assert_eq!(entities(&s), before + 3);
}
1 change: 1 addition & 0 deletions crates/engine/tests/it/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ mod burst;
mod compare_export;
mod constraint_geometry;
mod constraint_infer;
mod copy_count;
mod count;
mod dash_block;
mod data_extraction;
Expand Down
Loading