mbutrovich opened a new issue, #12284: URL: https://github.com/apache/arrow-rs/issues/12284
#### Describe the bug `force_validate` "runs full data validation for all arrays that are created" ([arrow-data/Cargo.toml](https://github.com/apache/arrow-rs/blob/d65329d3e71f4276ce41c8675446d178cecfc1fa/arrow-data/Cargo.toml?plain=1#L22-L25)). The unchecked array constructors honor it. For example, `GenericByteArray::new_unchecked` calls `Self::new` under the feature ([byte_array.rs](https://github.com/apache/arrow-rs/blob/d65329d3e71f4276ce41c8675446d178cecfc1fa/arrow-array/src/array/byte_array.rs#L144-L156)). `OffsetBuffer::new_unchecked` doesn't, because `arrow-buffer` has no `force_validate` feature ([offset.rs](https://github.com/apache/arrow-rs/blob/d65329d3e71f4276ce41c8675446d178cecfc1fa/arrow-buffer/src/buffer/offset.rs#L68-L76)). `GenericByteArray::try_new` then trusts the `OffsetBuffer` type invariant and doesn't check ordering ([types.rs](https://github.com/apache/arrow-rs/blob/d65329d3e71f4276ce41c8675446d178cecfc1fa/arrow-array/src/types.rs#L1732-L1741)). So offsets that decrease reach an array unchecked, even in the CI job that runs with `force_validate`. #### To Reproduce This test passes with `cargo test -p arrow-array --features force_validate`: ```rust use arrow_array::{Array, BinaryArray, StringArray}; use arrow_buffer::{Buffer, OffsetBuffer, ScalarBuffer}; #[test] fn non_monotonic_offsets_under_force_validate() { let offsets = || unsafe { OffsetBuffer::new_unchecked(ScalarBuffer::from(vec![0i32, 2, 1, 3])) }; let values = || Buffer::from(b"abc".as_slice()); let b = unsafe { BinaryArray::new_unchecked(offsets(), values(), None) }; let s = unsafe { StringArray::new_unchecked(offsets(), values(), None) }; assert_eq!((b.len(), s.len()), (3, 3)); } ``` #### Expected behavior Under `force_validate`, `OffsetBuffer::new_unchecked` should panic on offsets that break its `# Safety` contract, as `OffsetBuffer::new` does ([offset.rs](https://github.com/apache/arrow-rs/blob/d65329d3e71f4276ce41c8675446d178cecfc1fa/arrow-buffer/src/buffer/offset.rs#L49-L66)). Could we add `force_validate = []` to `arrow-buffer`, have `arrow-data`'s `force_validate` enable `arrow-buffer/force_validate`, and start `OffsetBuffer::new_unchecked` with `if cfg!(feature = "force_validate") { return Self::new(buffer); }`? `arrow-array` and `arrow` both enable `arrow-data/force_validate`, so no other manifest needs to change. A new cargo feature is an additive change. #### Additional context Code that used to build arrays through `ArrayDataBuilder::build_unchecked` got this check from `validate_each_offset` under `force_validate` ([data.rs](https://github.com/apache/arrow-rs/blob/d65329d3e71f4276ce41c8675446d178cecfc1fa/arrow-data/src/data.rs#L1776-L1790)). #12228 moves `GenericByteBuilder::finish` off that path. Code that builds an `OffsetBuffer` directly never had the check. That includes `GenericListBuilder::finish` ([generic_list_builder.rs](https://github.com/apache/arrow-rs/blob/d65329d3e71f4276ce41c8675446d178cecfc1fa/arrow-array/src/builder/generic_list_builder.rs#L300)) and the `take`, `filter`, `zip`, and `interleave` kernels in `arrow-select`. ### Ancillary: draft issue for `FixedSizeBinaryBuilder::append_value` This predates the PR and the PR doesn't touch it. `gh issue list --state all` found nothing for "FixedSizeBinaryBuilder append_value", "FixedSizeBinaryBuilder overflow", or "fixed size binary builder as i32". I didn't run the reproduction, because it allocates about 8 GiB. The behavior below is reasoned from the code. Use the Bug report template. Pick the "Willingness to contribute" answer yourself. Title: `FixedSizeBinaryBuilder::append_value` accepts values whose length differs from `value_length` by a multiple of 2^32 #### Describe the bug `FixedSizeBinaryBuilder::append_value` compares `self.value_length != value.as_ref().len() as i32` ([fixed_size_binary_builder.rs](https://github.com/apache/arrow-rs/blob/d65329d3e71f4276ce41c8675446d178cecfc1fa/arrow-array/src/builder/fixed_size_binary_builder.rs#L66-L77)). The `as i32` cast truncates, so a slice whose length is `value_length + k * 2^32` passes the check. The builder then appends every byte of it but counts one value. Later values read from the wrong position, and `value_data()` is longer than `len * value_length`, which violates the `FixedSizeBinaryArray::new_unchecked` contract that `finish` relies on. With `force_validate`, `finish` panics instead. #### To Reproduce This needs about 8 GiB of memory. ```rust let mut b = FixedSizeBinaryBuilder::new(1); let big = vec![0u8; (1usize << 32) + 1]; assert!(b.append_value(&big).is_err()); // currently Ok ``` #### Expected behavior `append_value` should return an error for any slice whose length isn't `value_length`. Comparing as `usize` fixes it: `value.as_ref().len() != self.value_length as usize`. `with_capacity` already asserts `value_length >= 0`, so the cast is lossless. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
