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-12403-52faedec7007a1386ce6107b3b8bb46d098ebdaa in repository https://gitbox.apache.org/repos/asf/arrow-rs.git
commit ede2aeedef33116f1696a5d3130c34b55a04e9c9 Author: mola <[email protected]> AuthorDate: Fri Nov 27 05:19:23 2026 +0000 fix(arrow-array): reject map offsets that go backwards (#12403) Closes #12395. `MapBuilder::append` trusted the child builders' lengths for the next offset. Finishing a child resets its length, so the next offset could be smaller than the last one already recorded. The finished array then had decreasing offsets, which is invalid. `append` and `append_nulls` now reject a next offset that is smaller than the last recorded one. They return an error before either buffer changes. `finish` and `finish_cloned` now check the recorded offsets before building. The offsets must be non-decreasing, and the last offset must fit inside the finished key and value arrays. A longer child is allowed, since those entries are not part of a map slot yet. ## Are these changes tested? Yes. Three new tests in `builder::map_builder::tests`. Note: #12353 touches the same `finish` code for performance, but it does not validate offsets. This fix is independent of it. AI usage disclosure: this PR was developed and tested with AI assistance (harness: Claude; model: Claude Opus 5.5). --------- Co-authored-by: Claude <[email protected]> --- arrow-array/src/builder/map_builder.rs | 125 +++++++++++++++++++++++++++++---- 1 file changed, 113 insertions(+), 12 deletions(-) diff --git a/arrow-array/src/builder/map_builder.rs b/arrow-array/src/builder/map_builder.rs index c86a17d6d6..cd5ca8b050 100644 --- a/arrow-array/src/builder/map_builder.rs +++ b/arrow-array/src/builder/map_builder.rs @@ -154,21 +154,36 @@ impl<K: ArrayBuilder, V: ArrayBuilder> MapBuilder<K, V> { /// Returns the offset of the next map slot, i.e. the current number of entries. /// - /// Returns an error if the number of entries does not fit in an `i32` offset. + /// Returns an error if the next offset would be less than the last offset + /// already recorded, or if the number of entries does not fit in an `i32` + /// offset. #[inline] fn next_offset(&self) -> Result<i32, ArrowError> { let len = self.key_builder.len(); - i32::try_from(len).map_err(|_| { + let offset = i32::try_from(len).map_err(|_| { ArrowError::InvalidArgumentError(format!( "Cannot append to a map builder with {len} entries: offset exceeds i32::MAX" )) - }) + })?; + // `keys().finish()` resets the child length, so the next offset can be + // smaller than one already recorded. Reject that before either buffer + // is updated. + if let Some(&last) = self.offsets_builder.last() + && offset < last + { + return Err(ArrowError::InvalidArgumentError(format!( + "MapBuilder reached invalid state: values truncated unexpectedly \ + (next offset {offset} is less than last offset {last})" + ))); + } + Ok(offset) } /// Finish the current map array slot /// /// Returns an error if the key and values builders are in an inconsistent state, - /// or if the number of entries exceeds `i32::MAX`. + /// the next offset would be less than the last offset, or the number of entries + /// exceeds `i32::MAX`. A rejected append leaves the builder unchanged. #[inline] pub fn append(&mut self, is_valid: bool) -> Result<(), ArrowError> { self.validate_equal_lengths()?; @@ -181,7 +196,8 @@ impl<K: ArrayBuilder, V: ArrayBuilder> MapBuilder<K, V> { /// Append `n` nulls to this [`MapBuilder`] /// /// Returns an error if the key and values builders are in an inconsistent state, - /// or if the number of entries exceeds `i32::MAX`. + /// the next offset would be less than the last offset, or the number of entries + /// exceeds `i32::MAX`. A rejected append leaves the builder unchanged. #[inline] pub fn append_nulls(&mut self, n: usize) -> Result<(), ArrowError> { self.validate_equal_lengths()?; @@ -195,9 +211,11 @@ impl<K: ArrayBuilder, V: ArrayBuilder> MapBuilder<K, V> { /// /// # Panics /// - /// Panics if the fields set with [`Self::with_keys_field`] or - /// [`Self::with_values_field`] do not match the data types of the key and value - /// builders, or if the keys contain nulls + /// Panics if: + /// - the fields set with [`Self::with_keys_field`] or [`Self::with_values_field`] + /// do not match the data types of the key and value builders + /// - the keys contain nulls + /// - the key or value builder was finished after the last append pub fn finish(&mut self) -> MapArray { // Build the keys let keys_arr = self.key_builder.finish(); @@ -213,9 +231,11 @@ impl<K: ArrayBuilder, V: ArrayBuilder> MapBuilder<K, V> { /// /// # Panics /// - /// Panics if the fields set with [`Self::with_keys_field`] or - /// [`Self::with_values_field`] do not match the data types of the key and value - /// builders, or if the keys contain nulls + /// Panics if: + /// - the fields set with [`Self::with_keys_field`] or [`Self::with_values_field`] + /// do not match the data types of the key and value builders + /// - the keys contain nulls + /// - the key or value builder was finished after the last append pub fn finish_cloned(&self) -> MapArray { // Build the keys let keys_arr = self.key_builder.finish_cloned(); @@ -248,6 +268,17 @@ impl<K: ArrayBuilder, V: ArrayBuilder> MapBuilder<K, V> { "Keys array must have no null values, found {} null value(s)", keys_arr.null_count() ); + // `append` keeps the offsets non-decreasing, but finishing a child builder + // afterwards can still leave the last offset past the end of the child. + // A longer child is allowed: those entries are not part of a map slot yet. + let last_offset = *offset_buffer.typed_data::<i32>().last().unwrap() as usize; + assert!( + last_offset <= keys_arr.len() && last_offset <= values_arr.len(), + "MapBuilder reached invalid state: values truncated unexpectedly \ + (last offset {last_offset}, keys length {}, values length {})", + keys_arr.len(), + values_arr.len() + ); let keys_field = match &self.key_field { Some(f) => { @@ -278,7 +309,9 @@ impl<K: ArrayBuilder, V: ArrayBuilder> MapBuilder<K, V> { false, // always non-nullable )); // SAFETY: offsets are appended from the entry count, which the builder - // keeps equal for keys and values, and the entries are non-nullable + // keeps equal for keys and values. `append` and `append_nulls` keep them + // non-decreasing, and the last offset was checked above to be within the + // finished key and value arrays. let offsets = unsafe { OffsetBuffer::new_unchecked(offset_buffer.into()) }; unsafe { MapArray::new_unchecked(map_field, offsets, struct_array, nulls, false) } } @@ -624,4 +657,72 @@ mod tests { assert_eq!(builder.offsets_builder, vec![0]); assert_eq!(builder.len(), 0); } + + #[test] + fn test_append_rejects_decreasing_offset_after_child_finish() { + let mut builder = MapBuilder::new(None, StringBuilder::new(), Int32Builder::new()); + builder.keys().append_value("1"); + builder.values().append_value(42); + builder.append(true).unwrap(); + + let offsets_before = builder.offsets_builder.clone(); + let len_before = builder.len(); + builder.keys().finish(); + builder.values().finish(); + + let err = builder.append(true).unwrap_err(); + assert!( + matches!(err, ArrowError::InvalidArgumentError(_)), + "expected InvalidArgumentError, got {err}" + ); + assert!( + err.to_string().contains("values truncated unexpectedly"), + "unexpected error: {err}" + ); + assert_eq!(builder.offsets_builder, offsets_before); + assert_eq!(builder.offsets_builder, vec![0, 1]); + assert_eq!(builder.len(), len_before); + + let mut nulls = MapBuilder::new(None, StringBuilder::new(), Int32Builder::new()); + nulls.keys().append_value("1"); + nulls.values().append_value(42); + nulls.append(true).unwrap(); + nulls.keys().finish(); + nulls.values().finish(); + let offsets_before = nulls.offsets_builder.clone(); + let err = nulls.append_nulls(2).unwrap_err(); + assert!( + matches!(err, ArrowError::InvalidArgumentError(_)), + "expected InvalidArgumentError, got {err}" + ); + assert_eq!(nulls.offsets_builder, offsets_before); + assert_eq!(nulls.len(), 1); + } + + #[test] + #[should_panic(expected = "values truncated unexpectedly")] + fn test_finish_panics_when_child_was_finished_after_append() { + let mut builder = MapBuilder::new(None, StringBuilder::new(), Int32Builder::new()); + builder.keys().append_value("1"); + builder.values().append_value(42); + builder.append(true).unwrap(); + builder.keys().finish(); + builder.values().finish(); + let _array = builder.finish(); + } + + #[test] + fn test_finish_allows_uncommitted_trailing_entries() { + let mut builder = MapBuilder::new(None, StringBuilder::new(), Int32Builder::new()); + builder.keys().append_value("1"); + builder.values().append_value(42); + builder.append(true).unwrap(); + builder.keys().append_value("trailing"); + builder.values().append_value(7); + + let map = builder.finish(); + assert_eq!(map.value_offsets(), &[0, 1]); + assert_eq!(map.keys().len(), 2); + assert_eq!(map.values().len(), 2); + } }
