Hyperion icon indicating copy to clipboard operation
Hyperion copied to clipboard

Error on deserialization when stream returns less bytes than requested

Open force-net opened this issue 6 years ago • 1 comments

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

force-net avatar Apr 06 '18 14:04 force-net

Duplicate of #40

Solution was already proposed but not merged yet.

Ralf1108 avatar Sep 04 '18 10:09 Ralf1108