Skip to content

Commit

Permalink
fix(tar): clear rest of buffer when eof is reached (#789)
Browse files Browse the repository at this point in the history
  • Loading branch information
piksel authored Oct 20, 2022
1 parent 6c96ce2 commit 75d1cf8
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
5 changes: 5 additions & 0 deletions src/ICSharpCode.SharpZipLib/Tar/TarBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,11 @@ private async ValueTask<bool> ReadRecordAsync(CancellationToken ct, bool isAsync
//
if (numBytes <= 0)
{
// Fill the rest of the buffer with 0 to clear any left over data in the shared buffer
for (; offset < RecordSize; offset++)
{
recordBuffer[offset] = 0;
}
break;
}

Expand Down
24 changes: 24 additions & 0 deletions test/ICSharpCode.SharpZipLib.Tests/Tar/TarInputStreamTests.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System;
using System.Buffers;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using ICSharpCode.SharpZipLib.Tar;
using ICSharpCode.SharpZipLib.Tests.TestSupport;
using NUnit.Framework;

namespace ICSharpCode.SharpZipLib.Tests.Tar
Expand Down Expand Up @@ -87,5 +89,27 @@ public async Task TestReadAsync()
Assert.AreEqual(975, read3);
Assert.AreEqual(entryBytes.AsSpan(1025, 975).ToArray(), buffer.AsSpan().Slice(0, 975).ToArray());
}

[Test]
public void ReadEmptyStreamWhenArrayPoolIsDirty()
{
// Rent an array with the same size as the tar buffer from the array pool
var buffer = ArrayPool<byte>.Shared.Rent(TarBuffer.DefaultRecordSize);

// Fill the array with anything but 0
Utils.FillArray(buffer, 0x8b);

// Return the now dirty buffer to the array pool
ArrayPool<byte>.Shared.Return(buffer);

Assert.DoesNotThrow(() =>
{
using var emptyStream = new MemoryStream(Array.Empty<byte>());
using var tarInputStream = new TarInputStream(emptyStream, Encoding.UTF8);
while (tarInputStream.GetNextEntry() is { } tarEntry)
{
}
}, "reading from an empty input stream should not cause an error");
}
}
}
17 changes: 13 additions & 4 deletions test/ICSharpCode.SharpZipLib.Tests/TestSupport/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,15 @@ public static void PatchFirstEntrySize(Stream stream, int newSize)
stream.Write(sizeBytes, 0, 4);
}
}

public static void FillArray(byte[] buffer, byte value)
{
#if NET6_0_OR_GREATER
Array.Fill(buffer, value);
#else
for(var i = 0; i < buffer.Length; i++) buffer[i] = value;
#endif
}
}

public class TestTraceListener : TraceListener
Expand Down Expand Up @@ -185,7 +194,7 @@ internal TempFile(string dirPath = null, string filename = null)
_fileInfo = new FileInfo(Path.Combine(dirPath, filename));
}

#region IDisposable Support
#region IDisposable Support

private bool _disposed; // To detect redundant calls

Expand Down Expand Up @@ -213,7 +222,7 @@ public void Dispose()
GC.SuppressFinalize(this);
}

#endregion IDisposable Support
#endregion IDisposable Support
}


Expand Down Expand Up @@ -245,7 +254,7 @@ public TempFile CreateDummyFile(string name, int size = 16, int seed = Utils.Def

public TempFile GetFile(string fileName) => new TempFile(FullPath, fileName);

#region IDisposable Support
#region IDisposable Support

private bool _disposed; // To detect redundant calls

Expand All @@ -272,6 +281,6 @@ public void Dispose()
GC.SuppressFinalize(this);
}

#endregion IDisposable Support
#endregion IDisposable Support
}
}

0 comments on commit 75d1cf8

Please sign in to comment.