Skip to content

Commit

Permalink
fix(tar): prevent infinite loop when reading corrupted archive
Browse files Browse the repository at this point in the history
  • Loading branch information
Blokyk committed Apr 24, 2024
1 parent b203d16 commit dbbc7c8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/SharpCompress/Archives/Tar/TarArchive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ var header in TarHeaderFactory.ReadHeader(
CompressionType.None
);
}
} 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 dbbc7c8

Please sign in to comment.