adamhathcock / sharpcompress

SharpCompress is a fully managed C# library to deal with many compression types and formats.
MIT License
2.25k stars 479 forks source link

IReader.MoveToNextEntry() not terminating #126

Closed eklann closed 8 years ago

eklann commented 8 years ago

I have encountered an issue with reading corrupt rar-files. When reading the entries in the file, the method IReader.MoveToNextEntry does not terminate, and the programs hangs. The program still consumes a lot of CPU, so it is doing something. I have tried to wait, but after 20 hours nothing has happened.

The following code can reproduce the issue:

using SharpCompress.Archive;
using SharpCompress.Reader;
using System;

namespace NonTerminatingRar
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            IArchive archive = ArchiveFactory.Open(args[0]);
            using (IReader reader = archive.ExtractAllEntries())
            {
                while (reader.MoveToNextEntry())
                {
                    Console.WriteLine(reader.Entry.Key);
                }
            }
        }
    }
}

I am using Sharpcompress 0.11.4 from nuget.org

I also have a file that can be used to reproduce the problem, but I am not sure where to send it? It is approximately 100MB

adamhathcock commented 8 years ago

If the RAR is corrupt, then I can't do anything about it. SharpCompress doesn't do anything to detect corrupt entries. It tries to extract and I guess the decompress is confused and looping.

eklann commented 8 years ago

I understand that I cannot expect the extraction to work. But I do expect it to terminate, for example by throwing an exception.

adamhathcock commented 8 years ago

If it's looping in the decompression, how do you propose detecting it? It's not just a matter of throwing an exception "if something bad happens." The problem is knowing if something bad happens. There is nothing that detects loops like this.

If you want, you can see if WinRAR itself detects it and perhaps suggest checksums or something is done but then again, you don't have a checksum if you can't decompress it.

eklann commented 8 years ago

I don't know about the inner workings of rar decompression, so I don't have a solution to this issue. I am posting it since I believe it to be a problem.

WinRAR gives me a Diagnostic message when opening the file, saying "Unexpected end of archive", and after that I can extract one file from the rar. The test program I provided lists the same file as WinRAR, but hangs after that.

adamhathcock commented 8 years ago

It might be an issue where I need to try to count the bytes read or there's a loop in header expectation. It all depends on the type of corrupted file. It's just not nothing I've worked on: making all corrupt scenarios work. For better or worse, I've just been concentrating on the happy path scenarios.

eklann commented 8 years ago

Would you like me to send you the file that can trigger this issue? If so, how?

adamhathcock commented 8 years ago

Sorry, I don't think I'm going to have any time soon to go down this rabbit hole. I'm really not inclined to for corrupt RARs.

You could get the source and debug it yourself though.

eklann commented 8 years ago

When do you think that this fix will be released on nuget.org?

adamhathcock commented 8 years ago

Put it there now.