Fix issue when handling 0-length streams.

This commit is contained in:
Frederik Carlier
2016-05-25 00:04:46 +02:00
parent 6571783fab
commit 12418b7284
3 changed files with 25 additions and 9 deletions

View File

@@ -0,0 +1,20 @@
using Claunia.PropertyList;
using NUnit.Framework;
using System.IO;
namespace plistcil.test
{
[TestFixture]
public class PropertyListParserTests
{
[Test]
[ExpectedException(typeof(PropertyListFormatException))]
public static void ParseEmptyStreamTest()
{
using (MemoryStream stream = new MemoryStream())
{
PropertyListParser.Parse(stream);
}
}
}
}

View File

@@ -39,6 +39,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ParseTest.cs" />
<Compile Include="IssueTest.cs" />
<Compile Include="PropertyListParserTests.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>

View File

@@ -81,7 +81,7 @@ namespace Claunia.PropertyList
//Skip Unicode byte order mark (BOM)
offset += 3;
}
while (offset < bytes.Length && bytes[offset] == ' ' || bytes[offset] == '\t' || bytes[offset] == '\r' || bytes[offset] == '\n' || bytes[offset] == '\f')
while (offset < bytes.Length && (bytes[offset] == ' ' || bytes[offset] == '\t' || bytes[offset] == '\r' || bytes[offset] == '\n' || bytes[offset] == '\f'))
{
offset++;
}
@@ -129,17 +129,12 @@ namespace Claunia.PropertyList
/// <param name="fs">The Stream pointing to the data that should be stored in the array.</param>
internal static byte[] ReadAll(Stream fs)
{
MemoryStream outputStream = new MemoryStream();
byte[] buf = new byte[512];
int read = 512;
while (read == 512)
using (MemoryStream outputStream = new MemoryStream())
{
read = fs.Read(buf, 0, 512);
if (read != -1)
outputStream.Write(buf, 0, read);
}
fs.CopyTo(outputStream);
return outputStream.ToArray();
}
}
/// <summary>
/// Parses a property list from a file.