Skip to content

Commit

Permalink
Correct redaction of Array.Empty<char>() and new char[0]
Browse files Browse the repository at this point in the history
Fixes #4308
  • Loading branch information
RussKie committed Aug 28, 2023
1 parent b273601 commit 30667e0
Showing 1 changed file with 7 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,14 @@ public int Redact<T>(T value, Span<char> destination, string? format = null, IFo
return Redact(((IFormattable)value).ToString(format, provider), destination);
}

if (value is char[] arrayOfChar)
if (value is char[])
{
return Redact(arrayOfChar.AsSpan(), destination);
// An attempt to call value.ToString() on a char[] will produce a string "System.Char[]" and all redaction will be attempted on it,
// instead of the provided array. This will lead to incorrectly allocated buffers.
//
// NB: not using pattern matching as it is recognized by the JIT and since this is a generic type, the JIT ends up generating code
// without any of those conditional statements being present. But this only happens when not using pattern matching.
return Redact(((char[])(object)value).AsSpan(), destination);
}

return Redact(value?.ToString(), destination);
Expand Down

0 comments on commit 30667e0

Please sign in to comment.