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

Calling clone on argument in write! causes stackoverflow #54200

Closed
klingtnet opened this issue Sep 13, 2018 · 7 comments
Closed

Calling clone on argument in write! causes stackoverflow #54200

klingtnet opened this issue Sep 13, 2018 · 7 comments

Comments

@klingtnet
Copy link
Contributor

klingtnet commented Sep 13, 2018

I just stumbled across this issue in a small tool I'm developing.
At the moment I can't pinpoint what the cause of the stack overflow is but running the example below will result in this terminal output:

thread 'main' has overflowed its stack
fatal runtime error: stack overflow
timeout: the monitored command dumped core

This can be reproduced for stable (1.29), beta and nightly channels.

Here is a minimal example (playground):

use std::fmt::{Display, Formatter, Result};

#[derive(Clone)]
enum E {
    A,
}
impl Display for E {
    fn fmt(&self, f: &mut Formatter) -> Result {
        write!(f, "{}", self.clone())
    }
}

fn main() {
    let e = E::A;
    println!("{}", e);
}
@klingtnet
Copy link
Contributor Author

Side note, this compiles without any warnings.

@sfackler
Copy link
Member

write!(f, "{}", self.clone()) calls the Display implementation of E, which calls write(f, "{}", self.clone()), which calls the Display implementation of E, which...

@Havvy
Copy link
Contributor

Havvy commented Sep 13, 2018

This also happens without the .clone(). It's entirely an expected stack overflow.

@klingtnet
Copy link
Contributor Author

klingtnet commented Sep 13, 2018

It's entirely an expected stack overflow.

With this context knowledge, then yes. This should be at least a clippy lint?

@sfackler
Copy link
Member

This is basically a duplicate of #45838 I think.

@klingtnet
Copy link
Contributor Author

klingtnet commented Sep 13, 2018

Looks like it, a call to to_string() causes a stack overflow as well.

@sfackler
Copy link
Member

Closing as a duplicate of that then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants