forked from 1b-agent/opencadstudio-upstream
Merge pull request #1340 from CiberNin/fix/editor-crashes
Fix editor crashes from typed input, text, and degenerate geometry
This commit is contained in:
commit
3c1ad62e9b
12 files changed
+391
-27
No files matched your search
@@ -45,6 +45,27 @@ pub const DROPDOWN_ITEMS: &[(&str, &str, IconKind)] = &[
|
||||
),
|
||||
];
|
||||
|
||||
// ── Item limit ─────────────────────────────────────────────────────────────
|
||||
|
||||
/// Most items one array may create, including the source. Every item becomes
|
||||
/// a transform and then a copy of each selected object, so an unbounded typed
|
||||
/// count exhausts memory before anything is drawn.
|
||||
pub(crate) const MAX_ARRAY_ITEMS: u64 = 100_000;
|
||||
|
||||
/// Whether an array of `counts` (rows, columns, levels, ...) stays in bounds.
|
||||
pub(crate) fn array_items_within_limit(counts: &[u64]) -> bool {
|
||||
counts
|
||||
.iter()
|
||||
.try_fold(1u64, |total, &count| total.checked_mul(count))
|
||||
.is_some_and(|total| total <= MAX_ARRAY_ITEMS)
|
||||
}
|
||||
|
||||
pub(crate) fn array_limit_error() -> CmdResult {
|
||||
CmdResult::ReportError(
|
||||
crate::tf!("Array size exceeds the limit of {} items.", MAX_ARRAY_ITEMS).into_owned(),
|
||||
)
|
||||
}
|
||||
|
||||
// ── Rectangular Array ──────────────────────────────────────────────────────
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
@@ -72,8 +93,8 @@ impl ArrayRectCommand {
|
||||
handles,
|
||||
wire_models,
|
||||
step: RectStep::Rows,
|
||||
default_rows: defaults::get_array_rows() as u32,
|
||||
default_cols: defaults::get_array_cols() as u32,
|
||||
default_rows: (defaults::get_array_rows() as u32).clamp(1, MAX_ARRAY_ITEMS as u32),
|
||||
default_cols: (defaults::get_array_cols() as u32).clamp(1, MAX_ARRAY_ITEMS as u32),
|
||||
default_row_sp: defaults::get_array_row_sp(),
|
||||
default_col_sp: defaults::get_array_col_sp(),
|
||||
plane: WorkingPlane::default(),
|
||||
@@ -154,6 +175,9 @@ impl CadCommand for ArrayRectCommand {
|
||||
self.default_rows
|
||||
} else {
|
||||
let v = t.parse::<u32>().unwrap_or(self.default_rows).max(1);
|
||||
if !array_items_within_limit(&[v as u64]) {
|
||||
return Some(array_limit_error());
|
||||
}
|
||||
defaults::set_array_rows(v as f64);
|
||||
self.default_rows = v;
|
||||
v
|
||||
@@ -165,11 +189,15 @@ impl CadCommand for ArrayRectCommand {
|
||||
let cols = if t.is_empty() {
|
||||
self.default_cols
|
||||
} else {
|
||||
let v = t.parse::<u32>().unwrap_or(self.default_cols).max(1);
|
||||
defaults::set_array_cols(v as f64);
|
||||
self.default_cols = v;
|
||||
v
|
||||
t.parse::<u32>().unwrap_or(self.default_cols).max(1)
|
||||
};
|
||||
if !array_items_within_limit(&[rows as u64, cols as u64]) {
|
||||
return Some(array_limit_error());
|
||||
}
|
||||
if !t.is_empty() {
|
||||
defaults::set_array_cols(cols as f64);
|
||||
self.default_cols = cols;
|
||||
}
|
||||
self.step = RectStep::RowSp { rows, cols };
|
||||
None
|
||||
}
|
||||
@@ -222,6 +250,10 @@ impl CadCommand for ArrayRectCommand {
|
||||
}
|
||||
RectStep::ColSp { rows, cols, row_sp } => (rows, cols, row_sp, self.default_col_sp),
|
||||
};
|
||||
// Defaults are only bounded one count at a time.
|
||||
if !array_items_within_limit(&[rows as u64, cols as u64]) {
|
||||
return vec![];
|
||||
}
|
||||
Self::build_transforms(rows, cols, row_sp, col_sp, self.plane)
|
||||
.iter()
|
||||
.flat_map(|t| {
|
||||
@@ -274,7 +306,7 @@ impl ArrayPolarCommand {
|
||||
handles,
|
||||
wire_models,
|
||||
step: PolarStep::Center,
|
||||
default_count: defaults::get_array_p_count() as u32,
|
||||
default_count: (defaults::get_array_p_count() as u32).clamp(2, MAX_ARRAY_ITEMS as u32),
|
||||
default_angle: defaults::get_array_p_angle(),
|
||||
plane: WorkingPlane::default(),
|
||||
}
|
||||
@@ -330,6 +362,9 @@ impl CadCommand for ArrayPolarCommand {
|
||||
self.default_count
|
||||
} else {
|
||||
let v = t.parse::<u32>().unwrap_or(self.default_count).max(2);
|
||||
if !array_items_within_limit(&[v as u64]) {
|
||||
return Some(array_limit_error());
|
||||
}
|
||||
defaults::set_array_p_count(v as f64);
|
||||
self.default_count = v;
|
||||
v
|
||||
@@ -461,7 +496,7 @@ impl ArrayPathCommand {
|
||||
wire_models,
|
||||
all_entities,
|
||||
step: PathStep::SelectPath,
|
||||
default_count: defaults::get_array_path_count() as u32,
|
||||
default_count: (defaults::get_array_path_count() as u32).clamp(2, MAX_ARRAY_ITEMS as u32),
|
||||
pick_pt: DVec3::ZERO,
|
||||
}
|
||||
}
|
||||
@@ -686,6 +721,9 @@ impl CadCommand for ArrayPathCommand {
|
||||
self.default_count
|
||||
} else {
|
||||
let v = t.parse::<u32>().unwrap_or(self.default_count).max(2);
|
||||
if !array_items_within_limit(&[v as u64]) {
|
||||
return Some(array_limit_error());
|
||||
}
|
||||
defaults::set_array_path_count(v as f64);
|
||||
self.default_count = v;
|
||||
v
|
||||
@@ -891,6 +929,9 @@ impl CadCommand for Array3DCommand {
|
||||
} else {
|
||||
t.parse::<u32>().unwrap_or(2).max(1)
|
||||
};
|
||||
if !array_items_within_limit(&[v as u64]) {
|
||||
return Some(array_limit_error());
|
||||
}
|
||||
self.step = Array3DStep::Cols { rows: v };
|
||||
Some(CmdResult::NeedPoint)
|
||||
}
|
||||
@@ -900,6 +941,9 @@ impl CadCommand for Array3DCommand {
|
||||
} else {
|
||||
t.parse::<u32>().unwrap_or(2).max(1)
|
||||
};
|
||||
if !array_items_within_limit(&[rows as u64, v as u64]) {
|
||||
return Some(array_limit_error());
|
||||
}
|
||||
self.step = Array3DStep::Levels { rows, cols: v };
|
||||
Some(CmdResult::NeedPoint)
|
||||
}
|
||||
@@ -909,6 +953,9 @@ impl CadCommand for Array3DCommand {
|
||||
} else {
|
||||
t.parse::<u32>().unwrap_or(2).max(1)
|
||||
};
|
||||
if !array_items_within_limit(&[rows as u64, cols as u64, v as u64]) {
|
||||
return Some(array_limit_error());
|
||||
}
|
||||
self.step = Array3DStep::RowSp {
|
||||
rows,
|
||||
cols,
|
||||
@@ -987,3 +1034,33 @@ impl CadCommand for Array3DCommand {
|
||||
|
||||
// ── Autocomplete registry ─────────────────────────────────
|
||||
inventory::submit!(crate::command::CommandRegistration { names: &["ARRAY3D", "3DARRAY"] });
|
||||
|
||||
#[cfg(test)]
|
||||
mod item_limit_tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn item_limit_counts_every_dimension_without_overflow() {
|
||||
assert!(array_items_within_limit(&[MAX_ARRAY_ITEMS]));
|
||||
assert!(!array_items_within_limit(&[MAX_ARRAY_ITEMS + 1]));
|
||||
assert!(!array_items_within_limit(&[1_000, 1_000]));
|
||||
assert!(!array_items_within_limit(&[u32::MAX as u64, u32::MAX as u64, u32::MAX as u64]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn oversized_counts_are_rejected_at_the_prompt() {
|
||||
let mut rect = ArrayRectCommand::new(vec![Handle::new(1)], Vec::new());
|
||||
assert!(rect.on_text_input("100000").is_none());
|
||||
assert!(matches!(rect.on_text_input("100000"), Some(CmdResult::ReportError(_))));
|
||||
assert!(matches!(rect.step, RectStep::Cols { rows: 100_000 }));
|
||||
|
||||
let mut polar = ArrayPolarCommand::new(vec![Handle::new(1)], Vec::new());
|
||||
polar.step = PolarStep::Count { center: DVec3::ZERO };
|
||||
assert!(matches!(polar.on_text_input("4294967295"), Some(CmdResult::ReportError(_))));
|
||||
|
||||
let mut cube = Array3DCommand::new(vec![Handle::new(1)]);
|
||||
assert!(matches!(cube.on_text_input("300"), Some(CmdResult::NeedPoint)));
|
||||
assert!(matches!(cube.on_text_input("300"), Some(CmdResult::NeedPoint)));
|
||||
assert!(matches!(cube.on_text_input("2"), Some(CmdResult::ReportError(_))));
|
||||
}
|
||||
}
|
||||
@@ -199,6 +199,9 @@ impl CadCommand for CopyCommand {
|
||||
fn on_text_input(&mut self, text: &str) -> Option<CmdResult> {
|
||||
if self.awaiting_count {
|
||||
if let Ok(n) = text.trim().parse::<usize>() {
|
||||
if !super::array::array_items_within_limit(&[n as u64]) {
|
||||
return Some(super::array::array_limit_error());
|
||||
}
|
||||
if n >= 2 {
|
||||
self.array_count = Some(n);
|
||||
}
|
||||
@@ -277,6 +280,18 @@ mod tests {
|
||||
cmd.options().into_iter().map(|o| o.keyword).collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn array_count_above_the_item_limit_is_rejected() {
|
||||
let mut cmd = CopyCommand::new(vec![Handle::new(1)], vec![]);
|
||||
cmd.awaiting_count = true;
|
||||
assert!(matches!(
|
||||
cmd.on_text_input("18446744073709551615"),
|
||||
Some(CmdResult::ReportError(_))
|
||||
));
|
||||
assert!(cmd.awaiting_count, "the count prompt stays up");
|
||||
assert_eq!(cmd.array_count, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mode_single_exits_after_one_copy() {
|
||||
let mut cmd = CopyCommand::new(vec![Handle::new(1)], vec![]);
|
||||
|
||||
@@ -88,6 +88,16 @@ pub fn explode_entity(entity: &EntityType, document: &CadDocument) -> Vec<Entity
|
||||
EntityType::Polyline2D(p) => explode_polyline2d(p),
|
||||
EntityType::Polyline(p) => explode_polyline(p),
|
||||
EntityType::Polyline3D(p) => explode_polyline3d(p),
|
||||
// A MINSERT expands to one copy of the block per cell; leave arrays
|
||||
// too large to materialize intact instead of exhausting memory.
|
||||
EntityType::Insert(ins)
|
||||
if !super::array::array_items_within_limit(&[
|
||||
ins.row_count as u64,
|
||||
ins.column_count as u64,
|
||||
]) =>
|
||||
{
|
||||
vec![]
|
||||
}
|
||||
EntityType::Insert(ins) => ins
|
||||
.explode_from_document(document)
|
||||
.into_iter()
|
||||
@@ -1478,6 +1488,18 @@ mod tests {
|
||||
use super::*;
|
||||
use acadrust::entities::DimensionLinear;
|
||||
|
||||
#[test]
|
||||
fn minsert_too_large_to_materialize_is_left_intact() {
|
||||
let mut doc = CadDocument::new();
|
||||
let mut block = acadrust::tables::BlockRecord::new("B1");
|
||||
block.handle = doc.allocate_handle();
|
||||
doc.block_records.add(block).unwrap();
|
||||
let mut insert = acadrust::entities::Insert::new("B1", Vector3::new(0.0, 0.0, 0.0));
|
||||
insert.row_count = u16::MAX;
|
||||
insert.column_count = u16::MAX;
|
||||
assert!(explode_entity(&EntityType::Insert(insert), &doc).is_empty());
|
||||
}
|
||||
|
||||
/// A dimension created without a geometry block gets a real `*D` block on
|
||||
/// bake, its `block_name` resolves to that block, and a second bake is a
|
||||
/// no-op (already has a valid block).
|
||||
|
||||
@@ -1062,10 +1062,13 @@ fn fillet_line_arc(
|
||||
+ (p1[1] + a * u[1] - click_line[1]).powi(2);
|
||||
let db = (p1[0] + b * u[0] - click_line[0]).powi(2)
|
||||
+ (p1[1] + b * u[1] - click_line[1]).powi(2);
|
||||
da.partial_cmp(&db).unwrap()
|
||||
da.total_cmp(&db)
|
||||
})?;
|
||||
let ix = p1[0] + t_best * u[0];
|
||||
let iy = p1[1] + t_best * u[1];
|
||||
if !(ix.is_finite() && iy.is_finite()) {
|
||||
return None;
|
||||
}
|
||||
|
||||
// Trim line to intersection
|
||||
let new_line = trim_line_to_point(line, [ix, iy], click_line)?;
|
||||
@@ -1154,9 +1157,11 @@ fn fillet_arc_arc(
|
||||
let ip = *pts.iter().min_by(|a, b| {
|
||||
(a[0] - cx)
|
||||
.hypot(a[1] - cy)
|
||||
.partial_cmp(&(b[0] - cx).hypot(b[1] - cy))
|
||||
.unwrap()
|
||||
.total_cmp(&(b[0] - cx).hypot(b[1] - cy))
|
||||
})?;
|
||||
if !(ip[0].is_finite() && ip[1].is_finite()) {
|
||||
return None;
|
||||
}
|
||||
|
||||
let ia1 = arc_angle_at(c1, ip);
|
||||
let ia2 = arc_angle_at(c2, ip);
|
||||
@@ -1514,7 +1519,7 @@ impl CadCommand for FilletCommand {
|
||||
return Some(CmdResult::NeedPoint);
|
||||
}
|
||||
// "R 5.0" inline shorthand
|
||||
if upper.starts_with('R') {
|
||||
if t.starts_with(['r', 'R']) {
|
||||
let body = t[1..].trim();
|
||||
if let Some(v) = crate::entities::common::parse_typed_length(body) {
|
||||
if v >= 0.0 {
|
||||
@@ -2109,7 +2114,7 @@ impl CadCommand for ChamferCommand {
|
||||
return Some(CmdResult::NeedPoint);
|
||||
}
|
||||
// "D 5.0" or "D 5.0 3.0" inline shorthand
|
||||
if upper.starts_with('D') {
|
||||
if t.starts_with(['d', 'D']) {
|
||||
let body = t[1..].trim();
|
||||
let parts: Vec<f64> = body
|
||||
.split_whitespace()
|
||||
@@ -2368,6 +2373,26 @@ mod tests {
|
||||
cmd.options().into_iter().map(|o| o.keyword).collect()
|
||||
}
|
||||
|
||||
/// The upper half of a radius-5 circle at (5, 0), scaled about the origin
|
||||
/// by an infinite factor: the center becomes (inf, NaN).
|
||||
fn overflowed_arc(sweep_sign: f64) -> ArcEnt {
|
||||
let mut arc = ArcEnt::new();
|
||||
arc.center = acadrust::types::Vector3::new(f64::INFINITY, f64::NAN, 0.0);
|
||||
arc.radius = f64::INFINITY;
|
||||
arc.start_angle = 0.0;
|
||||
arc.end_angle = sweep_sign * std::f64::consts::PI;
|
||||
arc
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn zero_radius_fillet_rejects_non_finite_geometry() {
|
||||
let line = LineEnt::from_coords(f64::NAN, f64::NAN, 0.0, f64::INFINITY, f64::INFINITY, 0.0);
|
||||
let arc = overflowed_arc(1.0);
|
||||
assert!(fillet_line_arc(&line, [5.0, 5.0], &arc, [5.0, 5.0], 0.0, 0.0).is_none());
|
||||
let other = overflowed_arc(-1.0);
|
||||
assert!(fillet_arc_arc(&arc, [5.0, 5.0], &other, [5.0, -5.0], 0.0, 0.0).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fillet_undo_option_appears_after_a_fillet_and_refreshes_from_document() {
|
||||
let mut command = FilletCommand::new(
|
||||
|
||||
@@ -179,12 +179,12 @@ impl CadCommand for ScaleCommand {
|
||||
return Some(CmdResult::NeedPoint);
|
||||
}
|
||||
let factor: f64 = t.replace(',', ".").parse().ok()?;
|
||||
(factor > 0.0).then(|| self.commit(base, factor))
|
||||
(factor.is_finite() && factor > 0.0).then(|| self.commit(base, factor))
|
||||
}
|
||||
Step::RefFirst { base } => {
|
||||
let base = *base;
|
||||
let ref_dist: f64 = t.replace(',', ".").parse().ok()?;
|
||||
if ref_dist > 0.0 {
|
||||
if ref_dist.is_finite() && ref_dist > 0.0 {
|
||||
self.step = Step::RefNew { base, ref_dist };
|
||||
return Some(CmdResult::NeedPoint);
|
||||
}
|
||||
@@ -194,7 +194,8 @@ impl CadCommand for ScaleCommand {
|
||||
Step::RefNew { base, ref_dist } => {
|
||||
let (base, ref_dist) = (*base, *ref_dist);
|
||||
let new_len: f64 = t.replace(',', ".").parse().ok()?;
|
||||
(new_len > 0.0).then(|| self.commit(base, new_len / ref_dist))
|
||||
let factor = new_len / ref_dist;
|
||||
(factor.is_finite() && factor > 0.0).then(|| self.commit(base, factor))
|
||||
}
|
||||
Step::Base => None,
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user