Skip to content

Commit 5832f68

Browse files
adamsitnikjtschuster
authored andcommitted
[NRBF] throw SerializationException when a surrogate character is read (dotnet#107532)
(so far an ArgumentException was thrown)
1 parent cdca873 commit 5832f68

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ internal static IReadOnlyList<T> DecodePrimitiveTypes(BinaryReader reader, int c
8585
}
8686
else if (typeof(T) == typeof(char))
8787
{
88-
return (T[])(object)reader.ReadChars(count);
88+
return (T[])(object)reader.ParseChars(count);
8989
}
9090

9191
// It's safe to pre-allocate, as we have ensured there is enough bytes in the stream.
@@ -206,7 +206,7 @@ private static List<T> DecodeFromNonSeekableStream(BinaryReader reader, int coun
206206
}
207207
else if (typeof(T) == typeof(char))
208208
{
209-
values.Add((T)(object)reader.ReadChar());
209+
values.Add((T)(object)reader.ParseChar());
210210
}
211211
else if (typeof(T) == typeof(short))
212212
{

src/libraries/System.Formats.Nrbf/src/System/Formats/Nrbf/Utils/BinaryReaderExtensions.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,18 @@ internal static char ParseChar(this BinaryReader reader)
115115
}
116116
}
117117

118+
internal static char[] ParseChars(this BinaryReader reader, int count)
119+
{
120+
try
121+
{
122+
return reader.ReadChars(count);
123+
}
124+
catch (ArgumentException) // A surrogate character was read.
125+
{
126+
throw new SerializationException(SR.Serialization_SurrogateCharacter);
127+
}
128+
}
129+
118130
/// <summary>
119131
/// Creates a <see cref="DateTime"/> object from raw data with validation.
120132
/// </summary>

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -564,8 +564,10 @@ public void InvalidDecimal(string textRepresentation)
564564
Assert.Throws<SerializationException>(() => NrbfDecoder.Decode(stream));
565565
}
566566

567-
[Fact]
568-
public void SurrogateCharacter()
567+
[Theory]
568+
[InlineData(true)]
569+
[InlineData(false)]
570+
public void SurrogateCharacters(bool array)
569571
{
570572
using MemoryStream stream = new();
571573
BinaryWriter writer = new(stream, Encoding.UTF8);
@@ -576,8 +578,22 @@ public void SurrogateCharacter()
576578
writer.Write("ClassWithCharField"); // type name
577579
writer.Write(1); // member count
578580
writer.Write("memberName");
579-
writer.Write((byte)BinaryType.Primitive);
580-
writer.Write((byte)PrimitiveType.Char);
581+
582+
if (array)
583+
{
584+
writer.Write((byte)BinaryType.PrimitiveArray);
585+
writer.Write((byte)PrimitiveType.Char);
586+
writer.Write((byte)SerializationRecordType.ArraySinglePrimitive);
587+
writer.Write(2); // array record Id
588+
writer.Write(1); // array length
589+
writer.Write((byte)PrimitiveType.Char);
590+
}
591+
else
592+
{
593+
writer.Write((byte)BinaryType.Primitive);
594+
writer.Write((byte)PrimitiveType.Char);
595+
}
596+
581597
writer.Write((byte)0xC0); // a surrogate character
582598
writer.Write((byte)SerializationRecordType.MessageEnd);
583599

0 commit comments

Comments
 (0)