Skip to content

Commit d4e2f59

Browse files
committed
trace2: fix pipe/socket name parsing
There are a few bugs in the way we read a pipe or socket name for the TRACE2 event target. We do not correctly trim the \\.\pipe\ prefix on Windows, if present. On Unix we do not correctly strip the af_unix: prefix correctly if a socket file path contains a ':'. Let's fix this! Signed-off-by: Matthew John Cheetham <[email protected]>
1 parent b62021f commit d4e2f59

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

src/shared/Core.Tests/Trace2Tests.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,26 @@ public class Trace2Tests
66
{
77
[PosixTheory]
88
[InlineData("af_unix:foo", "foo")]
9-
[InlineData("af_unix:stream:foo-bar", "foo-bar")]
10-
[InlineData("af_unix:dgram:foo-bar-baz", "foo-bar-baz")]
9+
[InlineData("af_unix:foo/bar", "foo/bar")]
10+
[InlineData("af_unix:stream:foo/bar", "foo/bar")]
11+
[InlineData("af_unix:dgram:foo/bar/baz", "foo/bar/baz")]
1112
public void TryGetPipeName_Posix_Returns_Expected_Value(string input, string expected)
1213
{
1314
var isSuccessful = Trace2.TryGetPipeName(input, out var actual);
1415

1516
Assert.True(isSuccessful);
16-
Assert.Matches(actual, expected);
17+
Assert.Equal(actual, expected);
1718
}
1819

1920
[WindowsTheory]
2021
[InlineData("\\\\.\\pipe\\git-foo", "git-foo")]
2122
[InlineData("\\\\.\\pipe\\git-foo-bar", "git-foo-bar")]
22-
[InlineData("\\\\.\\pipe\\foo\\git-bar", "git-bar")]
23+
[InlineData("\\\\.\\pipe\\foo\\git-bar", "foo\\git-bar")]
2324
public void TryGetPipeName_Windows_Returns_Expected_Value(string input, string expected)
2425
{
2526
var isSuccessful = Trace2.TryGetPipeName(input, out var actual);
2627

2728
Assert.True(isSuccessful);
28-
Assert.Matches(actual, expected);
29+
Assert.Equal(expected, actual);
2930
}
3031
}

src/shared/Core/Trace2.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -480,13 +480,16 @@ protected override void ReleaseManagedResources()
480480
internal static bool TryGetPipeName(string eventTarget, out string name)
481481
{
482482
// Use prefixes to determine whether target is a named pipe/socket
483-
if (eventTarget.Contains("af_unix:", StringComparison.OrdinalIgnoreCase) ||
484-
eventTarget.Contains("\\\\.\\pipe\\", StringComparison.OrdinalIgnoreCase) ||
485-
eventTarget.Contains("/./pipe/", StringComparison.OrdinalIgnoreCase))
483+
if (eventTarget.StartsWith("af_unix:", StringComparison.OrdinalIgnoreCase) ||
484+
eventTarget.StartsWith(@"\\.\pipe\", StringComparison.OrdinalIgnoreCase) ||
485+
eventTarget.StartsWith("//./pipe/", StringComparison.OrdinalIgnoreCase))
486486
{
487487
name = PlatformUtils.IsWindows()
488-
? eventTarget.TrimUntilLastIndexOf("\\")
489-
: eventTarget.TrimUntilLastIndexOf(":");
488+
? eventTarget.Replace('/', '\\')
489+
.TrimUntilIndexOf(@"\\.\pipe\")
490+
: eventTarget.Replace("af_unix:dgram:", "")
491+
.Replace("af_unix:stream:", "")
492+
.Replace("af_unix:", "");
490493
return true;
491494
}
492495

0 commit comments

Comments
 (0)