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

Error on deserialization when stream returns less bytes than requested #95

Open
force-net opened this issue Apr 6, 2018 · 1 comment

Comments

@force-net
Copy link

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

@Ralf1108
Copy link
Contributor

Ralf1108 commented Sep 4, 2018

Duplicate of #40

Solution was already proposed but not merged yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants