Unreachable code in RazorPagesRazorViewEngineOptionsSetup.cs

Open Beginner friendly
#67,151 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
78/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Quiet
Tech stack
csharp
Domain
web-dev

Research direction

Start in src/Mvc/Mvc.RazorPages/src/DependencyInjection/RazorPagesRazorViewEngineOptionsSetup.cs at the CombinePath method and inspect the conditions around lines 65-72. Reorder or refactor the checks as proposed, then verify that paths with both trailing and leading slashes are combined without duplicating the slash while the other cases retain their existing behavior.

Written by the indexing model from the issue text.

Description

area-mvc

Hello!

Description

There is a potential logical bug in the internal RazorPagesRazorViewEngineOptionsSetup.CombinePath method. The else if condition check is unreachable because the preceding if condition completely overlaps it.

https://github.com/dotnet/aspnetcore/blob/fc4c0464f6a5b02d8763d901fe885d1dd12996d9/src/Mvc/Mvc.RazorPages/src/DependencyInjection/RazorPagesRazorViewEngineOptionsSetup.cs#L65-L72

If both path1.EndsWith('/') and path2.StartsWith('/') are true, the first if block triggers because it uses the logical OR (||) operator. As a result, the else if block with the logical AND (&&) operator is dead code and never executes.

Proposed Fix

The conditions should be reordered so the stricter strict check (&&) comes first:

if (path1.EndsWith('/') && path2.StartsWith('/'))
{
    return string.Concat(path1, path2.AsSpan(1));
}
else if (path1.EndsWith('/') || path2.StartsWith('/'))
{
    return path1 + path2;
}

Or refactored into a switch expression.

Please review this when you have a moment. Thank you!

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

Dominant language
C#
Stars
38.5k
Forks
11.6k
Avg merge
2d 3h
Merged PRs (30d)
243

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dotnet/aspnetcore

All issues in dotnet/aspnetcore

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.