Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix infinite loop when reading truncated data #157

Merged
merged 1 commit into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions Amazon.IonDotnet.Tests/Internals/BinaryReaderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,25 @@ public void MultipleBlobs()
}
}

[TestMethod]
public void ReadAll_FailOnTruncatedData()
{
IIonReader binReader;
using (var ms = new MemoryStream()) {
ReadOnlySpan<byte> fullString = new ReadOnlySpan<byte>(new byte[] {
0xE0, 0x01, 0x00, 0xEA, 0x85, 0x31, 0x32, 0x33, 0x34, 0x35
});
ms.Write(fullString.Slice(0, 9));

ms.Seek(0, SeekOrigin.Begin);
binReader = IonReaderBuilder.Build(ms);

var type = binReader.MoveNext();
Assert.AreEqual(IonType.String, type);
Assert.ThrowsException<UnexpectedEofException>(() => binReader.StringValue());
}
}

/// <summary>
/// Aims to test the correctness of skipping with step in-out in the middle
/// of container
Expand Down
7 changes: 7 additions & 0 deletions Amazon.IonDotnet/Internals/Binary/RawBinaryReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1167,6 +1167,13 @@ private void ReadAll(Span<byte> bufferSpan, int length)
while (length > 0)
{
var amount = this.input.Read(bufferSpan.Slice(0, length));

// length > 0 (because we didn't stop)
if (amount == 0)
{
throw new UnexpectedEofException();
}

length -= amount;
bufferSpan = bufferSpan.Slice(amount);
this.localRemaining -= amount;
Expand Down