Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions src/nanoarrow/ipc/decoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -1750,11 +1750,12 @@ ArrowErrorCode ArrowIpcDecoderVerifyFooter(struct ArrowIpcDecoder* decoder,

// Check that data contains at least the entire footer (return ESPIPE to signal
// that reading more data may help).
int32_t footer_and_size_and_magic_size =
decoder->header_size_bytes + sizeof(int32_t) + (int)strlen(NANOARROW_IPC_MAGIC);
int64_t footer_and_size_and_magic_size = (int64_t)decoder->header_size_bytes +
(int64_t)sizeof(int32_t) +
(int64_t)strlen(NANOARROW_IPC_MAGIC);
if (data.size_bytes < footer_and_size_and_magic_size) {
ArrowErrorSet(error,
"Expected >= %d bytes of data but only %" PRId64
"Expected >= %" PRId64 " bytes of data but only %" PRId64
" bytes are in the buffer",
footer_and_size_and_magic_size, data.size_bytes);
return ESPIPE;
Expand Down Expand Up @@ -1960,8 +1961,19 @@ ArrowErrorCode ArrowIpcDecoderDecodeFooter(struct ArrowIpcDecoder* decoder,
struct ArrowIpcDecoderPrivate* private_data =
(struct ArrowIpcDecoderPrivate*)decoder->private_data;

int32_t footer_and_size_and_magic_size =
decoder->header_size_bytes + sizeof(int32_t) + (int)strlen(NANOARROW_IPC_MAGIC);
// Check that data contains at least the entire footer (return ESPIPE to signal
// that reading more data may help).
int64_t footer_and_size_and_magic_size = (int64_t)decoder->header_size_bytes +
(int64_t)sizeof(int32_t) +
(int64_t)strlen(NANOARROW_IPC_MAGIC);
if (data.size_bytes < footer_and_size_and_magic_size) {
ArrowErrorSet(error,
"Expected >= %" PRId64 " bytes of data but only %" PRId64
" bytes are in the buffer",
footer_and_size_and_magic_size, data.size_bytes);
return ESPIPE;
}

const uint8_t* footer_data =
data.data.as_uint8 + data.size_bytes - footer_and_size_and_magic_size;
ns(Footer_table_t) footer = ns(Footer_as_root(footer_data));
Expand Down
23 changes: 23 additions & 0 deletions src/nanoarrow/ipc/decoder_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1372,6 +1372,29 @@ TEST(NanoarrowIpcTest, NanoarrowIpcFooterDecodingErrors) {
EXPECT_EQ(decoder->header_size_bytes, 0xFFFF);
}

TEST(NanoarrowIpcTest, NanoarrowIpcFooterSizeOverflow) {
struct ArrowError error;

nanoarrow::ipc::UniqueDecoder decoder;
ArrowIpcDecoderInit(decoder.get());

// A footer size close to INT32_MAX must not overflow the size computation
struct ArrowBufferView data = {{"\0\0\0\0\0\0"
"\xFE\xFF\xFF\x7F"
"ARROW1"},
16};
EXPECT_EQ(ArrowIpcDecoderVerifyFooter(decoder.get(), data, &error), ESPIPE);
EXPECT_STREQ(error.message,
"Expected >= 2147483656 bytes of data but only 16 bytes are in the "
"buffer");
EXPECT_EQ(decoder->header_size_bytes, 0x7FFFFFFE);

// DecodeFooter must not read out of bounds either
EXPECT_EQ(ArrowIpcDecoderPeekFooter(decoder.get(), data, &error), NANOARROW_OK)
<< error.message;
EXPECT_EQ(ArrowIpcDecoderDecodeFooter(decoder.get(), data, &error), ESPIPE);
}

#if defined(NANOARROW_BUILD_TESTS_WITH_ARROW)
class ArrowTypeParameterizedTestFixture
: public ::testing::TestWithParam<std::shared_ptr<arrow::DataType>> {
Expand Down
Loading