From 3292d786667875dd42ca4e31a7e2ddf6837ee713 Mon Sep 17 00:00:00 2001 From: pedrobtz Date: Wed, 21 Oct 2026 23:51:02 +0200 Subject: [PATCH] fix(ipc): Don't read past the footer when reporting a bad file magic The error message printed the input as a string starting one byte past its end. Print the six bytes that were compared instead. Co-Authored-By: Claude Opus 5.5 --- src/nanoarrow/ipc/decoder.c | 3 ++- src/nanoarrow/ipc/decoder_test.cc | 8 ++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/nanoarrow/ipc/decoder.c b/src/nanoarrow/ipc/decoder.c index 80e53ec8d..690604d08 100644 --- a/src/nanoarrow/ipc/decoder.c +++ b/src/nanoarrow/ipc/decoder.c @@ -1701,7 +1701,8 @@ ArrowErrorCode ArrowIpcDecoderPeekFooter(struct ArrowIpcDecoder* decoder, const char* footer_size_data = magic - sizeof(int32_t); if (memcmp(magic, NANOARROW_IPC_MAGIC, strlen(NANOARROW_IPC_MAGIC)) != 0) { - ArrowErrorSet(error, "Expected file to end with ARROW1 but got %s", data_end); + // data is not NUL-terminated: print only the bytes that were compared + ArrowErrorSet(error, "Expected file to end with ARROW1 but got %.6s", magic); return EINVAL; } diff --git a/src/nanoarrow/ipc/decoder_test.cc b/src/nanoarrow/ipc/decoder_test.cc index 8f89acc6f..14c54f173 100644 --- a/src/nanoarrow/ipc/decoder_test.cc +++ b/src/nanoarrow/ipc/decoder_test.cc @@ -16,6 +16,7 @@ // under the License. #include +#include #include #if defined(NANOARROW_BUILD_TESTS_WITH_ARROW) @@ -1312,6 +1313,13 @@ TEST(NanoarrowIpcTest, NanoarrowIpcFooterDecodingErrors) { EINVAL) << error.message; + // the message must not read past the end of data, which isn't NUL-terminated + std::unique_ptr unterminated(new char[10]); + memcpy(unterminated.get(), "\0\0\0\0blargh", 10); + EXPECT_EQ(ArrowIpcDecoderPeekFooter(decoder.get(), {{unterminated.get()}, 10}, &error), + EINVAL); + EXPECT_STREQ(error.message, "Expected file to end with ARROW1 but got blargh"); + // negative size EXPECT_EQ(ArrowIpcDecoderPeekFooter(decoder.get(), {{"\xFF\xFF\xFF\xFF"