Skip to content

Stack overflow in fmt::Display impl #45838

Open
@brayniac

Description

@brayniac

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-feature-requestCategory: A feature request, i.e: not implemented / a PR.E-hardCall for participation: Hard difficulty. Experience needed to fix: A lot.L-unconditional_recursionLint: unconditional_recursionT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions