Skip to content

Unreachable code in Mono.Options GetArgumentName (always false do-while condition) #126538

Closed

Description

@rustamque

Hello! While running static analysis on the codebase, an unreachable code warning was flagged in Options.cs inside the GetArgumentName method.

In the following block, the variable j is initialized to 0 and is not modified before the while condition is evaluated:

int start, j = 0;
do {
    start = description.IndexOf (nameStart [i], j);
} while (start >= 0 && j != 0 ? description [j++ - 1] == '{' : false);

Because j is exactly 0 at the end of the first iteration, the ternary operator j != 0 ? ... : false always evaluates to false. This means the loop guarantees to exit after the first iteration, making the check description [j++ - 1] == '{' completely unreachable dead code.

Since this code is quite old, we are unsure if the do-while loop was originally intended to perform a deeper search (and a variable increment is missing), or if the loop is simply redundant and can be safely replaced with a single IndexOf call.

Could you please take a look and advise if this needs a logic fix or just a cleanup?

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Activity

  1. added
    tenet-performance
    on Apr 4, 2026
  2. added
    untriaged
    on Apr 4, 2026
  3. KalleOlaviNiemitalo commented on Apr 5, 2026

    @KalleOlaviNiemitalo

    You refer to this loop in OptionSet.GetArgumentName:

    int start, j = 0;
    do
    {
    start = description.IndexOf(nameStart[i], j);
    } while (start >= 0 && j != 0 ? description[j++ - 1] == '{' : false);

    It originated in mono/mono@3e206c4#diff-d2a050f05daebe430e526b31f34e3f616b80774900673e21b524a57822bbc31aR990-R993.

    It was copied to dotnet/linker in dotnet/linker@2e5c599#diff-f2ae5aec3d6f84439a0f15e836bcb9f2397a04461356a037a64fdf5cf51f9e51R1403-R1406 and from there to dotnet/runtime.

    GetArgumentName is used for when outputting help for a command-line option. It searches an Option.Description string for markup like {name} or {0:name} (or some other number) and then returns name. The purpose of the suspicious loop seems to be to ignore matches that are preceded by another opening brace, like {{name}. That does not work and the whole GetArgumentName method was rewritten in mono/mono#6506 to use regular expressions instead.

    AFAICT, this copy of Mono.Options is used only in src/tools/illink/src/tlens/TLens/Driver.cs. There, the Option.Description strings do not include doubled opening braces so the bug has no effect.

    I imagine the static analysis warning could be fixed by cherry-picking the change from Mono. I think that would be better than diverging from the upstream source by removing the double-opening-brace feature. Although regular expressions may be less efficient than hand-coded loops, it doesn't matter here because the method is called only for command-line help.

    area-Tools-ILLink .NET linker development as well as trimming analyzers might be more appropriate than area-VM-meta-mono .

  4. added
    area-Tools-ILLink
    and removed on Apr 5, 2026
  5. MichalStrehovsky commented on Apr 5, 2026

    @MichalStrehovsky
    Member

    Moved to ILLink. If this is only used in tlens, I think the plan is to just delete tlens (#125275). We should make sure to delete Options.cs too.

  6. dotnet-policy-service commented on Apr 5, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @agocke, @dotnet/illink
    See info in area-owners.md if you want to be subscribed.

  7. self-assigned this
    on Jun 18, 2026
  8. added this to the 11.0.0 milestone on Jun 18, 2026
  9. removed
    untriaged
    on Jun 18, 2026
  10. added a commit that references this issue on Jun 18, 2026
    9ef210e
  11. locked and limited conversation to collaborators on Aug 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-Tools-ILLinktenet-performance

Type

No type

Projects

  • Status
    No status

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions