Skip to content

Commit

Permalink
Fails on too-short UTF-8 sequences consumed from seekable Streams. (#47)
Browse files Browse the repository at this point in the history
#101 tracks the equivalent fix for non-seekable streams.
  • Loading branch information
R-maan authored Mar 31, 2020
1 parent d01176a commit 89edf02
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
5 changes: 2 additions & 3 deletions Amazon.IonDotnet.Tests/Integration/VectorBad.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ public class VectorBad
{
private static readonly HashSet<string> Excludes = new HashSet<string>
{
"shortUtf8Sequence_1.ion",
"shortUtf8Sequence_2.ion",
"shortUtf8Sequence_3.ion"
// To exclude a test file of ion-test submodule from running, add the
// test file with its extension here. For example: "test.ion"
};

private static readonly DirectoryInfo IonTestDir = DirStructure.IonTestDir();
Expand Down
30 changes: 26 additions & 4 deletions Amazon.IonDotnet/Internals/Text/UnicodeStream.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using Amazon.IonDotnet.Utils;

namespace Amazon.IonDotnet.Internals.Text
{
internal class UnicodeStream : TextStream
{
private readonly StreamReader _streamReader;
private readonly Stack<int> _unreadStack;
private long? remainingChars = null;

public UnicodeStream(Stream inputStream) : this(inputStream, Encoding.UTF8)
{
Expand All @@ -20,9 +19,12 @@ public UnicodeStream(Stream inputStream, Encoding encoding)
{
if (!inputStream.CanRead)
throw new ArgumentException("Input stream must be readable", nameof(inputStream));

_streamReader = new StreamReader(inputStream, encoding);
_unreadStack = new Stack<int>();
if (inputStream.CanSeek)
{
remainingChars = inputStream.Length;
}
}

public UnicodeStream(Stream inputStream, Span<byte> readBytes)
Expand All @@ -44,6 +46,7 @@ public UnicodeStream(Stream inputStream, Encoding encoding, Span<byte> readBytes
_streamReader = new StreamReader(inputStream, encoding);
if (inputStream.CanSeek)
{
remainingChars = inputStream.Length;
InputStream.Seek(-readBytes.Length, SeekOrigin.Current);
return;
}
Expand All @@ -57,7 +60,18 @@ public UnicodeStream(Stream inputStream, Encoding encoding, Span<byte> readBytes

public override int Read()
{
return _unreadStack.Count > 0 ? _unreadStack.Pop() : _streamReader.Read();
var value = _unreadStack.Count > 0 ? _unreadStack.Pop() : _streamReader.Read();

if (remainingChars.HasValue)
{
remainingChars--;
if (_streamReader.CurrentEncoding == Encoding.UTF8)
{
IsValidUTF8Character();
}
}

return value;
}

public override void Unread(int c)
Expand All @@ -72,5 +86,13 @@ public override void Unread(int c)
}

private Stream InputStream => _streamReader.BaseStream;

private void IsValidUTF8Character()
{
if (remainingChars > 0 && _streamReader.Peek() == -1)
{
throw new IonException("Input stream is not a valid UTF-8 stream.");
}
}
}
}

0 comments on commit 89edf02

Please sign in to comment.