Fix ActivityContext.TryParse null handling (#53265)
authorStephen Toub <stoub@microsoft.com>
Wed, 26 May 2021 10:51:39 +0000 (06:51 -0400)
committerGitHub <noreply@github.com>
Wed, 26 May 2021 10:51:39 +0000 (06:51 -0400)
src/libraries/System.Diagnostics.DiagnosticSource/ref/System.Diagnostics.DiagnosticSourceActivity.cs
src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/ActivityContext.cs
src/libraries/System.Diagnostics.DiagnosticSource/tests/ActivitySourceTests.cs

index a5ac6cb..482a58b 100644 (file)
@@ -213,7 +213,7 @@ namespace System.Diagnostics
         public System.Diagnostics.ActivityTraceFlags TraceFlags  { get { throw null; } }
         public string? TraceState  { get { throw null; } }
         public bool IsRemote { get { throw null; } }
-        public static bool TryParse(string traceParent, string? traceState, out System.Diagnostics.ActivityContext context) { throw null; }
+        public static bool TryParse(string? traceParent, string? traceState, out System.Diagnostics.ActivityContext context) { throw null; }
         public static System.Diagnostics.ActivityContext Parse(string traceParent, string? traceState) { throw null; }
         public static bool operator ==(System.Diagnostics.ActivityContext left, System.Diagnostics.ActivityContext right) { throw null; }
         public static bool operator !=(System.Diagnostics.ActivityContext left, System.Diagnostics.ActivityContext right) { throw null; }
index 07710b6..93661f5 100644 (file)
@@ -66,11 +66,12 @@ namespace System.Diagnostics
         /// <param name="traceParent">W3C trace parent header.</param>
         /// <param name="traceState">W3C trace state.</param>
         /// <param name="context">The ActivityContext object created from the parsing operation.</param>
-        public static bool TryParse(string traceParent, string? traceState, out ActivityContext context)
+        public static bool TryParse(string? traceParent, string? traceState, out ActivityContext context)
         {
-            if (traceParent == null)
+            if (traceParent is null)
             {
-                throw new ArgumentNullException(nameof(traceParent));
+                context = default;
+                return false;
             }
 
             return Activity.TryConvertIdToContext(traceParent, traceState, out context);
@@ -86,7 +87,12 @@ namespace System.Diagnostics
         /// </returns>
         public static ActivityContext Parse(string traceParent, string? traceState)
         {
-            if (!TryParse(traceParent, traceState, out ActivityContext context))
+            if (traceParent is null)
+            {
+                throw new ArgumentNullException(nameof(traceParent));
+            }
+
+            if (!Activity.TryConvertIdToContext(traceParent, traceState, out ActivityContext context))
             {
                 throw new ArgumentException(SR.InvalidTraceParent);
             }
index 0ae2bb1..edc6955 100644 (file)
@@ -480,7 +480,7 @@ namespace System.Diagnostics.Tests
             context = ActivityContext.Parse(w3cId, null);
             Assert.Null(context.TraceState);
 
-            Assert.Throws<ArgumentNullException>(() => ActivityContext.TryParse(null, "k=v", out context));
+            Assert.False(ActivityContext.TryParse(null, "k=v", out context));
             Assert.Throws<ArgumentNullException>(() => ActivityContext.Parse(null, null));
             Assert.Throws<ArgumentException>(() => ActivityContext.Parse("BadW3C", null));