diff --git a/arrow-array/src/builder/fixed_size_binary_builder.rs b/arrow-array/src/builder/fixed_size_binary_builder.rs index 70f0dde26d8e..83dace534a57 100644 --- a/arrow-array/src/builder/fixed_size_binary_builder.rs +++ b/arrow-array/src/builder/fixed_size_binary_builder.rs @@ -65,7 +65,7 @@ impl FixedSizeBinaryBuilder { /// distinct value element. #[inline] pub fn append_value(&mut self, value: impl AsRef<[u8]>) -> Result<(), ArrowError> { - if self.value_length != value.as_ref().len() as i32 { + if value.as_ref().len() != self.value_length as usize { Err(ArrowError::InvalidArgumentError( "Byte slice does not have the same length as FixedSizeBinaryBuilder value lengths" .to_string(), @@ -146,7 +146,7 @@ impl FixedSizeBinaryBuilder { } /// Returns the total memory capacity in bytes currently pub fn capacity(&self) -> usize { - self.values_builder.capacity() + self.null_buffer_builder.allocated_size() + 4 // i32 + self.values_builder.capacity() + self.null_buffer_builder.allocated_size() + 4 // i32 } } @@ -210,6 +210,16 @@ mod tests { assert_eq!(b"world", array.value(5)); } + #[test] + #[cfg(target_pointer_width = "64")] + #[ignore = "allocates >4GB to test 32-bit length truncation"] + fn test_fixed_size_binary_builder_rejects_wrapped_length() { + let mut b = FixedSizeBinaryBuilder::new(1); + let big = vec![0u8; (1usize << 32) + 1]; + let err = b.append_value(&big).unwrap_err(); + assert!(matches!(err, ArrowError::InvalidArgumentError(_))); + } + #[test] fn test_fixed_size_binary_builder_finish_cloned() { let mut builder = FixedSizeBinaryBuilder::with_capacity(3, 5); diff --git a/arrow-array/src/builder/fixed_size_binary_dictionary_builder.rs b/arrow-array/src/builder/fixed_size_binary_dictionary_builder.rs index 5ee8b05a0ed8..0c12c09a78af 100644 --- a/arrow-array/src/builder/fixed_size_binary_dictionary_builder.rs +++ b/arrow-array/src/builder/fixed_size_binary_dictionary_builder.rs @@ -234,7 +234,7 @@ where /// /// Returns an error if the new index would overflow the key type. pub fn append(&mut self, value: impl AsRef<[u8]>) -> Result { - if self.byte_width != value.as_ref().len() as i32 { + if value.as_ref().len() != self.byte_width as usize { Err(ArrowError::InvalidArgumentError(format!( "Invalid input length passed to FixedSizeBinaryBuilder. Expected {} got {}", self.byte_width, @@ -256,7 +256,7 @@ where value: impl AsRef<[u8]>, count: usize, ) -> Result { - if self.byte_width != value.as_ref().len() as i32 { + if value.as_ref().len() != self.byte_width as usize { Err(ArrowError::InvalidArgumentError(format!( "Invalid input length passed to FixedSizeBinaryBuilder. Expected {} got {}", self.byte_width,