Skip to content

Commit 61a9b14

Browse files
committed
second bug fix: SerializationRecord.TypeNameMatches should know the difference between SZArray and single-dimension, non-zero offset arrays (example: int[] and int[*])
1 parent 969a2ef commit 61a9b14

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/libraries/System.Formats.Nrbf/src/System/Formats/Nrbf/SerializationRecord.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,13 @@ private static bool Matches(Type type, TypeName typeName)
9999
// At first, check the non-allocating properties for mismatch.
100100
if (type.IsArray != typeName.IsArray || type.IsConstructedGenericType != typeName.IsConstructedGenericType
101101
|| type.IsNested != typeName.IsNested
102-
|| (type.IsArray && type.GetArrayRank() != typeName.GetArrayRank()))
102+
|| (type.IsArray && type.GetArrayRank() != typeName.GetArrayRank())
103+
#if NET
104+
|| type.IsSZArray != typeName.IsSZArray // int[] vs int[*]
105+
#else
106+
|| (type.IsArray && type.Name != typeName.Name)
107+
#endif
108+
)
103109
{
104110
return false;
105111
}

src/libraries/System.Formats.Nrbf/tests/TypeMatchTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,24 @@ public void ThrowsForNullType()
8383
Assert.Throws<ArgumentNullException>(() => record.TypeNameMatches(type: null));
8484
}
8585

86+
[Fact]
87+
public void TakesCustomOffsetsIntoAccount()
88+
{
89+
int[] input = [1, 2, 3];
90+
91+
SerializationRecord record = NrbfDecoder.Decode(Serialize(input));
92+
93+
Assert.True(record.TypeNameMatches(typeof(int[])));
94+
95+
Type nonSzArray = typeof(int).Assembly.GetType("System.Int32[*]");
96+
#if NET
97+
Assert.False(nonSzArray.IsSZArray);
98+
Assert.True(nonSzArray.IsVariableBoundArray);
99+
#endif
100+
Assert.Equal(1, nonSzArray.GetArrayRank());
101+
Assert.False(record.TypeNameMatches(nonSzArray));
102+
}
103+
86104
[Fact]
87105
public void TakesGenericTypeDefinitionIntoAccount()
88106
{

0 commit comments

Comments
 (0)