You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ExtractContent() of a corrupt file holds the extracting file open #321
zip a file, open the .zip file in Notepad++, intention delete the second half of the file.
Create a folder and ExtractContent() into the new folder.
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
Take a file and zip it; I use 7-Zip
Open the zip file in Notepad++ and delete the second half of the file.
Save the file.
Pass the file to ExtractContents(); I have ExtractContents() extracting to
the folder pointed to by the Windows' TEMP environment variable.
ExtractContents() you should see ExtractContent() throw the "EOF" file
error.
-- Correct me if I am wrong but in your code you will never know if
ExtractContent() throws anything because your catch will just toss any
problems await unseen.
After ExtractContents() crashes open Windows File Explorer and try to
delete THE PARTIALLY UNZIPPED CONTENTS.
Do this on a WINDOWS box, not Linux.
On Fri, Feb 15, 2019 at 5:16 AM decipherer ***@***.***> wrote:
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 not reproduce that the file cannot 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));
}
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#321 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AFZh2s3IaTlA3fouWtgIpnU1VHgbb6owks5vNokZgaJpZM4a8wdQ>
.
--
"In preparing for battle I have always found that plans are useless, but
planning is indispensable."
- General Dwight D. Eisenhower
“For the things we have to learn before we can do them, we learn by doing
them.”
- Aristotle, The Nicomachean Ethics
"A leader is someone who steps back from the entire system and tries to
build a more collaborative, more innovative system that will work over the
long term."
- Robert Reich
PS in addition to my reply of a minute ago I also opened your source code.
Line 644 of TarArchive.cs doesn't do anything to clean up file handles in
the advent of a Exception.
I don't see a try-catch or anything else trying to handle critical errors
thrown by .Net nor the OS in ExtractContents() or ExtractEntry(); lines
549-680.
On Fri, Feb 15, 2019 at 5:16 AM decipherer ***@***.***> wrote:
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 not reproduce that the file cannot 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));
}
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#321 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AFZh2s3IaTlA3fouWtgIpnU1VHgbb6owks5vNokZgaJpZM4a8wdQ>
.
--
"In preparing for battle I have always found that plans are useless, but
planning is indispensable."
- General Dwight D. Eisenhower
“For the things we have to learn before we can do them, we learn by doing
them.”
- Aristotle, The Nicomachean Ethics
"A leader is someone who steps back from the entire system and tries to
build a more collaborative, more innovative system that will work over the
long term."
- Robert Reich
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?
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.
--
"In preparing for battle I have always found that plans are useless, but
planning is indispensable."
- General Dwight D. Eisenhower
“For the things we have to learn before we can do them, we learn by doing
them.”
- Aristotle, The Nicomachean Ethics
"A leader is someone who steps back from the entire system and tries to
build a more collaborative, more innovative system that will work over the
long term."
- Robert Reich
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?
On Sun, Feb 17, 2019, 17:18 Richard Webb ***@***.*** wrote:
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?
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#321 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AFZh2s9DCvxd-7nWphHBRVBnU3e46Ab1ks5vOdVAgaJpZM4a8wdQ>
.
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.
--
"In preparing for battle I have always found that plans are useless, but
planning is indispensable."
- General Dwight D. Eisenhower
“For the things we have to learn before we can do them, we learn by doing
them.”
- Aristotle, The Nicomachean Ethics
"A leader is someone who steps back from the entire system and tries to
build a more collaborative, more innovative system that will work over the
long term."
- Robert Reich
--
"In preparing for battle I have always found that plans are useless, but
planning is indispensable."
- General Dwight D. Eisenhower
“For the things we have to learn before we can do them, we learn by doing
them.”
- Aristotle, The Nicomachean Ethics
"A leader is someone who steps back from the entire system and tries to
build a more collaborative, more innovative system that will work over the
long term."
- Robert Reich
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 :
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.
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?
#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)
Activity
decipherer commentedon Feb 15, 2019
I tried to reproduce this in a unit test, but:
Does this unit test reflect your problem?
sslyle commentedon Feb 15, 2019
sslyle commentedon Feb 15, 2019
Numpsy commentedon Feb 15, 2019
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?
Numpsy commentedon Feb 15, 2019
Does look like the file stream created at
SharpZipLib/src/ICSharpCode.SharpZipLib/Tar/TarArchive.cs
Line 628 in 25ee644
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?)
decipherer commentedon Feb 16, 2019
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.
sslyle commentedon Feb 17, 2019
Numpsy commentedon Feb 17, 2019
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
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?
sslyle commentedon Feb 17, 2019
Numpsy commentedon Feb 17, 2019
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.
sslyle commentedon Feb 17, 2019
piksel commentedon Feb 18, 2019
The limit is 10MB.
sslyle commentedon Feb 18, 2019
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
sslyle commentedon Feb 18, 2019
sslyle commentedon Feb 18, 2019
decipherer commentedon Feb 19, 2019
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 :
SharpZipLib/src/ICSharpCode.SharpZipLib/Tar/TarArchive.cs
Line 628 in 25ee644
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.
Numpsy commentedon Feb 19, 2019
I get the same - looks to be issues in this area, but I can't repro the initially described one.
sslyle commentedon Feb 19, 2019
sslyle commentedon Feb 21, 2019
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
Numpsy commentedon Feb 21, 2019
Haven't had chance to try it, but it does seem more likely that something would fail if the stream read @
SharpZipLib/src/ICSharpCode.SharpZipLib/Tar/TarArchive.cs
Line 644 in 25ee644
is actually streaming from a GZip stream that is decompressing as it goes.
Numpsy commentedon Feb 21, 2019
#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)
Numpsy commentedon Aug 10, 2019
Hopefully fixed by #326.
piksel commentedon Aug 11, 2019
Should be fixed. If it persists, make a new Issue (this one contains too much side tracking anyway)