Jefffrey commented on code in PR #11044:
URL: https://github.com/apache/arrow-rs/pull/11044#discussion_r4034660901


##########
arrow-ipc/tests/invalid_validity.rs:
##########
@@ -0,0 +1,105 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use std::collections::HashMap;
+use std::io::Cursor;
+use std::sync::Arc;
+
+use arrow_buffer::Buffer;
+use arrow_ipc::reader::{StreamReader, read_record_batch};
+use arrow_ipc::writer::StreamWriter;
+use arrow_ipc::{MetadataVersion, root_as_message};
+use arrow_schema::{DataType, Field, Schema, SchemaRef};
+
+// Encode malformed IPC directly, without constructing an invalid Arrow array.

Review Comment:
   is it necessary to encode the test this way? perhaps we can just keep the 
test from the original issue which is much more succinct



##########
arrow-data/src/data.rs:
##########
@@ -2954,6 +2953,72 @@ mod tests {
         );
     }
 
+    #[test]
+    fn test_builder_rejects_short_null_bit_buffer() {
+        for (len, offset, bytes) in [(8000, 0, 1), (1, 0, 0), (8, 1, 1), (0, 
9, 1)] {
+            for null_count in [None, Some(0), Some(len)] {
+                let mut builder = ArrayData::builder(DataType::Int32)
+                    .len(len)
+                    .offset(offset)
+                    .add_buffer(make_i32_buffer(len + offset))
+                    .null_bit_buffer(Some(Buffer::from(vec![0_u8; bytes])));
+                if let Some(null_count) = null_count {
+                    builder = builder.null_count(null_count);
+                }
+                let err = builder.build().unwrap_err();
+                assert_eq!(
+                    err.to_string(),
+                    format!(
+                        "Invalid argument error: null_bit_buffer size too 
small. got {bytes} needed {}",
+                        bit_util::ceil(len + offset, 8)
+                    )
+                );
+            }
+        }
+    }
+
+    #[test]
+    fn test_builder_null_bit_buffer_length_overflow() {
+        let err = ArrayData::builder(DataType::Int32)
+            .len(usize::MAX)
+            .offset(1)
+            .null_bit_buffer(Some(Buffer::default()))
+            .build()
+            .unwrap_err();
+        assert_eq!(
+            err.to_string(),
+            format!(
+                "Invalid argument error: Length {} with offset 1 overflows 
usize for Int32",
+                usize::MAX
+            )
+        );
+    }
+
+    #[test]
+    fn test_builder_accepts_valid_null_bit_buffer() {
+        for (len, offset) in [(0, 0), (0, 8), (7, 1), (8, 0), (9, 0)] {
+            for valid in [false, true] {
+                let bytes = bit_util::ceil(len + offset, 8);
+                let buffer = Buffer::from(vec![if valid { 255_u8 } else { 0 }; 
bytes]);
+                let expected_nulls = if valid { 0 } else { len };
+                for null_count in [None, Some(expected_nulls)] {
+                    let mut builder = ArrayData::builder(DataType::Int32)
+                        .len(len)
+                        .offset(offset)
+                        .add_buffer(make_i32_buffer(len + offset))

Review Comment:
   some of this feels excessive for the change being tested here; for example, 
is parameterizing on the null count required considering the paths being 
changed in this PR dont touch it? similarly for having a valid & non-valid 
toggle



##########
arrow-data/src/data.rs:
##########
@@ -2954,6 +2953,72 @@ mod tests {
         );
     }
 
+    #[test]
+    fn test_builder_rejects_short_null_bit_buffer() {
+        for (len, offset, bytes) in [(8000, 0, 1), (1, 0, 0), (8, 1, 1), (0, 
9, 1)] {
+            for null_count in [None, Some(0), Some(len)] {
+                let mut builder = ArrayData::builder(DataType::Int32)
+                    .len(len)
+                    .offset(offset)

Review Comment:
   similarly here; i feel we dont need the null_count parameter, and would only 
need perhaps 2 cases max at the top level?



-- 
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]

Reply via email to