This is an automated email from the ASF dual-hosted git repository.
paleolimbot pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-nanoarrow.git
The following commit(s) were added to refs/heads/main by this push:
new ee22361f fix: Zero the padding of footer Blocks (#986)
ee22361f is described below
commit ee22361fa22581e9b8e26cc0c0a37134ffdcfba2
Author: Pedro Z <[email protected]>
AuthorDate: Wed Nov 11 17:59:11 2026 +0100
fix: Zero the padding of footer Blocks (#986)
`ArrowIpcEncoderEncodeFooter()` builds each `Block` of the footer on the
stack with an initializer, then copies it into the flatbuffer vector.
`Block` has four bytes of padding after `metaDataLength`, and an
initializer doesn't set padding. So the footer carried whatever bytes
were on the stack. That makes files non-reproducible, and the stack
bytes end up in the file. With Apple clang 17 at `-O0`, filling the
stack with `0xAB` before the call gives padding `abababab`.
The vector is now zeroed after `extend`, and each element is set with
the generated `Block_assign()`. Both the record batch and the dictionary
block loops are changed.
The new `NanoarrowIpcFooterEncodingIsDeterministic` test encodes the
same footer twice, after filling the stack with `0x00` and then `0xFF`,
and requires identical bytes. In a plain Debug build it fails without
the fix and passes with it. Under ASan it passes either way, because
ASan moves stack frames.
Found through a byte-for-byte determinism test in the
[zuarrow](https://github.com/pedrobtz/zuarrow) R package, which vendors
nanoarrow.
`ctest` passes in a plain Debug build and with ASan and UBSan (410 tests
each). The changed files are formatted with the pinned clang-format
16.0.6.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---------
Co-authored-by: Claude Opus 5.5 <[email protected]>
Co-authored-by: Dewey Dunnington <[email protected]>
---
src/nanoarrow/ipc/encoder.c | 22 ++++++++++------------
1 file changed, 10 insertions(+), 12 deletions(-)
diff --git a/src/nanoarrow/ipc/encoder.c b/src/nanoarrow/ipc/encoder.c
index 1a7c5375..5b78f315 100644
--- a/src/nanoarrow/ipc/encoder.c
+++ b/src/nanoarrow/ipc/encoder.c
@@ -1234,13 +1234,12 @@ ArrowErrorCode ArrowIpcEncoderEncodeFooter(struct
ArrowIpcEncoder* encoder,
struct ns(Block)* flatcc_RecordBatch_blocks =
ns(Footer_recordBatches_extend(builder, _ArrowInt64ToSizeT(n_blocks)));
FLATCC_RETURN_IF_NULL(flatcc_RecordBatch_blocks, error);
+ // Zero the vector first so that the padding after metaDataLength is
+ // deterministic, then set each field
+ memset(flatcc_RecordBatch_blocks, 0, (size_t)n_blocks * sizeof(struct
ns(Block)));
for (int64_t i = 0; i < n_blocks; i++) {
- struct ns(Block) block = {
- blocks[i].offset,
- blocks[i].metadata_length,
- blocks[i].body_length,
- };
- flatcc_RecordBatch_blocks[i] = block;
+ ns(Block_assign)(&flatcc_RecordBatch_blocks[i], blocks[i].offset,
+ blocks[i].metadata_length, blocks[i].body_length);
}
FLATCC_RETURN_UNLESS_0(Footer_recordBatches_end(builder), error);
@@ -1253,13 +1252,12 @@ ArrowErrorCode ArrowIpcEncoderEncodeFooter(struct
ArrowIpcEncoder* encoder,
struct ns(Block)* flatcc_dict_blocks =
ns(Footer_dictionaries_extend(builder,
_ArrowInt64ToSizeT(n_dict_blocks)));
FLATCC_RETURN_IF_NULL(flatcc_dict_blocks, error);
+ // Zero the vector first so that the padding after metaDataLength is
+ // deterministic, then set each field
+ memset(flatcc_dict_blocks, 0, (size_t)n_dict_blocks * sizeof(struct
ns(Block)));
for (int64_t i = 0; i < n_dict_blocks; i++) {
- struct ns(Block) block = {
- dict_blocks[i].offset,
- dict_blocks[i].metadata_length,
- dict_blocks[i].body_length,
- };
- flatcc_dict_blocks[i] = block;
+ ns(Block_assign)(&flatcc_dict_blocks[i], dict_blocks[i].offset,
+ dict_blocks[i].metadata_length,
dict_blocks[i].body_length);
}
FLATCC_RETURN_UNLESS_0(Footer_dictionaries_end(builder), error);