Skip to content

Commit

Permalink
fix(VarInt): raise EndOfStreamException
Browse files Browse the repository at this point in the history
  • Loading branch information
richardschneider committed Mar 16, 2018
1 parent 92557db commit 59f1d90
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/VarInt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ public static void WriteVarint(this Stream stream, long value)
/// <param name="stream">
/// A varint encoded <see cref="Stream"/>.
/// </param>
/// <exception cref="EndOfStreamException">
/// When the no bytes exist in the <paramref name="stream"/>.
/// </exception>
/// <exception cref="InvalidDataException">
/// When the varint value is greater than <see cref="Int32.MaxValue"/>.
/// </exception>
Expand All @@ -137,6 +140,9 @@ public static int ReadVarint32(this Stream stream)
/// <param name="stream">
/// A varint encoded <see cref="Stream"/>.
/// </param>
/// <exception cref="EndOfStreamException">
/// When the no bytes exist in the <paramref name="stream"/>.
/// </exception>
/// <exception cref="InvalidDataException">
/// When the varint value is greater than <see cref="Int64.MaxValue"/>.
/// </exception>
Expand Down Expand Up @@ -196,6 +202,9 @@ public static long ReadVarint64(this Stream stream)
/// A task that represents the asynchronous operation. The task's result
/// is the integer value in the <paramref name="stream"/>.
/// </returns>
/// <exception cref="EndOfStreamException">
/// When the no bytes exist in the <paramref name="stream"/>.
/// </exception>
/// <exception cref="InvalidDataException">
/// When the varint value is greater than <see cref="Int32.MaxValue"/>.
/// </exception>
Expand All @@ -219,6 +228,9 @@ public static long ReadVarint64(this Stream stream)
/// <exception cref="InvalidDataException">
/// When the varint value is greater than <see cref="Int64.MaxValue"/>.
/// </exception>
/// <exception cref="EndOfStreamException">
/// When the no bytes exist in the <paramref name="stream"/>.
/// </exception>
/// <returns>
/// A task that represents the asynchronous operation. The task's result
/// is the integer value in the <paramref name="stream"/>.
Expand All @@ -233,6 +245,9 @@ public static long ReadVarint64(this Stream stream)
{
if (1 != await stream.ReadAsync(buffer, 0, 1, cancel))
{
if (bytesRead == 0)
throw new EndOfStreamException();

throw new InvalidDataException("Varint is not terminated");
}
if (++bytesRead > 9)
Expand Down
2 changes: 1 addition & 1 deletion test/VarintTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public void Unterminated()
public void Empty()
{
var bytes = new byte[0];
ExceptionAssert.Throws<InvalidDataException>(() => Varint.DecodeInt64(bytes));
ExceptionAssert.Throws<EndOfStreamException>(() => Varint.DecodeInt64(bytes));
}

[TestMethod]
Expand Down

0 comments on commit 59f1d90

Please sign in to comment.