Skip to content

Easy to accidentally cause stack overflows #301

@theRookieCoder

Description

@theRookieCoder

I find that I write

#[derive(thiserror::Error)]
#[error("{}: {}", self, .0)
Foo {
	Bar(#[from] inner::Error),
	...
}

by accident very often.

By writing this, what I intended is for Foo::Bar(inner::Error) to be formatted into a string as "Bar: <display impl for inner::Error>". But that's not what self means. Instead what happens is this code attempts to format self using itself, leading to unconstrained recursion and a stack overflow.

What's problematic about this is there's no indication of the mistake I'm making during compile time or even linting. I'm not sure how this crate could 'fix' this, but I'm curious why self is even allowed to be used in the #[error(...)] macro.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions