This is an automated email from the ASF dual-hosted git repository. github-merge-queue[bot] pushed a commit to branch gh-readonly-queue/main/pr-12292-6747ecda382b135ba0fb08868dd859a47c0304ad in repository https://gitbox.apache.org/repos/asf/arrow-rs.git
commit 67f96ac9f0869f4a6f44698be1574dd585793a33 Author: mola <[email protected]> AuthorDate: Mon Nov 23 12:53:50 2026 +0000 fix(arrow-buffer): add new `force_validate` feature to validate offsets in `OffsetBuffer::new_unchecked` (#12292) Closes #12284. `OffsetBuffer::new_unchecked` never validated its input because `arrow-buffer` had no `force_validate` feature, so decreasing offsets could reach an array unchecked even in the CI job that runs with `force_validate` (`GenericByteArray::try_new` trusts the `OffsetBuffer` type invariant). This adds `force_validate = []` to `arrow-buffer`, wires `arrow-data`'s `force_validate` to enable `arrow-buffer/force_validate`, and makes `OffsetBuffer::new_unchecked` delegate to `Self::new` when the feature is on (same pattern as `GenericByteArray::new_unchecked`). `arrow`'s `force_validate` now also enables `arrow-buffer/force_validate` explicitly. The same gap existed in two more unchecked constructors. `NullBuffer::new_unchecked` now asserts the given `null_count` matches the buffer's unset-bit count under `force_validate`. `RunEndBuffer::new_unchecked` now delegates to `Self::new` under `force_validate`. ## Are these changes tested? Yes. Three regression tests, all `#[should_panic]` and gated on `force_validate`: `buffer::offset::tests::non_monotonic_offsets_unchecked_force_validate`, `buffer::null::tests::new_unchecked_wrong_null_count_force_validate`, and `buffer::run::tests::new_unchecked_non_increasing_force_validate`. Each fails before the fix ("test did not panic as expected") and passes after. The issue's end-to-end scenario (BinaryArray/StringArray built from offsets `[0, 2, 1, 3]` via `new_unchecked`) now panics with "offsets must be monotonically increasing" under `cargo test -p arrow-array --features force_validate`. Before the fix: the new tests fail (do not panic as expected) <img width="935" height="70" alt="arrow-rs-12284-verify-before" src="https://github.com/user-attachments/assets/72c8c5a2-9400-4056-8b32-5ebee8308b59" /> After the fix: the new tests pass, and all suites pass with 0 failures <img width="899" height="343" alt="arrow-rs-12284-verify-after" src="https://github.com/user-attachments/assets/05047ced-1a32-482a-a745-a3e3f699fba8" /> AI usage disclosure: this PR was developed and testedwith AI assistance (harness: Muse; model: Muse Spark; effort: issue triage, implementation, and verification).With human reviewed**qwq** --------- Co-authored-by: Mola-maker <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]> --- arrow-buffer/Cargo.toml | 4 ++++ arrow-buffer/src/buffer/null.rs | 12 ++++++++++++ arrow-buffer/src/buffer/offset.rs | 10 ++++++++++ arrow-buffer/src/buffer/run.rs | 10 ++++++++++ arrow-data/Cargo.toml | 2 +- arrow/Cargo.toml | 2 +- 6 files changed, 38 insertions(+), 2 deletions(-) diff --git a/arrow-buffer/Cargo.toml b/arrow-buffer/Cargo.toml index 3fcea6799d..baa65a7bdb 100644 --- a/arrow-buffer/Cargo.toml +++ b/arrow-buffer/Cargo.toml @@ -24,6 +24,10 @@ all-features = true [features] # Enable memory tracking support pool = [] +# force_validate runs full data validation for all arrays that are created +# this is not enabled by default as it is too computationally expensive +# but is run as part of our CI checks +force_validate = [] [dependencies] bytes = { version = "1.4" } diff --git a/arrow-buffer/src/buffer/null.rs b/arrow-buffer/src/buffer/null.rs index 48f385f0e8..0af63c1d55 100644 --- a/arrow-buffer/src/buffer/null.rs +++ b/arrow-buffer/src/buffer/null.rs @@ -56,6 +56,10 @@ impl NullBuffer { /// /// `buffer` must contain `null_count` `0` bits pub unsafe fn new_unchecked(buffer: BooleanBuffer, null_count: usize) -> Self { + if cfg!(feature = "force_validate") { + let expected = buffer.len() - buffer.count_set_bits(); + assert_eq!(null_count, expected, "null_count does not match buffer"); + } Self { buffer, null_count } } @@ -540,4 +544,12 @@ mod tests { assert!((2 * count..3 * count).all(|i| expanded.is_valid(i))); } } + + #[test] + #[should_panic(expected = "null_count does not match buffer")] + #[cfg(feature = "force_validate")] + fn new_unchecked_wrong_null_count_force_validate() { + let buffer = BooleanBuffer::from(vec![true, false, true]); + unsafe { NullBuffer::new_unchecked(buffer, 0) }; + } } diff --git a/arrow-buffer/src/buffer/offset.rs b/arrow-buffer/src/buffer/offset.rs index 8ef9f7755e..c873c196ad 100644 --- a/arrow-buffer/src/buffer/offset.rs +++ b/arrow-buffer/src/buffer/offset.rs @@ -72,6 +72,9 @@ impl<O: ArrowNativeType> OffsetBuffer<O> { /// `buffer` must be a non-empty buffer containing monotonically increasing /// values greater than or equal to zero pub unsafe fn new_unchecked(buffer: ScalarBuffer<O>) -> Self { + if cfg!(feature = "force_validate") { + return Self::new(buffer); + } Self(buffer) } @@ -623,6 +626,13 @@ mod tests { OffsetBuffer::new(vec![1, 2, 0].into()); } + #[test] + #[should_panic(expected = "offsets must be monotonically increasing")] + #[cfg(feature = "force_validate")] + fn non_monotonic_offsets_unchecked_force_validate() { + unsafe { OffsetBuffer::<i32>::new_unchecked(vec![0, 2, 1, 3].into()) }; + } + #[test] fn from_lengths() { let buffer = OffsetBuffer::<i32>::from_lengths([2, 6, 3, 7, 2]); diff --git a/arrow-buffer/src/buffer/run.rs b/arrow-buffer/src/buffer/run.rs index f33a3a2d62..e77f7efaba 100644 --- a/arrow-buffer/src/buffer/run.rs +++ b/arrow-buffer/src/buffer/run.rs @@ -146,6 +146,9 @@ where logical_offset: usize, logical_length: usize, ) -> Self { + if cfg!(feature = "force_validate") { + return Self::new(run_ends, logical_offset, logical_length); + } Self { run_ends, logical_length, @@ -416,4 +419,11 @@ mod tests { let sliced_values: Vec<i32> = sliced.sliced_values().collect(); assert_eq!(sliced_values, &[2]); } + + #[test] + #[should_panic(expected = "run-ends not strictly increasing")] + #[cfg(feature = "force_validate")] + fn new_unchecked_non_increasing_force_validate() { + unsafe { RunEndBuffer::<i32>::new_unchecked(vec![1, 3, 2].into(), 0, 2) }; + } } diff --git a/arrow-data/Cargo.toml b/arrow-data/Cargo.toml index 4b9e2eb230..05909b6c27 100644 --- a/arrow-data/Cargo.toml +++ b/arrow-data/Cargo.toml @@ -22,7 +22,7 @@ bench = false # force_validate runs full data validation for all arrays that are created # this is not enabled by default as it is too computationally expensive # but is run as part of our CI checks -force_validate = [] +force_validate = ["arrow-buffer/force_validate"] # Enable ffi support ffi = ["arrow-schema/ffi"] # Enable memory tracking support diff --git a/arrow/Cargo.toml b/arrow/Cargo.toml index 92a9d9e10c..6677f6601b 100644 --- a/arrow/Cargo.toml +++ b/arrow/Cargo.toml @@ -66,7 +66,7 @@ pyarrow-experimental-inspect = ["pyarrow", "arrow-pyarrow/experimental-inspect"] # force_validate runs full data validation for all arrays that are created # this is not enabled by default as it is too computationally expensive # but is run as part of our CI checks -force_validate = ["arrow-array/force_validate", "arrow-data/force_validate"] +force_validate = ["arrow-array/force_validate", "arrow-buffer/force_validate", "arrow-data/force_validate"] # Enable ffi support ffi = ["arrow-schema/ffi", "arrow-data/ffi", "arrow-array/ffi"] chrono-tz = ["arrow-array/chrono-tz"]
