Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[tracing] add null checks when iterating headers in propagators #6460

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 37 additions & 23 deletions tracer/src/Datadog.Trace/Propagators/ParseUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,21 @@ internal class ParseUtility
return null;

// IEnumerable version (different method to avoid try/finally in the caller)
static bool TryParse(IEnumerable<string?> headerValues, ref bool hasValue, out ulong result)
static bool TryParse(IEnumerable<string?>? headerValues, ref bool hasValue, out ulong result)
{
result = 0;
foreach (string? headerValue in headerValues)

if (headerValues is not null)
{
if (ulong.TryParse(headerValue, NumberStyles.Integer, CultureInfo.InvariantCulture, out result))
foreach (string? headerValue in headerValues)
{
return true;
}
if (ulong.TryParse(headerValue, NumberStyles.Integer, CultureInfo.InvariantCulture, out result))
{
return true;
}

hasValue = true;
hasValue = true;
}
}

return false;
Expand Down Expand Up @@ -133,20 +137,24 @@ static bool TryParse(IEnumerable<string?> headerValues, ref bool hasValue, out u
return null;

// IEnumerable version (different method to avoid try/finally in the caller)
static bool TryParse(IEnumerable<string?> headerValues, ref bool hasValue, out int result)
static bool TryParse(IEnumerable<string?>? headerValues, ref bool hasValue, out int result)
{
result = 0;
foreach (string? headerValue in headerValues)

if (headerValues is not null)
{
if (int.TryParse(headerValue, out result))
foreach (string? headerValue in headerValues)
{
// note this int value may not be defined in the enum,
// but we should pass it along without validation
// for forward compatibility
return true;
if (int.TryParse(headerValue, out result))
{
// note this int value may not be defined in the enum,
// but we should pass it along without validation
// for forward compatibility
return true;
}

hasValue = true;
}

hasValue = true;
}

return false;
Expand Down Expand Up @@ -175,13 +183,16 @@ static bool TryParse(IEnumerable<string?> headerValues, ref bool hasValue, out i
return ParseStringIEnumerable(headerValues);

// IEnumerable version (different method to avoid try/finally in the caller)
static string? ParseStringIEnumerable(IEnumerable<string?> headerValues)
static string? ParseStringIEnumerable(IEnumerable<string?>? headerValues)
{
foreach (string? headerValue in headerValues)
if (headerValues is not null)
{
if (!string.IsNullOrEmpty(headerValue))
foreach (string? headerValue in headerValues)
{
return headerValue;
if (!string.IsNullOrEmpty(headerValue))
{
return headerValue;
}
}
}

Expand Down Expand Up @@ -211,13 +222,16 @@ static bool TryParse(IEnumerable<string?> headerValues, ref bool hasValue, out i
return ParseStringIEnumerable(headerValues);

// IEnumerable version (different method to avoid try/finally in the caller)
static string? ParseStringIEnumerable(IEnumerable<string?> headerValues)
static string? ParseStringIEnumerable(IEnumerable<string?>? headerValues)
{
foreach (string? headerValue in headerValues)
if (headerValues is not null)
{
if (!string.IsNullOrEmpty(headerValue))
foreach (string? headerValue in headerValues)
{
return headerValue;
if (!string.IsNullOrEmpty(headerValue))
{
return headerValue;
}
}
}

Expand Down
Loading