Skip to content

Commit 47f3013

Browse files
authored
Fix NullReferenceException in ASP.NET Core when RoutePattern.RawText is null (#5880 -> v2) (#5888)
## Summary of changes Fix `NullReferenceException` in aspnetcore handler ## Reason for change `RoutePattern.RawText` can be null ## Implementation details Add `#nullable enable` annotation to the duck type, and `AspNetCoreResourceNameHelper` and follow the errors. ## Test coverage Covered by existing in general, hard to reproduce this specific bug Backport of #5880
1 parent 9db38aa commit 47f3013

File tree

3 files changed

+21
-18
lines changed

3 files changed

+21
-18
lines changed

tracer/missing-nullability-files.csv

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,11 @@ src/Datadog.Trace/Debugger/ILineProbeResolver.cs
123123
src/Datadog.Trace/Debugger/LiveDebugger.cs
124124
src/Datadog.Trace/Debugger/ProbeLocationType.cs
125125
src/Datadog.Trace/DiagnosticListeners/AspNetCoreDiagnosticObserver.cs
126-
src/Datadog.Trace/DiagnosticListeners/AspNetCoreResourceNameHelper.cs
127126
src/Datadog.Trace/DiagnosticListeners/DiagnosticManager.cs
128127
src/Datadog.Trace/DiagnosticListeners/DiagnosticObserver.cs
129128
src/Datadog.Trace/DiagnosticListeners/EndpointFeatureProxy.cs
130129
src/Datadog.Trace/DiagnosticListeners/IDiagnosticManager.cs
131130
src/Datadog.Trace/DiagnosticListeners/RouteEndpoint.cs
132-
src/Datadog.Trace/DiagnosticListeners/RoutePattern.cs
133131
src/Datadog.Trace/DogStatsd/NoOpStatsd.cs
134132
src/Datadog.Trace/DogStatsd/StatsdExtensions.cs
135133
src/Datadog.Trace/DogStatsd/TracerMetricNames.cs

tracer/src/Datadog.Trace/DiagnosticListeners/AspNetCoreResourceNameHelper.cs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License.
33
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc.
44
// </copyright>
5+
6+
#nullable enable
7+
58
#if !NETFRAMEWORK
69

710
using System;
@@ -18,15 +21,15 @@ internal class AspNetCoreResourceNameHelper
1821
internal static string SimplifyRoutePattern(
1922
RoutePattern routePattern,
2023
RouteValueDictionary routeValueDictionary,
21-
string areaName,
22-
string controllerName,
23-
string actionName,
24+
string? areaName,
25+
string? controllerName,
26+
string? actionName,
2427
bool expandRouteParameters)
2528
{
26-
var maxSize = routePattern.RawText.Length
27-
+ (string.IsNullOrEmpty(areaName) ? 0 : Math.Max(areaName.Length - 4, 0)) // "area".Length
28-
+ (string.IsNullOrEmpty(controllerName) ? 0 : Math.Max(controllerName.Length - 10, 0)) // "controller".Length
29-
+ (string.IsNullOrEmpty(actionName) ? 0 : Math.Max(actionName.Length - 6, 0)) // "action".Length
29+
var maxSize = (routePattern.RawText?.Length ?? 0)
30+
+ (string.IsNullOrEmpty(areaName) ? 0 : Math.Max(areaName!.Length - 4, 0)) // "area".Length
31+
+ (string.IsNullOrEmpty(controllerName) ? 0 : Math.Max(controllerName!.Length - 10, 0)) // "controller".Length
32+
+ (string.IsNullOrEmpty(actionName) ? 0 : Math.Max(actionName!.Length - 6, 0)) // "action".Length
3033
+ 1; // '/' prefix
3134

3235
var sb = StringBuilderCache.Acquire(maxSize);
@@ -129,15 +132,15 @@ internal static string SimplifyRoutePattern(
129132
internal static string SimplifyRouteTemplate(
130133
RouteTemplate routePattern,
131134
RouteValueDictionary routeValueDictionary,
132-
string areaName,
133-
string controllerName,
134-
string actionName,
135+
string? areaName,
136+
string? controllerName,
137+
string? actionName,
135138
bool expandRouteParameters)
136139
{
137-
var maxSize = routePattern.TemplateText.Length
138-
+ (string.IsNullOrEmpty(areaName) ? 0 : Math.Max(areaName.Length - 4, 0)) // "area".Length
139-
+ (string.IsNullOrEmpty(controllerName) ? 0 : Math.Max(controllerName.Length - 10, 0)) // "controller".Length
140-
+ (string.IsNullOrEmpty(actionName) ? 0 : Math.Max(actionName.Length - 6, 0)) // "action".Length
140+
var maxSize = (routePattern.TemplateText?.Length ?? 0)
141+
+ (string.IsNullOrEmpty(areaName) ? 0 : Math.Max(areaName!.Length - 4, 0)) // "area".Length
142+
+ (string.IsNullOrEmpty(controllerName) ? 0 : Math.Max(controllerName!.Length - 10, 0)) // "controller".Length
143+
+ (string.IsNullOrEmpty(actionName) ? 0 : Math.Max(actionName!.Length - 6, 0)) // "action".Length
141144
+ 1; // '/' prefix
142145

143146
var sb = StringBuilderCache.Acquire(maxSize);
@@ -228,7 +231,7 @@ internal static string SimplifyRouteTemplate(
228231
return string.IsNullOrEmpty(simplifiedRoute) ? "/" : simplifiedRoute.ToLowerInvariant();
229232
}
230233

231-
private static bool IsIdentifierSegment(object value, [NotNullWhen(false)] out string valueAsString)
234+
private static bool IsIdentifierSegment(object? value, [NotNullWhen(true)] out string? valueAsString)
232235
{
233236
valueAsString = value as string ?? value?.ToString();
234237
if (valueAsString is null)

tracer/src/Datadog.Trace/DiagnosticListeners/RoutePattern.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc.
44
// </copyright>
55

6+
#nullable enable
7+
68
using System.Collections;
79
using Datadog.Trace.DuckTyping;
810

@@ -22,6 +24,6 @@ internal struct RoutePattern
2224
/// <summary>
2325
/// Gets the RoutePattern.RawText
2426
/// </summary>
25-
public string RawText;
27+
public string? RawText;
2628
}
2729
}

0 commit comments

Comments
 (0)