LibArchive: Move loading the next tar header into a helper function

This now also validates the first header that is loaded, so we can drop
the corresponding FIXME from `tar`.
This commit is contained in:
Tim Schumacher 2022-11-28 19:07:39 +01:00 committed by Andreas Kling
parent cbeaba0c12
commit fd3a823a20
Notes: sideshowbarker 2024-07-17 17:38:29 +09:00
4 changed files with 9 additions and 16 deletions

View File

@ -25,9 +25,6 @@ extern "C" int LLVMFuzzerTestOneInput(uint8_t const* data, size_t size)
auto tar_stream = tar_stream_or_error.release_value();
if (!tar_stream->valid())
return 0;
while (!tar_stream->finished()) {
auto const& header = tar_stream->header();

View File

@ -56,13 +56,7 @@ ErrorOr<NonnullOwnPtr<TarInputStream>> TarInputStream::construct(NonnullOwnPtr<C
{
auto tar_stream = TRY(adopt_nonnull_own_or_enomem(new (nothrow) TarInputStream(move(stream))));
// Try and read the header.
auto header_span = TRY(tar_stream->m_stream->read(Bytes(&tar_stream->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 block.
TRY(tar_stream->m_stream->discard(block_size - sizeof(TarFileHeader)));
TRY(tar_stream->load_next_header());
return tar_stream;
}
@ -89,7 +83,13 @@ ErrorOr<void> TarInputStream::advance()
TRY(m_stream->discard(block_ceiling(file_size) - m_file_offset));
m_file_offset = 0;
// FIXME: This is not unlike the initial initialization. Maybe we should merge those two.
TRY(load_next_header());
return {};
}
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");

View File

@ -45,6 +45,7 @@ public:
private:
TarInputStream(NonnullOwnPtr<Core::Stream::Stream>);
ErrorOr<void> load_next_header();
TarFileHeader m_header;
NonnullOwnPtr<Core::Stream::Stream> m_stream;

View File

@ -88,11 +88,6 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
}());
auto tar_stream = TRY(Archive::TarInputStream::construct(move(input_stream)));
// FIXME: implement ErrorOr<TarInputStream>?
if (!tar_stream->valid()) {
warnln("the provided file is not a well-formatted ustar file");
return 1;
}
HashMap<String, String> global_overrides;
HashMap<String, String> local_overrides;