Skip to content

Commit 969a2ef

Browse files
committed
first bug fix: SerializationRecord.TypeNameMatches should throw ArgumentNullException for null Type argument
1 parent 88b9dba commit 969a2ef

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

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

+14-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,20 @@ internal SerializationRecord() // others can't derive from this type
5050
/// </remarks>
5151
/// <param name="type">The type to compare against.</param>
5252
/// <returns><see langword="true" /> if the serialized type name matches the provided type; otherwise, <see langword="false" />.</returns>
53-
public bool TypeNameMatches(Type type) => Matches(type, TypeName);
53+
/// <exception cref="ArgumentNullException"><paramref name="type" /> is <see langword="null" />.</exception>
54+
public bool TypeNameMatches(Type type)
55+
{
56+
#if NET
57+
ArgumentNullException.ThrowIfNull(type);
58+
#else
59+
if (type is null)
60+
{
61+
throw new ArgumentNullException(nameof(type));
62+
}
63+
#endif
64+
65+
return Matches(type, TypeName);
66+
}
5467

5568
private static bool Matches(Type type, TypeName typeName)
5669
{

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

+10
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,16 @@ public void CanRecognizeGenericSystemTypes()
7373
Verify(new Dictionary<string, List<ValueTuple<int, short>>>());
7474
}
7575

76+
[Fact]
77+
public void ThrowsForNullType()
78+
{
79+
List<int> input = new List<int>();
80+
81+
SerializationRecord record = NrbfDecoder.Decode(Serialize(input));
82+
83+
Assert.Throws<ArgumentNullException>(() => record.TypeNameMatches(type: null));
84+
}
85+
7686
[Fact]
7787
public void TakesGenericTypeDefinitionIntoAccount()
7888
{

0 commit comments

Comments
 (0)