Skip to content

Commit 759dccd

Browse files
committed
guard against stack overflows from deeply nested boxes
1 parent 3daa0fc commit 759dccd

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/grid_fmt.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type Metagrid = Grid<Grid>;
2222
pub struct GridFmtParams {
2323
pub boxed: bool,
2424
pub label: bool,
25+
pub depth: usize,
2526
}
2627

2728
pub trait GridFmt {
@@ -165,6 +166,9 @@ impl GridFmt for Complex {
165166

166167
impl GridFmt for Value {
167168
fn fmt_grid(&self, params: GridFmtParams) -> Grid {
169+
if params.depth > 100 {
170+
return vec!["…".to_string().chars().collect()];
171+
}
168172
if self.meta().flags.contains(ArrayFlags::SEED) && self.rank() == 0 {
169173
if let Value::Num(arr) = self {
170174
let seed = arr.data[0].to_bits();
@@ -186,6 +190,7 @@ impl GridFmt for Value {
186190
for Boxed(val) in &b.data {
187191
let grid = val.fmt_grid(GridFmtParams {
188192
boxed: false,
193+
depth: params.depth + 1,
189194
..params
190195
});
191196
if grid.len() == 1 {
@@ -214,8 +219,11 @@ impl GridFmt for Value {
214219
Value::Num(n) => n.fmt_grid(params),
215220
Value::Byte(b) => b.fmt_grid(params),
216221
Value::Complex(c) => c.fmt_grid(params),
217-
Value::Box(v) => v.fmt_grid(params),
218222
Value::Char(c) => c.fmt_grid(params),
223+
Value::Box(v) => v.fmt_grid(GridFmtParams {
224+
depth: params.depth + 1,
225+
..params
226+
}),
219227
}
220228
}
221229
}

0 commit comments

Comments
 (0)