diff --git a/tracer/src/Datadog.Trace/Iast/IastModule.cs b/tracer/src/Datadog.Trace/Iast/IastModule.cs index 5532cbeb79ea..aebbbd75fa50 100644 --- a/tracer/src/Datadog.Trace/Iast/IastModule.cs +++ b/tracer/src/Datadog.Trace/Iast/IastModule.cs @@ -694,8 +694,7 @@ private static IastModuleResponse GetScope(string evidenceValue, IntegrationId i private static Location? GetLocation(StackTrace? stack = null, Span? currentSpan = null) { stack ??= StackWalker.GetStackTrace(); - var stackFrame = StackWalker.GetFrame(stack); - if (stackFrame is null) + if (!StackWalker.TryGetFrame(stack, out var stackFrame)) { return null; } diff --git a/tracer/src/Datadog.Trace/Iast/StackWalker.cs b/tracer/src/Datadog.Trace/Iast/StackWalker.cs index 8d2230fabeb6..ff81a686fdf8 100644 --- a/tracer/src/Datadog.Trace/Iast/StackWalker.cs +++ b/tracer/src/Datadog.Trace/Iast/StackWalker.cs @@ -52,8 +52,9 @@ public static StackTrace GetStackTrace() return new StackTrace(DefaultSkipFrames, true); } - public static StackFrame? GetFrame(StackTrace stackTrace) + public static bool TryGetFrame(StackTrace stackTrace, out StackFrame? targetFrame) { + targetFrame = null; foreach (var frame in stackTrace.GetFrames()) { var declaringType = frame?.GetMethod()?.DeclaringType; @@ -62,18 +63,19 @@ public static StackTrace GetStackTrace() { if (excludeType == declaringType?.FullName) { - return null; + return false; } } var assembly = declaringType?.Assembly.GetName().Name; if (assembly != null && !MustSkipAssembly(assembly)) { - return frame; + targetFrame = frame; + break; } } - return null; + return true; } public static bool MustSkipAssembly(string assembly)