Skip to content

[C++][Feather] Avoid reading outside sliced bitmap buffers in V1 writer #50762

Description

@tcmartin

Describe the bug

The Feather V1 writer shifts bitmap bytes when serializing a sliced Boolean
array or validity bitmap. On the final output byte, the shift loop
unconditionally reads the next source byte even when the logical slice ends in
the current byte.

A valid ArrayData backed by an exact-sized Buffer therefore causes a
one-byte heap-buffer-overflow read. The adjacent bit is also folded into the
unused bits of the serialized bitmap byte.

Component

C++ IPC / Feather V1 writer.

Version

Reproduced on current main at
35c5ffd12173284406e4a2c86405415444e596d7.

Minimal reproduction

auto owner = std::make_unique<uint8_t[]>(1);
owner[0] = 0x03;
auto values = std::make_shared<arrow::Buffer>(owner.get(), 1);

auto data = arrow::ArrayData::Make(arrow::boolean(), 1, {nullptr, values},
                                   /*null_count=*/0, /*offset=*/1);
auto array = arrow::MakeArray(data);
auto table = arrow::Table::Make(
    arrow::schema({arrow::field("flag", arrow::boolean())}),
    {std::make_shared<arrow::ChunkedArray>(array)});

ARROW_ASSIGN_OR_RAISE(auto sink, arrow::io::BufferOutputStream::Create());
auto properties = arrow::ipc::feather::WriteProperties::DefaultsV1();
ARROW_RETURN_NOT_OK(
    arrow::ipc::feather::WriteTable(*table, sink.get(), properties));

Under ASan, the write deterministically reports a one-byte
heap-buffer-overflow read in WritePaddedWithOffset in
cpp/src/arrow/ipc/feather.cc.

The same harness with offset = 0 completes without a sanitizer error.

Expected behavior

The writer should read only the bitmap bytes required by the logical slice and
clear unused trailing bits in the serialized bitmap byte.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions