Skip to content

Stack overflow using the SpanFormatter #242

Closed as not planned
Closed as not planned
@Keboo

Description

@Keboo

The following code throws a stack overflow exception.

var formatter = new SpanFormatter();
formatter.AddFormatter<int>(x => $"Int: {x}");
Span span = formatter.Format(42);

The issue is the SpanFormatter recurses on the parameter, and each time looks up the formatter, which causes it to loop back on itself.

If you specify a format parameter the problem no longer occurs:

var formatter = new SpanFormatter();
formatter.AddFormatter<int>(x => $"Int: {x:F0}");
Span span = formatter.Format(42);

This is due to the different code path that handles the format.

We also need to consider how to handle this in the generic case. Consider what happens if a format specifier is declared on string.

var formatter = new SpanFormatter();
formatter.AddFormatter<string>(x => $"Prefix: {x}");
Span span = formatter.Format("42");

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions