Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix(tar): clear rest of buffer when eof is reached #789

Merged
merged 2 commits into from
Oct 20, 2022
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: 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
}
}