Skip to content

GH-33823: [C++][IPC] Improve error messages when opening files that are the wrong format#49771

Open
RobertLD wants to merge 6 commits into
apache:mainfrom
RobertLD:33823-improve-err-msg-for-file-reader
Open

GH-33823: [C++][IPC] Improve error messages when opening files that are the wrong format#49771
RobertLD wants to merge 6 commits into
apache:mainfrom
RobertLD:33823-improve-err-msg-for-file-reader

Conversation

@RobertLD

@RobertLD RobertLD commented Apr 16, 2026

Copy link
Copy Markdown

Rationale for this change

The original error messages did not provide instruction to users on how to best correct their usage when opening an Arrow IPC file with the wrong reader.

What changes are included in this PR?

Improved error messages when an IPC file is opened with the stream reader and vice versa, guiding users to the correct reader.

PR review suggestions applied:

  • The file-magic detection in DecodeMessage now compares the decoded metadata length against a constant derived from FromLittleEndian, hopefully avoiding any little/big endianness issues
  • The magic check now runs before the large file->Read(metadata_length) call, avoiding the 1.3 GB read when an IPC file is fed to the stream reader.
  • Rewording error message again
  • Test substring matches use more specific strings

Are these changes tested?

Yes. New tests were added to OpenFileSuggestsFileReader

  1. CorruptDataDoesNotSuggestFileReader
  2. OpenStreamFormatSuggestsStreamReader

Existing Tests executed and passing.

Are there any user-facing changes?

Yes, error messages when using the wrong IPC reader have been updated.

@github-actions

Copy link
Copy Markdown

Thanks for opening a pull request!

If this is not a minor PR. Could you open an issue for this pull request on GitHub? https://github.com/apache/arrow/issues/new/choose

Opening GitHub issues ahead of time contributes to the Openness of the Apache Arrow project.

Then could you also rename the pull request title in the following format?

GH-${GITHUB_ISSUE_ID}: [${COMPONENT}] ${SUMMARY}

or

MINOR: [${COMPONENT}] ${SUMMARY}

See also:

@RobertLD RobertLD changed the title #33823 Improve err msgs when opening files that are the wrong format GH-33823: [C++] Improve err msgs when opening files that are the wrong format Apr 16, 2026
@RobertLD
RobertLD force-pushed the 33823-improve-err-msg-for-file-reader branch from d7f5423 to abd2b9c Compare April 16, 2026 12:48
@RobertLD
RobertLD marked this pull request as ready for review April 16, 2026 14:04
@kou
kou requested a review from Copilot April 19, 2026 21:12

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves user-facing error messages in the C++ IPC readers/decoders when callers attempt to open an IPC File as a Stream (or vice versa), providing more actionable guidance aligned with GH-33823.

Changes:

  • Update the IPC file reader footer/magic check failure message to suggest the streaming reader when appropriate.
  • Add a heuristic in the IPC stream message decoder to detect an IPC file magic prefix being misinterpreted as a stream message length, and emit a more instructive error.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
cpp/src/arrow/ipc/reader.cc Improves the “not an Arrow file” error when the footer magic doesn’t match, suggesting the streaming reader.
cpp/src/arrow/ipc/message.cc Adds detection for IPC File magic bytes when decoding a stream message and returns a more targeted “wrong format” error.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread cpp/src/arrow/ipc/reader.cc Outdated
Comment thread cpp/src/arrow/ipc/message.cc Outdated
@RobertLD

RobertLD commented Apr 19, 2026

Copy link
Copy Markdown
Author

Decent feedback Mr. Bot, I'll make changes shortly. Honestly impressed the LLM review had such good context. Good bot

RobertLD and others added 2 commits April 24, 2026 10:41
Create more generic message instead of referencing c++ interface

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Similarly, keep messages more generic and not referencing the c++ interface

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@RobertLD

Copy link
Copy Markdown
Author

@kou Comments addressed

@kou
kou requested a review from Copilot April 28, 2026 02:56
@kou kou changed the title GH-33823: [C++] Improve err msgs when opening files that are the wrong format GH-33823: [C++] Improve error messages when opening files that are the wrong format Apr 28, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@kou

kou commented Apr 28, 2026

Copy link
Copy Markdown
Member

Could you add tests for these cases?

We want to ensure we're providing advice to the user that is correct/actionable.
@RobertLD

RobertLD commented Apr 29, 2026

Copy link
Copy Markdown
Author

Added a pos/neg case, kept the string matching minimal so the tests weren't as flakey is the messages get reworked in another way in the future. If it's standard practice to match the entire err message I can make that change over a subset match @kou

@pitrou

pitrou commented May 7, 2026

Copy link
Copy Markdown
Member

Thanks for the PR @RobertLD . This is a good idea. In the future I think it would be more convenient if IPC files could read using the IPC stream reader (*), at least with a dedicated option enabled; but this PR is a good start.

(*) This was discussed a bit on the dev ML: https://lists.apache.org/thread/jpxl3yzm96wkxzb1clokxklsy32b3plh

Comment thread cpp/src/arrow/ipc/message.cc Outdated
Comment on lines +572 to +573
std::string_view(reinterpret_cast<const char*>(metadata->data()),
remaining_magic.size()) == remaining_magic) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would only work on a little-endian machine. We need to be endianness-agnostic (see ConsumeInitialData).

Comment thread cpp/src/arrow/ipc/message.cc Outdated
ARROW_ASSIGN_OR_RAISE(auto metadata, file->Read(metadata_length));
if (metadata->size() != metadata_length) {
// The first sizeof(int32_t) bytes of the Arrow file magic ("ARRO") may have been
// misread as metadata_length. Check if the remaining bytes complete the magic.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How likely is it to have a metadata message exactly 1330795073 bytes long ("ARRO" decoded as a 32-bit little-endian integer)? Perhaps we can check up front instead of trying to read so much data?

cc @lidavidm @paleolimbot for opinions.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, a gigabyte of metadata raises flags anyways

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In nanoarrow we check the first few bytes for the magic string and skip them (then attempt to read the rest of the input as an IPC stream). We've never run into a complaint about this not working but I'm not sure how widespread the usage is (we could add an option to turn it off or improve the error that occurs if we run into one). I think 1330795073 bytes of metadata would never reasonably occur on purpose.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved the check to happen first, and avoid the large read if possible

Comment thread cpp/src/arrow/ipc/read_write_test.cc Outdated

io::BufferReader reader(helper.buffer_);
// Check we mention using the file_reader when we detect file format
EXPECT_RAISES_WITH_MESSAGE_THAT(Invalid, ::testing::HasSubstr("file reader"),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we be a bit more specific and match a longer substring? For example "Try the IPC file reader".

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed

Comment thread cpp/src/arrow/ipc/read_write_test.cc Outdated
auto buf_reader = std::make_shared<io::BufferReader>(helper.buffer_);
// Check we mention using the stream_reader when we detect stream format
EXPECT_RAISES_WITH_MESSAGE_THAT(
Invalid, ::testing::HasSubstr("stream reader"),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

Comment thread cpp/src/arrow/ipc/reader.cc Outdated
@pitrou pitrou changed the title GH-33823: [C++] Improve error messages when opening files that are the wrong format GH-33823: [C++][IPC] Improve error messages when opening files that are the wrong format May 7, 2026
@pitrou

pitrou commented May 7, 2026

Copy link
Copy Markdown
Member

Also can you make the PR description a bit more specific @RobertLD ?

@github-actions github-actions Bot added awaiting committer review Awaiting committer review awaiting changes Awaiting changes and removed awaiting review Awaiting review awaiting committer review Awaiting committer review labels May 7, 2026
@RobertLD

RobertLD commented May 7, 2026 via email

Copy link
Copy Markdown
Author

Fix endianness bug, avoid huge metadata reads and alter err messages
@github-actions github-actions Bot added awaiting change review Awaiting change review and removed awaiting changes Awaiting changes labels May 12, 2026
@RobertLD

Copy link
Copy Markdown
Author

Also can you make the PR description a bit more specific @RobertLD ?

Addressed this and other PR suggestions with my latest commit
d01cb5d

Comment on lines +575 to +577
if (peek->size() >= static_cast<int64_t>(kRemainingMagic.size()) &&
std::string_view(reinterpret_cast<const char*>(peek->data()),
kRemainingMagic.size()) == kRemainingMagic) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if this check fails? Are we assuming that the file is a valid IPC stream that happens to have a metadata size exactly 0x4F525241 bytes?

In any case, we have read kRemainingMagic bytes that are discarded below, so we'll be out of sync anyway.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pitrou Okay I see what you're saying and this was an oversight on my part. My idea is to continue to let it fall through and add a separate check immediately below it to reject metadata over 1GB. I think this is the best solution but it does change the behavior of the library (and I'm concerned there may be a single user somewhere who has 10Gb of metadata) and it's a bit of stretch scope wise considering this ticket is about error messages.

What are your thoughts? I'm thinking I just go forward with the above (or alternatively, just throw a failure with a different message on specifically 0x4F525241 even if it's not IPC)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Letting it fall through means a mythical user with a valid 0x4F525241 bytes metadata block will get a regression with a weird message decoding the Flatbuffers metadata. I'd rather an explicit message.

@RobertLD

Copy link
Copy Markdown
Author

I will address this last comment shortly, but am on vacation. Just pinging this so it doesn't get closed as stale

@pitrou

pitrou commented Jun 16, 2026

Copy link
Copy Markdown
Member

I'm thinking of another problem: this will detect the IPC file marker for every metadata message, but we actually only want to do that at the start of the stream. State::INITIAL would not be enough to detect that, because we go back to INITIAL after each successfully decoded message.

Perhaps DecodeMessage is not the right place for this and this should be done at a higher level in the call tree?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants