SharpZipLib issue #834 repro

CSharp · Console

/Program.cs

using System;
using System.IO;
using System.Text;
using System.Linq;
using System.Diagnostics;
using System.Reflection;
using System.Threading.Tasks;
using System.Threading;

using NUnit.Framework;
using NUnit.Framework.Internal;

using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.GZip;
using ICSharpCode.SharpZipLib.Tar;

// Test with input that works correctly
Test(Repro, "foo");

void Repro(string input) {
	
	//less than this seems to not cause issues (but I didn't try every value)
	var size = 70000;
	var data = new byte[size];
	var random = new Random(data.Length);
	random.NextBytes(data);

	using (var zipStream = new MemoryStream(size * 2))
	{
		using (var zipOutputStream = new ZipOutputStream(zipStream))
		{
			var entry = new ZipEntry("x");
			zipOutputStream.PutNextEntry(entry);
			zipOutputStream.Write(data, 0, data.Length);
			zipOutputStream.CloseEntry();
		}

		//we get the underlying buffer instead of .ToArray() so we get extra trailing zeros
		var bughere = zipStream.GetBuffer();
		using (var zipFile = new ZipFile(new MemoryStream(bughere)))
		{
		}
	}
}

#region Helpers // These are just a helpers to make the results more legible

void Test<T>(Action<T> a, T input) 
{
	// Prevent NUnit from grouping exceptions together
	using var ctx = new TestExecutionContext.IsolatedContext();
	Console.WriteLine($"Testing input \"{input}\"...\n");
	try { 
		a(input);
		Console.WriteLine("\n=> OK!");
	}
	catch (Exception x) {
		Console.WriteLine($"\n=> Failed! ({x.GetType().Name})");	
		Console.WriteLine($"\n{x.Message}");
		var trimmedStack = string.Join("\n", x.StackTrace.Split("\n").Where(l => !l.Contains("ReportFailure")));
		Console.WriteLine($"{trimmedStack}");
	}
	Console.WriteLine($"\n{new string('=', 128)}\n");
}
void Log(string message) => Console.WriteLine($"  {message}");
#endregion
;