Skip to content

Stack overflow using the SpanFormatter #242

Closed as not planned
Closed as not planned

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");

Activity

  1. added
    bugSomething isn't working
    on Oct 4, 2018
  2. adamsitnik commented on Jun 19, 2025

    @adamsitnik
    Member

    Hello!

    We have decided to deprecate the System.CommandLine.Rendering package: #2576 (comment)

    We recommend using https://www.nuget.org/packages/Spectre.Console which became the golden standard in the .NET Ecosystem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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