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

Fix for malformed utf8 sequence #47

Merged
merged 10 commits into from
Mar 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.");
}
}
}
}