Skip to content

Commit

Permalink
validate that arrow IPC files include a valid embedded IPC stream wit…
Browse files Browse the repository at this point in the history
…h EOS
  • Loading branch information
bkietz committed Aug 26, 2024
1 parent 2328b6e commit 2f03fba
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions cpp/src/arrow/integration/json_integration_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,34 @@ static Status ValidateFull(const RecordBatch& batch) {
return Status::OK();
}

static Status ValidateEmbeddedStream(
const std::shared_ptr<io::RandomAccessFile>& arrow_file) {
// Many validations are skipped here since they will already
// have been handled by RecordBatchFileReader.
// For example we already know that the magic is in place.
ARROW_ASSIGN_OR_RAISE(int64_t file_size, arrow_file->GetSize());
ARROW_ASSIGN_OR_RAISE(auto footer_cookie, arrow_file->ReadAt(file_size - 10, 10));
auto footer_size =
bit_util::FromLittleEndian(util::SafeLoadAs<int32_t>(footer_cookie->data()));
int64_t footer_offset = 8 + file_size - footer_size - 10;

// Get a read stream past the padded magic at the start of the file
ARROW_ASSIGN_OR_RAISE(auto stream,
io::RandomAccessFile::GetStream(arrow_file, 8, file_size - 8));
ARROW_ASSIGN_OR_RAISE(auto arrow_reader, ipc::RecordBatchStreamReader::Open(stream));
for (auto maybe_batch : *arrow_reader) {
RETURN_NOT_OK(maybe_batch.status());
}
ARROW_ASSIGN_OR_RAISE(int64_t stream_size, stream->Tell());

if (footer_offset <= stream_size) {
return Status::Invalid("Embedded stream (", stream_size,
" bytes long) overlaps with the file's footer (at offset ",
footer_offset, ")");
}
return Status::OK();
}

static Status ValidateArrowVsJson(const std::string& arrow_path,
const std::string& json_path) {
// Construct JSON reader
Expand Down Expand Up @@ -217,6 +245,7 @@ static Status ValidateArrowVsJson(const std::string& arrow_path,
}
}

RETURN_NOT_OK(ValidateEmbeddedStream(arrow_file));
return Status::OK();
}

Expand Down

0 comments on commit 2f03fba

Please sign in to comment.