Skip to content

Commit 78288bc

Browse files
committed
types: cut off display after 10000 nodes
We have logic to output `...` when a type gets too deep and to stop recursing at that point. Most notably this is useful when outputting an occurs-check-failing type. But the max depth is 64, so you can make a type of size 2^64 without hitting this limit, which will still blow out your memory and effectively stall. This can create pretty confusing symptoms when fuzzing for pathological programs -- you will get an apparent stall with no output, and no matter how many debug printlns you add it will appear that they all execute before the stall -- and it turns out that in fact all your code was working correctly up to the point of the final panic where it tries to *display* an occurs-check error. Cut off the display at 10000 nodes. If somebody really needs to output a text representation of such a thing they can write their own logic.
1 parent 23c3ffb commit 78288bc

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

src/types/mod.rs

+9
Original file line numberDiff line numberDiff line change
@@ -368,12 +368,17 @@ impl Type {
368368
}
369369

370370
const MAX_DISPLAY_DEPTH: usize = 64;
371+
const MAX_DISPLAY_LENGTH: usize = 10000;
371372

372373
impl fmt::Debug for Type {
373374
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
374375
for data in (&self.ctx, self.inner.bound.root())
375376
.verbose_pre_order_iter::<NoSharing>(Some(MAX_DISPLAY_DEPTH))
376377
{
378+
if data.index > MAX_DISPLAY_LENGTH {
379+
write!(f, "... [truncated type after {} nodes]", MAX_DISPLAY_LENGTH)?;
380+
return Ok(());
381+
}
377382
if data.depth == MAX_DISPLAY_DEPTH {
378383
if data.n_children_yielded == 0 {
379384
f.write_str("...")?;
@@ -407,6 +412,10 @@ impl fmt::Display for Type {
407412
for data in (&self.ctx, self.inner.bound.root())
408413
.verbose_pre_order_iter::<NoSharing>(Some(MAX_DISPLAY_DEPTH))
409414
{
415+
if data.index > MAX_DISPLAY_LENGTH {
416+
write!(f, "... [truncated type after {} nodes]", MAX_DISPLAY_LENGTH)?;
417+
return Ok(());
418+
}
410419
if data.depth == MAX_DISPLAY_DEPTH {
411420
if data.n_children_yielded == 0 {
412421
f.write_str("...")?;

0 commit comments

Comments
 (0)