2

I have a code block that looks like this:

using (MemoryStream outputZip = new MemoryStream())
{
    using (ZipOutputStream zipstream = new ZipOutputStream(outputZip))
    {
        zipstream.Password = req.password;
        zipstream.SetLevel(3);
        foreach (var doc in loadocs)
        {
            ZipEntry entry = new ZipEntry(doc.FileName);
            zipstream.PutNextEntry(entry);
            zipstream.Write(doc.FileData, 0, doc.FileData.Length);
        }

        zipstream.CloseEntry();
        zipstream.IsStreamOwner = false;
    }
    response.result = new Document()
    {
        name = doc1.Custodian + DateTime.Now.Date.ToString().Replace('/', '_').Replace(':', '_') + ".zip",
        content = outputZip.ToArray()
    };
}

I keep getting back

CWE 404 - Improper resource shutdown or release

pointing to the first line. Shouldn't the using statement take care of disposing of the object when it is completed? I can't seem to mitigate this error no matter what I do. I have tried to wrap everything up in try/catch/finally and dispose of outputZip in the finally but same error results pointing to declaration of outputZip.

I have updated my code to look like this:

 MemoryStream zipOutput = new MemoryStream();
                    try
                    {
                        ZipOutputStream zipstream = new ZipOutputStream(zipOutput);
                        try
                        {

                        }
                        catch (Exception ex)
                        {

                        }
                        finally
                        {
                            zipstream = null;
                            zipstream.Close();
                        }
                    }
                    catch(Exception ex)
                    {

                    }
                    finally
                    {
                        zipOutput = null;
                        zipOutput.Close();
                    }

I am still seeing the Improper resource shutdown or release on the MemoryStream. Although very redundant doesn't this code actually release the resource?

VinnyGuitara
  • 605
  • 8
  • 26
  • 1
    You seem to be using old software that does not know of the `using` statement in c#. Your code is fine, the error is not. VS also has this issue on some of the warnings you can set up on FxCop – Camilo Terevinto Jan 16 '18 at 17:08
  • 1
    The software that this is running through is veracode - I'm not sure we have the ability to ignore this warning as it is a third party service. – VinnyGuitara Jan 16 '18 at 17:12

0 Answers0