Skip to content

Commit 34a4f52

Browse files
authored
Fix infinite loop when reading truncated data; Add unit test for expected exception (#157)
1 parent e8b6e66 commit 34a4f52

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

Diff for: Amazon.IonDotnet.Tests/Internals/BinaryReaderTest.cs

+19
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,25 @@ public void MultipleBlobs()
335335
}
336336
}
337337

338+
[TestMethod]
339+
public void ReadAll_FailOnTruncatedData()
340+
{
341+
IIonReader binReader;
342+
using (var ms = new MemoryStream()) {
343+
ReadOnlySpan<byte> fullString = new ReadOnlySpan<byte>(new byte[] {
344+
0xE0, 0x01, 0x00, 0xEA, 0x85, 0x31, 0x32, 0x33, 0x34, 0x35
345+
});
346+
ms.Write(fullString.Slice(0, 9));
347+
348+
ms.Seek(0, SeekOrigin.Begin);
349+
binReader = IonReaderBuilder.Build(ms);
350+
351+
var type = binReader.MoveNext();
352+
Assert.AreEqual(IonType.String, type);
353+
Assert.ThrowsException<UnexpectedEofException>(() => binReader.StringValue());
354+
}
355+
}
356+
338357
/// <summary>
339358
/// Aims to test the correctness of skipping with step in-out in the middle
340359
/// of container

Diff for: Amazon.IonDotnet/Internals/Binary/RawBinaryReader.cs

+7
Original file line numberDiff line numberDiff line change
@@ -1167,6 +1167,13 @@ private void ReadAll(Span<byte> bufferSpan, int length)
11671167
while (length > 0)
11681168
{
11691169
var amount = this.input.Read(bufferSpan.Slice(0, length));
1170+
1171+
// length > 0 (because we didn't stop)
1172+
if (amount == 0)
1173+
{
1174+
throw new UnexpectedEofException();
1175+
}
1176+
11701177
length -= amount;
11711178
bufferSpan = bufferSpan.Slice(amount);
11721179
this.localRemaining -= amount;

0 commit comments

Comments
 (0)