Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ARROW-10448: [Rust] Remove PrimitiveArray::new that can cause UB #8560

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 4 additions & 34 deletions rust/arrow/src/array/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,16 +412,6 @@ pub struct PrimitiveArray<T: ArrowPrimitiveType> {
}

impl<T: ArrowPrimitiveType> PrimitiveArray<T> {
pub fn new(length: usize, values: Buffer, null_count: usize, offset: usize) -> Self {
let array_data = ArrayData::builder(T::DATA_TYPE)
.len(length)
.add_buffer(values)
.null_count(null_count)
.offset(offset)
.build();
PrimitiveArray::from(array_data)
}

/// Returns the length of this array.
pub fn len(&self) -> usize {
self.data.len()
Expand Down Expand Up @@ -2423,10 +2413,9 @@ mod tests {
#[test]
fn test_primitive_array_from_vec() {
let buf = Buffer::from(&[0, 1, 2, 3, 4].to_byte_slice());
let buf2 = buf.clone();
let arr = Int32Array::new(5, buf, 0, 0);
let arr = Int32Array::from(vec![0, 1, 2, 3, 4]);
let slice = unsafe { std::slice::from_raw_parts(arr.raw_values(), 5) };
assert_eq!(buf2, arr.values());
assert_eq!(buf, arr.values());
assert_eq!(&[0, 1, 2, 3, 4], slice);
assert_eq!(5, arr.len());
assert_eq!(0, arr.offset());
Expand Down Expand Up @@ -2716,8 +2705,7 @@ mod tests {

#[test]
fn test_int32_fmt_debug() {
let buf = Buffer::from(&[0, 1, 2, 3, 4].to_byte_slice());
let arr = Int32Array::new(5, buf, 0, 0);
let arr = Int32Array::from(vec![0, 1, 2, 3, 4]);
assert_eq!(
"PrimitiveArray<Int32>\n[\n 0,\n 1,\n 2,\n 3,\n 4,\n]",
format!("{:?}", arr)
Expand Down Expand Up @@ -2757,8 +2745,7 @@ mod tests {

#[test]
fn test_boolean_fmt_debug() {
let buf = Buffer::from(&[true, false, false].to_byte_slice());
let arr = BooleanArray::new(3, buf, 0, 0);
let arr = BooleanArray::from(vec![true, false, false]);
assert_eq!(
"PrimitiveArray<Boolean>\n[\n true,\n false,\n false,\n]",
format!("{:?}", arr)
Expand Down Expand Up @@ -2833,23 +2820,6 @@ mod tests {
Int32Array::from(data);
}

#[test]
fn test_boolean_array_new() {
// 00000010 01001000
let buf = Buffer::from([72_u8, 2_u8]);
let buf2 = buf.clone();
let arr = BooleanArray::new(10, buf, 0, 0);
assert_eq!(buf2, arr.values());
assert_eq!(10, arr.len());
assert_eq!(0, arr.offset());
assert_eq!(0, arr.null_count());
for i in 0..10 {
assert!(!arr.is_null(i));
assert!(arr.is_valid(i));
assert_eq!(i == 3 || i == 6 || i == 9, arr.value(i), "failed at {}", i)
}
}

#[test]
fn test_boolean_array_from_vec() {
let buf = Buffer::from([10_u8]);
Expand Down