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

Dispose some disposables #882

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net462;net6.0</TargetFrameworks>
<LangVersion>8.0</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public TarInputStream()
tarEntry.Size = 1024 * 1024;
zipOutputStream.PutNextEntry(tarEntry);

var rng = RandomNumberGenerator.Create();
using var rng = RandomNumberGenerator.Create();
var inputBuffer = new byte[1024];
rng.GetBytes(inputBuffer);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public long WriteZipOutputStream()
{
using (var memoryStream = new MemoryStream(outputBuffer))
{
var zipOutputStream = new SharpZipLib.Zip.ZipOutputStream(memoryStream);
using var zipOutputStream = new SharpZipLib.Zip.ZipOutputStream(memoryStream);
zipOutputStream.PutNextEntry(new SharpZipLib.Zip.ZipEntry("0"));

for (int i = 0; i < ChunkCount; i++)
Expand Down
2 changes: 1 addition & 1 deletion src/ICSharpCode.SharpZipLib/Encryption/ZipAESStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public ZipAESStream(Stream stream, ZipAESTransform transform, CryptoStreamMode m
// total length of block + auth code
private const int BLOCK_AND_AUTH = CRYPTO_BLOCK_SIZE + AUTH_CODE_LENGTH;

private Stream _stream;
private readonly Stream _stream;
private ZipAESTransform _transform;
private byte[] _slideBuffer;
private int _slideBufStartPos;
Expand Down
12 changes: 8 additions & 4 deletions src/ICSharpCode.SharpZipLib/Encryption/ZipAESTransform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ public ZipAESTransform(string key, byte[] saltBytes, int blockSize, bool writeMo

// Performs the equivalent of derive_key in Dr Brian Gladman's pwd2key.c
#if NET472_OR_GREATER || NETSTANDARD2_1_OR_GREATER || NETCOREAPP2_0_OR_GREATER
var pdb = new Rfc2898DeriveBytes(key, saltBytes, KEY_ROUNDS, HashAlgorithmName.SHA1);
using var pdb = new Rfc2898DeriveBytes(key, saltBytes, KEY_ROUNDS, HashAlgorithmName.SHA1);
#else
var pdb = new Rfc2898DeriveBytes(key, saltBytes, KEY_ROUNDS);
using var pdb = new Rfc2898DeriveBytes(key, saltBytes, KEY_ROUNDS);
#endif
var rm = Aes.Create();
using var rm = Aes.Create();
rm.Mode = CipherMode.ECB; // No feedback from cipher for CTR mode
_counterNonce = new byte[_blockSize];
byte[] key1bytes = pdb.GetBytes(_blockSize);
Expand Down Expand Up @@ -171,7 +171,11 @@ public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int input
/// <summary>
/// Cleanup internal state.
/// </summary>
public void Dispose() => _encryptor.Dispose();
public void Dispose()
{
_encryptor.Dispose();
_hmacsha1.Dispose();
}

#endregion ICryptoTransform Members
}
Expand Down
1 change: 1 addition & 0 deletions src/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0</TargetFrameworks>
<LangVersion>8.0</LangVersion>
<IsTrimmable>true</IsTrimmable>
<EnableTrimAnalyzer>true</EnableTrimAnalyzer>
<SignAssembly>true</SignAssembly>
Expand Down
12 changes: 6 additions & 6 deletions src/ICSharpCode.SharpZipLib/Zip/ZipFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -863,7 +863,7 @@ public int FindEntry(string name, bool ignoreCase)
// TODO: This will be slow as the next ice age for huge archives!
for (int i = 0; i < entries_.Length; i++)
{
if (string.Compare(name, entries_[i].Name, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal) == 0)
if (string.Equals(name, entries_[i].Name, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal))
{
return i;
}
Expand Down Expand Up @@ -1208,7 +1208,7 @@ private long TestLocalHeader(ZipEntry entry, HeaderTest tests)
byte[] extraData = new byte[extraDataLength];
StreamUtils.ReadFully(baseStream_, extraData);

var localExtraData = new ZipExtraData(extraData);
using var localExtraData = new ZipExtraData(extraData);

// Extra data / zip64 checks
if (localExtraData.Find(headerID: 1))
Expand Down Expand Up @@ -2249,7 +2249,7 @@ private void WriteLocalEntryHeader(ZipUpdate update)
throw new ZipException("Entry name too long.");
}

var ed = new ZipExtraData(entry.ExtraData);
using var ed = new ZipExtraData(entry.ExtraData);

if (entry.LocalHeaderRequiresZip64)
{
Expand Down Expand Up @@ -2360,7 +2360,7 @@ private int WriteCentralDirectoryHeader(ZipEntry entry)
WriteLEShort(name.Length);

// Central header extra data is different to local header version so regenerate.
var ed = new ZipExtraData(entry.ExtraData);
using var ed = new ZipExtraData(entry.ExtraData);

if (entry.CentralHeaderRequiresZip64)
{
Expand Down Expand Up @@ -3795,7 +3795,7 @@ private Stream CreateAndInitDecryptionStream(Stream baseStream, ZipEntry entry)
{
if (entry.Version < ZipConstants.VersionStrongEncryption || !entry.HasFlag(GeneralBitFlags.StrongEncryption))
{
var classicManaged = new PkzipClassicManaged();
using var classicManaged = new PkzipClassicManaged();

OnKeysRequired(entry.Name);
if (HaveKeys == false)
Expand All @@ -3821,7 +3821,7 @@ private Stream CreateAndInitEncryptionStream(Stream baseStream, ZipEntry entry)
if (entry.Version >= ZipConstants.VersionStrongEncryption &&
entry.HasFlag(GeneralBitFlags.StrongEncryption)) return null;

var classicManaged = new PkzipClassicManaged();
using var classicManaged = new PkzipClassicManaged();

OnKeysRequired(entry.Name);
if (HaveKeys == false)
Expand Down
2 changes: 1 addition & 1 deletion src/ICSharpCode.SharpZipLib/Zip/ZipOutputStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,7 @@ private byte[] CreateZipCryptoHeader(long crcValue)
/// <param name="password">The password.</param>
private void InitializeZipCryptoPassword(string password)
{
var pkManaged = new PkzipClassicManaged();
using var pkManaged = new PkzipClassicManaged();
byte[] key = PkzipClassic.GenerateKeys(ZipCryptoEncoding.GetBytes(password));
cryptoTransform_ = pkManaged.CreateEncryptor(key, null);
}
Expand Down