Skip to content

Commit

Permalink
guard against stack overflows from deeply nested boxes
Browse files Browse the repository at this point in the history
  • Loading branch information
kaikalii committed Oct 16, 2024
1 parent 3daa0fc commit 759dccd
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/grid_fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type Metagrid = Grid<Grid>;
pub struct GridFmtParams {
pub boxed: bool,
pub label: bool,
pub depth: usize,
}

pub trait GridFmt {
Expand Down Expand Up @@ -165,6 +166,9 @@ impl GridFmt for Complex {

impl GridFmt for Value {
fn fmt_grid(&self, params: GridFmtParams) -> Grid {
if params.depth > 100 {
return vec!["…".to_string().chars().collect()];
}
if self.meta().flags.contains(ArrayFlags::SEED) && self.rank() == 0 {
if let Value::Num(arr) = self {
let seed = arr.data[0].to_bits();
Expand All @@ -186,6 +190,7 @@ impl GridFmt for Value {
for Boxed(val) in &b.data {
let grid = val.fmt_grid(GridFmtParams {
boxed: false,
depth: params.depth + 1,
..params
});
if grid.len() == 1 {
Expand Down Expand Up @@ -214,8 +219,11 @@ impl GridFmt for Value {
Value::Num(n) => n.fmt_grid(params),
Value::Byte(b) => b.fmt_grid(params),
Value::Complex(c) => c.fmt_grid(params),
Value::Box(v) => v.fmt_grid(params),
Value::Char(c) => c.fmt_grid(params),
Value::Box(v) => v.fmt_grid(GridFmtParams {
depth: params.depth + 1,
..params
}),
}
}
}
Expand Down

0 comments on commit 759dccd

Please sign in to comment.