You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Different output in Unix between Path.GetPathRoot overloads for redundant segments #35260
While working on this issue (PR), I found a bug in the string overload for Path.GetPathRoot (Unix):
usingSystem;usingSystem.IO;namespaceConsoleAppCore{classProgram{staticvoidMain(){stringoriginal="/home/../folder/../..";stringexpected="/";ReadOnlySpan<char>actualSpan=Path.GetPathRoot(original.AsSpan());stringactualString=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 '\'
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.
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.
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
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.
Activity
carlossanlop commentedon Apr 21, 2020
Assigning to myself to investigate and fix since it's blocking my redundant segments PR.
jkotas commentedon Apr 21, 2020
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.
carlossanlop commentedon Apr 22, 2020
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.
carlossanlop commentedon Apr 28, 2020
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.
[-]Different output between Path.GetPathRoot overloads for redundant segments[/-][+]Different output in Unix between Path.GetPathRoot overloads for redundant segments[/+]carlossanlop commentedon Apr 28, 2020
Found the root cause - The
IsEffectivelyEmptycheck is wrong:runtime/src/libraries/System.Private.CoreLib/src/System/IO/Path.Unix.cs
Lines 130 to 133 in 0a2f0e6
It should be inverted to match the same way we check for empty in the string overload:
runtime/src/libraries/System.Private.CoreLib/src/System/IO/Path.Unix.cs
Lines 123 to 128 in 0a2f0e6
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.
jkotas commentedon Apr 28, 2020
I would be better to submit the fix for the bug and the associated test coverage in separate PR.