From 8d1a69f778a9931d91a6583d4642e3c58da11b4d Mon Sep 17 00:00:00 2001 From: Lucas Pimentel Date: Wed, 18 Dec 2024 23:19:39 -0500 Subject: [PATCH] add null checks for all the foreach --- .../Datadog.Trace/Propagators/ParseUtility.cs | 60 ++++++++++++------- 1 file changed, 37 insertions(+), 23 deletions(-) diff --git a/tracer/src/Datadog.Trace/Propagators/ParseUtility.cs b/tracer/src/Datadog.Trace/Propagators/ParseUtility.cs index da74da6a21ef..2ceb14a634ed 100644 --- a/tracer/src/Datadog.Trace/Propagators/ParseUtility.cs +++ b/tracer/src/Datadog.Trace/Propagators/ParseUtility.cs @@ -67,17 +67,21 @@ internal class ParseUtility return null; // IEnumerable version (different method to avoid try/finally in the caller) - static bool TryParse(IEnumerable headerValues, ref bool hasValue, out ulong result) + static bool TryParse(IEnumerable? 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; @@ -133,20 +137,24 @@ static bool TryParse(IEnumerable headerValues, ref bool hasValue, out u return null; // IEnumerable version (different method to avoid try/finally in the caller) - static bool TryParse(IEnumerable headerValues, ref bool hasValue, out int result) + static bool TryParse(IEnumerable? 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; @@ -175,13 +183,16 @@ static bool TryParse(IEnumerable headerValues, ref bool hasValue, out i return ParseStringIEnumerable(headerValues); // IEnumerable version (different method to avoid try/finally in the caller) - static string? ParseStringIEnumerable(IEnumerable headerValues) + static string? ParseStringIEnumerable(IEnumerable? 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; + } } } @@ -211,13 +222,16 @@ static bool TryParse(IEnumerable headerValues, ref bool hasValue, out i return ParseStringIEnumerable(headerValues); // IEnumerable version (different method to avoid try/finally in the caller) - static string? ParseStringIEnumerable(IEnumerable headerValues) + static string? ParseStringIEnumerable(IEnumerable? 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; + } } }