Skip to content

Commit 8f76943

Browse files
authored
Use RuntimeHelpers.GetHashCode in IndexObject (#7678)
## Summary of changes This changes to use a means to get the hash code of an object to handle `null`, `object.GetHashCode` and a fallback to use RuntimeHelpers `GetHashCode` for when `object.GetHashCode` fails. ## Reason for change My guess is that `GetHashCode` is overridden for the given object and we probably should avoid that? Spotted the following in Error Tracking: ``` Error : PropagationModuleImpl.PropagateWholeResultWhenInputTainted exception System.NullReferenceException at REDACTED at Datadog.Trace.Iast.DefaultTaintedMap.IndexObject(Object objectStored) at Datadog.Trace.Iast.DefaultTaintedMap.Get(Object objectToFind) at Datadog.Trace.Iast.Propagation.PropagationModuleImpl.PropagateWholeResultWhenInputTainted(String result, Object input1, Object input2, Object input3) ``` ## Implementation details Swapped from one to the other. ## Test coverage 🤷 Didn't try to reproduce ## Other details <!-- Fixes #{issue} --> <!-- ⚠️ Note: Where possible, please obtain 2 approvals prior to merging. Unless CODEOWNERS specifies otherwise, for external teams it is typically best to have one review from a team member, and one review from apm-dotnet. Trivial changes do not require 2 reviews. MergeQueue is NOT enabled in this repository. If you have write access to the repo, the PR has 1-2 approvals (see above), and all of the required checks have passed, you can use the Squash and Merge button to merge the PR. If you don't have write access, or you need help, reach out in the #apm-dotnet channel in Slack. -->
1 parent 8603e6d commit 8f76943

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

tracer/src/Datadog.Trace/Iast/DefaultTaintedMap.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ public void Clear()
228228

229229
private int IndexObject(object objectStored)
230230
{
231-
return Index(PositiveHashCode(objectStored.GetHashCode()));
231+
return Index(PositiveHashCode(IastUtils.IdentityHashCode(objectStored)));
232232
}
233233

234234
private int PositiveHashCode(int hash)

tracer/src/Datadog.Trace/Iast/IastUtils.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,20 @@ private static int GetHash<T>(T element)
7171

7272
public static int IdentityHashCode(object item)
7373
{
74-
return (item?.GetHashCode() ?? 0);
74+
if (item == null)
75+
{
76+
return 0;
77+
}
78+
79+
try
80+
{
81+
return item.GetHashCode();
82+
}
83+
catch
84+
{
85+
// Seen this that it appears some customer objects have overrode GetHashCode and throw
86+
return System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode(item);
87+
}
7588
}
7689

7790
public static Range[] GetRangesForString(string stringValue, Source source)

0 commit comments

Comments
 (0)