Skip to content

Commit

Permalink
second bug fix: SerializationRecord.TypeNameMatches should know the d…
Browse files Browse the repository at this point in the history
…ifference between SZArray and single-dimension, non-zero offset arrays (example: int[] and int[*])
  • Loading branch information
adamsitnik committed Sep 12, 2024
1 parent 969a2ef commit 61a9b14
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,13 @@ private static bool Matches(Type type, TypeName typeName)
// At first, check the non-allocating properties for mismatch.
if (type.IsArray != typeName.IsArray || type.IsConstructedGenericType != typeName.IsConstructedGenericType
|| type.IsNested != typeName.IsNested
|| (type.IsArray && type.GetArrayRank() != typeName.GetArrayRank()))
|| (type.IsArray && type.GetArrayRank() != typeName.GetArrayRank())
#if NET
|| type.IsSZArray != typeName.IsSZArray // int[] vs int[*]
#else
|| (type.IsArray && type.Name != typeName.Name)
#endif
)
{
return false;
}
Expand Down
18 changes: 18 additions & 0 deletions src/libraries/System.Formats.Nrbf/tests/TypeMatchTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,24 @@ public void ThrowsForNullType()
Assert.Throws<ArgumentNullException>(() => record.TypeNameMatches(type: null));
}

[Fact]
public void TakesCustomOffsetsIntoAccount()
{
int[] input = [1, 2, 3];

SerializationRecord record = NrbfDecoder.Decode(Serialize(input));

Assert.True(record.TypeNameMatches(typeof(int[])));

Type nonSzArray = typeof(int).Assembly.GetType("System.Int32[*]");
#if NET
Assert.False(nonSzArray.IsSZArray);
Assert.True(nonSzArray.IsVariableBoundArray);
#endif
Assert.Equal(1, nonSzArray.GetArrayRank());
Assert.False(record.TypeNameMatches(nonSzArray));
}

[Fact]
public void TakesGenericTypeDefinitionIntoAccount()
{
Expand Down

0 comments on commit 61a9b14

Please sign in to comment.