Closed
Description
It's possible to implement Display trait in such a way which recursively calls itself until stack is exhausted. There's probably more variations how to encounter this bug. I provide one example.
Consider the following code:
use std::fmt::{Display, Formatter, Result};
struct Foo;
impl Display for Foo {
fn fmt(&self, f: &mut Formatter) -> Result {
write!(f, "{}", self)
}
}
fn main() {
println!("{}", Foo);
}
Compiles perfectly fine, but there's stack trace at run time:
$ cargo run -q
thread 'main' has overflowed its stack
fatal runtime error: stack overflow
Abort trap: 6
The bug is in line write!(f, "{}", self)
which recursively calls itself by trying to use Display trait implementation for self.