Skip to content

ExtractContent() of a corrupt file holds the extracting file open #321

Closed

Description

@sslyle

Steps to reproduce

  1. zip a file, open the .zip file in Notepad++, intention delete the second half of the file.
  2. Create a folder and ExtractContent() into the new folder.
  3. Try to delete the file (being a developer you want to clean up after yourself don't you?)

Expected behavior

will be successfully deleted

Actual behavior

ExtractContent() throws "Unexpected EOF"
You can then .Close() the TarArchive, even .Dispose() but your code still has the file open and it can't be deleted. Not even from Windows File Explorer.
Neither can you delete the folder you just created to unzip the archive into.
You can not clean up after attempting to unzip a corrupted archive file

Version of SharpZipLib

(1.0.0)

Obtained from (only keep the relevant lines)

  • Compiled from source, commit: _______ - N/A
  • Downloaded from GitHub - NO
  • Package installed using NuGet - YES

Activity

  1. decipherer commented on Feb 15, 2019

    @decipherer
    Contributor

    I tried to reproduce this in a unit test, but:

    • I'm not sure I understand the problem exactly (you talk about zipping, but also about tar archives)
    • I can't reproduce that the file can't be deleted.

    Does this unit test reflect your problem?

    [Test]
    [Category("Tar")]
    public void Corrupted_Tar_Should_Be_Disposable()
    {
    	var tarfilename = Utils.GetDummyFileName();
    	using (var tarfile = new FileStream(tarfilename, FileMode.OpenOrCreate, FileAccess.ReadWrite))
    	{
    		using (var tarOut = TarArchive.CreateOutputTarArchive(tarfile))
    		using (var dummyFile = Utils.GetDummyFile(1024))
    		{
    			tarOut.IsStreamOwner = false;
    			tarOut.WriteEntry(TarEntry.CreateEntryFromFile(dummyFile.Filename), false);
    		}
    		tarfile.SetLength(200); // corrupt archive
    		tarfile.Seek(0, SeekOrigin.Begin);
    
    		using (var tarIn = TarArchive.CreateInputTarArchive(tarfile))
    		using (var tempDir = new Utils.TempDir())
    		{
    			tarIn.IsStreamOwner = false;
    			try
    			{
    				tarIn.ExtractContents(tempDir.Fullpath);
    			}
    			catch (Exception)
    			{ }
    		}
    	}
    	File.Delete(tarfilename);
    	Assert.IsFalse(File.Exists(tarfilename));
    }
  2. sslyle commented on Feb 15, 2019

    @sslyle
    Author
  3. sslyle commented on Feb 15, 2019

    @sslyle
    Author
  4. Numpsy commented on Feb 15, 2019

    @Numpsy
    Contributor

    You say that you zip the file, but then talk about extracting it with TarArchive - are you actually trying to extract a corrupt zip file with the Tar reader?

  5. Numpsy commented on Feb 15, 2019

    @Numpsy
    Contributor

    Does look like the file stream created at

    Stream outputStream = File.Create(destFile);

    might not be closed in some error cases though. (I guess the exact behaviour there would depend on exactly where the input data is corrupted though?)

  6. decipherer commented on Feb 16, 2019

    @decipherer
    Contributor

    I was confused by trying to open (half) a zip as a tar file, as they’re two different formats. But you’re right, ExtractContents should always clean up after itself, no matter the input. I’ll try changing the unit test to half a zip file and play with the exact length a bit to see if I’m able to reproduce the problem.

    I also noticed the lack of exception handling, therefore thinking I could easily reproduce the problem.

    The unit test in fact does throw an exception, but I’m not so much interested in the exception itself, therefore it just gets swallowed in the test. I’m interested in the lack of closing any file handles, even when the calling code is implemented properly. Therefore I try to delete the file at the end of the test, if the file handle isn’t closed I’d expect that to fail, throwing an exception either there or on the next line where it makes sure the file is no longer there.

    I do realize the test only covers half the problem you mention, only testing for the closing of the original tar file, not the extracted contents.

  7. sslyle commented on Feb 17, 2019

    @sslyle
    Author
  8. Numpsy commented on Feb 17, 2019

    @Numpsy
    Contributor

    I'd suggest adding a finally block to dispose of the stream in case of error, but I don't have a repro of an 'EOF' exception to test it with.

    In fact, it looks to me like there are some situations where it will create an output file of the original size, and then fill it with junk past the part where the data is truncated, when i would expect it to throw instead?

    If i run the test

    public void TarStreamReadTest()
    {
    	var tarfilename = Utils.GetDummyFileName();
    	using (var tarfile = new FileStream(tarfilename, FileMode.OpenOrCreate, FileAccess.ReadWrite))
    	{
    		using (var tarOut = TarArchive.CreateOutputTarArchive(tarfile))
    		using (var dummyFile = Utils.GetDummyFile(1024))
    		{
    			tarOut.IsStreamOwner = false;
    			tarOut.WriteEntry(TarEntry.CreateEntryFromFile(dummyFile.Filename), false);
    		}
    				
    		tarfile.SetLength(500); // corrupt archive
    		tarfile.Seek(0, SeekOrigin.Begin);
    
    		using (var tarIn = new TarInputStream(tarfile))
    		{
    			tarIn.IsStreamOwner = false;
    
    			TarEntry entry = tarIn.GetNextEntry();
    			byte[] buffer = new byte[2048];
    			var result = tarIn.Read(buffer, 0, buffer.Length);
    
    			Assert.That(result, Is.EqualTo(1024));
    		}
    	}
    }

    then it appears that call to TarInputStream.Read will report having read 1024 bytes, even though the input stream is only 500 - doesn't seem right?

  9. sslyle commented on Feb 17, 2019

    @sslyle
    Author
  10. Numpsy commented on Feb 17, 2019

    @Numpsy
    Contributor

    If the file is less than a certain size (I forget what the limit is offhand), then you can attach it to the bug entry via the Github web interface.

  11. sslyle commented on Feb 17, 2019

    @sslyle
    Author
  12. piksel commented on Feb 18, 2019

    @piksel
    Member

    The limit is 10MB.

  13. sslyle commented on Feb 18, 2019

    @sslyle
    Author

    You just might recognize the content of the attached zip file

    But again. All you have to do is 1) zip a file, 2) open the zipped file in Notepad++, 3) delete the second half of the file's content, 4) close and save.
    This approximates what happens when you are streaming a file over then inet, ftp, ... and and somethign cuts the stream off short.

    BrokenZipFile.zip

  14. sslyle commented on Feb 18, 2019

    @sslyle
    Author
  15. sslyle commented on Feb 18, 2019

    @sslyle
    Author
  16. decipherer commented on Feb 19, 2019

    @decipherer
    Contributor

    I'm sorry, but I'm not able to reproduce the problem with the file provided. I get a "System.ArgumentOutOfRangeException: 'Cannot be less than zero
    Parameter name: value'" (the same as I got from the unit test). After that the file handle gets closed properly.

    At the point of the exception, this hasn't even been called :

    Stream outputStream = File.Create(destFile);

    I tried reproducing this with partial zip files and tar archives, but cannot reproduce it that way. If I trigger a fault, manually editing some values using the debugger somewhere after that line, I am able to reproduce that the partially extracted file can not be deleted.

  17. Numpsy commented on Feb 19, 2019

    @Numpsy
    Contributor

    I get the same - looks to be issues in this area, but I can't repro the initially described one.

  18. sslyle commented on Feb 19, 2019

    @sslyle
    Author
  19. sslyle commented on Feb 21, 2019

    @sslyle
    Author

    I learned something new I never had to suspect before.
    A tarball zipped into a zip isn't just simply a zip file.
    The magic 2 characters are different.
    Either that or a zip's file first 2 and a gzip's first two are not the same.

    My work and where this case began are about a broken gzipped tar ball.
    The example file I provided are of a zip file (first 2 characters = 'PK')

    Attached is a broken gzipped tarball.

    What results to you get when you use this attachment?

    Thanks.

    NotBrokenTarZip.tar.gz
    NotBrokenTarZip.tar.gz

  20. Numpsy commented on Feb 21, 2019

    @Numpsy
    Contributor

    Haven't had chance to try it, but it does seem more likely that something would fail if the stream read @

    int numRead = tarIn.Read(rdbuf, 0, rdbuf.Length);

    is actually streaming from a GZip stream that is decompressing as it goes.

  21. Numpsy commented on Feb 21, 2019

    @Numpsy
    Contributor

    #326 possibly? (had to make the unit test truncate the file to a length greater than the tar reader record buffer size, otherwise it would throw before getting as far as creating the output files)

  22. Numpsy commented on Aug 10, 2019

    @Numpsy
    Contributor

    Hopefully fixed by #326.

  23. piksel commented on Aug 11, 2019

    @piksel
    Member

    Should be fixed. If it persists, make a new Issue (this one contains too much side tracking anyway)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions