Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stack overflow in fmt::Display impl #45838

Open
brayniac opened this issue Nov 7, 2017 · 3 comments
Open

Stack overflow in fmt::Display impl #45838

brayniac opened this issue Nov 7, 2017 · 3 comments
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-feature-request Category: A feature request, i.e: not implemented / a PR. E-hard Call for participation: Hard difficulty. Experience needed to fix: A lot. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@brayniac
Copy link

brayniac commented Nov 7, 2017

It's possible to write a impl of fmt::Display which compiles successfully and produces a stack overflow.

A minimal repro is available here: https://play.rust-lang.org/?gist=fb115e1e625e1b8038cdd13c5f9cdbb8&version=stable

The code is here for completeness of the bug report:

use std::fmt;

fn main() {
    println!("Hello, world!");
    let test = Test::Alpha;
    println!("Preparing to stack overflow");
    let test_string = test.to_string();
    println!("test string: {}", test_string);
}

pub enum Test {
  Alpha,
  Bravo,
}

impl fmt::Display for Test {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    write!(f, "{}", self.to_string())
  }
}

I would expect an error at compile time

Instead the error occurs at runtime as a stack overflow.

Meta

rustc 1.21.0 (3b72af97e 2017-10-09)
binary: rustc
commit-hash: 3b72af97e42989b2fe104d8edbaee123cdf7c58f
commit-date: 2017-10-09
host: x86_64-apple-darwin
release: 1.21.0
LLVM version: 4.0
thread 'main' has overflowed its stack
fatal runtime error: stack overflow
Abort trap: 6
@kennytm
Copy link
Member

kennytm commented Nov 7, 2017

Rust does not emit any compiler error for stack overflow, except the very simple ones like fn f() { f(); }. The following will blow the stack without any warnings or errors as well:

fn f() {
    g();
}
fn g() {
    f();
}
fn main() {
    f();
}

@kennytm kennytm added A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-feature-request Category: A feature request, i.e: not implemented / a PR. labels Nov 7, 2017
@brayniac
Copy link
Author

brayniac commented Nov 7, 2017

I think the interesting thing about this one is that ToString isn't implemented for the enum, except by having the impl for fmt::Display. Maybe this makes it easier to detect and throw an error?

@jonas-schievink
Copy link
Contributor

Marking as E-hard since this requires not just a fix for #57965, but also a complete rewrite of the formatting infrastructure to avoid dynamic dispatch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-feature-request Category: A feature request, i.e: not implemented / a PR. E-hard Call for participation: Hard difficulty. Experience needed to fix: A lot. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants