scovich commented on code in PR #7987:
URL: https://github.com/apache/arrow-rs/pull/7987#discussion_r2232860635


##########
parquet-variant/src/builder.rs:
##########
@@ -1216,24 +1211,45 @@ impl<'a> ListBuilder<'a> {
 
     /// Finalizes this list and appends it to its parent, which otherwise 
remains unmodified.
     pub fn finish(mut self) {
-        let data_size = self.buffer.offset();
+        let buffer = self.parent_state.buffer();
+
+        let data_size = buffer.offset() - self.parent_value_offset_base;
+
         let num_elements = self.offsets.len();
         let is_large = num_elements > u8::MAX as usize;
         let offset_size = int_size(data_size);
 
-        // Get parent's buffer
-        let parent_buffer = self.parent_state.buffer();
-        let starting_offset = parent_buffer.offset();
+        let starting_offset = self.parent_value_offset_base;
+
+        let header_size = 1 +      // header
+            if is_large { 4 } else { 1 } +  // is_large
+            (self.offsets.len() + 1) * offset_size as usize; // offsets and 
data size
 
+        // Calculated header size becomes a hint; being wrong only risks extra 
allocations.
+        // Make sure to reserve enough capacity to handle the extra bytes 
we'll truncate.

Review Comment:
   > When `header_size` will be incorrect?
   
   The size if calculated separately, and then the actual bytes are appended. 
That opens up a bug surface -- any time the two disagree, `header_size` will be 
wrong. If the code directly relied on the size being correct, e.g. because we 
allocate that many bytes and then index them, we could have produce a bad 
variant value (either because there's an extra run of inserted bytes, or 
because of a buffer overflow while indexing. But because the calculated size is 
only a capacity hint for the vec, the cost of being wrong is very low.



-- 
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: github-unsubscr...@arrow.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to