akkadotnet / akkadotnet/Hyperion

Error on deserialization when stream returns less bytes than requested

Open
#95 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
281
Forks
62
PR merge metrics
No merged PRs in 30d

Description

I've found and issue with Hyperion related to stream reading.
I did not analyze carefully which line of code is failing (it seems, it serious issue related to StreamEx class). E.g. next code:
```
public static int ReadInt32(this Stream self, DeserializerSession session)
{
byte[] buffer = session.GetBuffer(4);
self.Read(buffer, 0, 4);
return BitConverter.ToInt32(buffer, 0);
}
```
self can return less than four bytes and it is normal situation (e.g. for network streams, or compressed streams), as result int value will be incorrect and other data will be garbage.

You need to replace read method to something like
```
public override int Read(byte[] buffer, int offset, int count)
{
var total = 0;
while (count > 0)
{
var cnt = _origStream.Read(buffer, offset, count);
offset += cnt;
count -= cnt;
total += cnt;
}

return total;
}
```
But only for places where you need to read specific count of bytes

Contributor guide

Open the contributing guide

Research direction

Start with the StreamEx class and the ReadInt32 deserialization entry point shown in the issue, then locate other places that read a specific byte count. Verify how short reads are handled and add or update coverage for streams that return fewer bytes than requested; done means fixed-width values remain correct for those streams.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.