Skip to content

Different output in Unix between Path.GetPathRoot overloads for redundant segments #35260

Closed

Description

@carlossanlop

While working on this issue (PR), I found a bug in the string overload for Path.GetPathRoot (Unix):

using System;
using System.IO;
namespace ConsoleAppCore
{
    class Program
    {
        static void Main()
        {
            string original = "/home/../folder/../..";
            string expected = "/";
            ReadOnlySpan<char> actualSpan = Path.GetPathRoot(original.AsSpan());
            string actualString = Path.GetPathRoot(original);
            Console.WriteLine($"Expected:      '{expected}'");
            Console.WriteLine($"Actual string: '{actualString}'");          // This returns '/'
            Console.WriteLine($"Actual span:   '{actualSpan.ToString()}'"); // This returns ''
        }
    }
}

I also found that running the same code in Windows will yield the behavior below, which is why I originally opened this issue, but @jkotas kindly pointed out it's expected behavior - the string method overload normalizes, while the span overload does not:

            Console.WriteLine($"Actual span:   '{actualSpan.ToString()}'"); // This returns '/'
            Console.WriteLine($"Actual string: '{actualString}'");          // This returns '\'

Activity

  1. added this to the 5.0 milestone on Apr 21, 2020
  2. self-assigned this
    on Apr 21, 2020
  3. carlossanlop commented on Apr 21, 2020

    @carlossanlop
    ContributorAuthor

    Assigning to myself to investigate and fix since it's blocking my redundant segments PR.

  4. removed
    untriagedNew issue has not been triaged by the area owner
    on Apr 21, 2020
  5. jkotas commented on Apr 21, 2020

    @jkotas
    Member

    FWIW, this is by design. From https://docs.microsoft.com/en-us/dotnet/api/system.io.path.getpathroot?view=netcore-3.1#System_IO_Path_GetPathRoot_System_ReadOnlySpan_System_Char__ :

    Unlike the string overload, this method doesn't normalize directory separators.

  6. carlossanlop commented on Apr 22, 2020

    @carlossanlop
    ContributorAuthor

    Thanks, @jkotas. I assume the behavior is different because we wanted to prevent introducing a breaking change in the old string overload method, and the new method had the behavior corrected to prevent normalization. I found a couple of places in our own code where the string overload of GetPathRoot is being used to normalize a path.

  7. carlossanlop commented on Apr 28, 2020

    @carlossanlop
    ContributorAuthor

    I'm reopening this issue because I reviewed my notes more closely and realized the actual bug is in Unix. I edited the original description with the details.

  8. changed the title [-]Different output between Path.GetPathRoot overloads for redundant segments[/-] [+]Different output in Unix between Path.GetPathRoot overloads for redundant segments[/+] on Apr 28, 2020
  9. carlossanlop commented on Apr 28, 2020

    @carlossanlop
    ContributorAuthor

    Found the root cause - The IsEffectivelyEmpty check is wrong:

    public static ReadOnlySpan<char> GetPathRoot(ReadOnlySpan<char> path)
    {
    return PathInternal.IsEffectivelyEmpty(path) && IsPathRooted(path) ? PathInternal.DirectorySeparatorCharAsString.AsSpan() : ReadOnlySpan<char>.Empty;
    }

    It should be inverted to match the same way we check for empty in the string overload:

    public static string? GetPathRoot(string? path)
    {
    if (PathInternal.IsEffectivelyEmpty(path)) return null;
    return IsPathRooted(path) ? PathInternal.DirectorySeparatorCharAsString : string.Empty;
    }

    I also noticed we do not have unit tests verifying this method. I'll include this fix in my PR for removing redundant segments, which also adds unit tests for the case that helped me find this problem.

  10. jkotas commented on Apr 28, 2020

    @jkotas
    Member

    I would be better to submit the fix for the bug and the associated test coverage in separate PR.

  11. ghost locked as resolved and limited conversation to collaborators on Dec 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions