Skip to content

Commit

Permalink
Merge pull request #835 from Blokyk/fix-736
Browse files Browse the repository at this point in the history
Prevent infinite loop when reading corrupted archive
  • Loading branch information
adamhathcock authored Apr 24, 2024
2 parents b203d16 + 9483856 commit 7488802
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/SharpCompress/Archives/Tar/TarArchive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@ var header in TarHeaderFactory.ReadHeader(
);
}
}
else
{
throw new IncompleteArchiveException("Failed to read TAR header");
}
}
}

Expand Down
15 changes: 15 additions & 0 deletions tests/SharpCompress.Test/Tar/TarReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,21 @@ public void Tar_Broken_Stream()
Assert.Throws<IncompleteArchiveException>(() => reader.MoveToNextEntry());
}

[Fact]
public void Tar_Corrupted()
{
var archiveFullPath = Path.Combine(TEST_ARCHIVES_PATH, "TarCorrupted.tar");
using Stream stream = File.OpenRead(archiveFullPath);
using var reader = ReaderFactory.Open(stream);
var memoryStream = new MemoryStream();

Assert.True(reader.MoveToNextEntry());
Assert.True(reader.MoveToNextEntry());
reader.WriteEntryTo(memoryStream);
stream.Close();
Assert.Throws<IncompleteArchiveException>(() => reader.MoveToNextEntry());
}

#if !NETFRAMEWORK
[Fact]
public void Tar_GZip_With_Symlink_Entries()
Expand Down
Binary file added tests/TestArchives/Archives/TarCorrupted.tar
Binary file not shown.

0 comments on commit 7488802

Please sign in to comment.