1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 16:27:35 +00:00

LibArchive: Implement proper support for Tar file end markers

Previously this was handled implicitly, as our implementation of Tar
would just stop processing input as soon as it found something invalid.
However, since we now error out as soon as something is found to be
wrong, we require proper handling for zero blocks, which aren't actually
fatal.
This commit is contained in:
Tim Schumacher 2022-11-29 01:01:13 +01:00 committed by Andreas Kling
parent cb48b9bc30
commit 714f0c3dce
4 changed files with 33 additions and 7 deletions

View file

@ -90,16 +90,30 @@ ErrorOr<void> TarInputStream::advance()
ErrorOr<void> TarInputStream::load_next_header()
{
auto header_span = TRY(m_stream->read(Bytes(&m_header, sizeof(m_header))));
if (header_span.size() != sizeof(m_header))
return Error::from_string_literal("Failed to read the entire header");
size_t number_of_consecutive_zero_blocks = 0;
while (true) {
auto header_span = TRY(m_stream->read(Bytes(&m_header, sizeof(m_header))));
if (header_span.size() != sizeof(m_header))
return Error::from_string_literal("Failed to read the entire header");
// Discard the rest of the header block.
TRY(m_stream->discard(block_size - sizeof(TarFileHeader)));
if (!header().is_zero_block())
break;
number_of_consecutive_zero_blocks++;
// Two zero blocks in a row marks the end of the archive.
if (number_of_consecutive_zero_blocks >= 2) {
m_found_end_of_archive = true;
return {};
}
}
if (!TRY(valid()))
return Error::from_string_literal("Header has an invalid magic or checksum");
// Discard the rest of the header block.
TRY(m_stream->discard(block_size - sizeof(TarFileHeader)));
return {};
}