jhorstmann commented on code in PR #9778:
URL: https://github.com/apache/arrow-rs/pull/9778#discussion_r3117527386


##########
arrow-ipc/src/reader.rs:
##########
@@ -875,8 +875,12 @@ fn read_block<R: Read + Seek>(mut reader: R, block: 
&Block) -> Result<Buffer, Ar
     let metadata_len = block.metaDataLength().to_usize().unwrap();
     let total_len = body_len.checked_add(metadata_len).unwrap();
 
-    let mut buf = MutableBuffer::from_len_zeroed(total_len);
-    reader.read_exact(&mut buf)?;
+    let mut buf = MutableBuffer::with_capacity(total_len);
+    // Buffer is immediately fully initialized by `read_exact` before any read 
occurs

Review Comment:
   Strictly speaking, this is not sound for arbitrary `Read` implementations. 
Unfortunately, nothing would prevent a `Read`  impl from reading the 
uninitialized bytes in `buf`, causing undefined behavior. For a known 
implementation like `File` this pattern could be fine though.
   
   An alternative could be to combine `take` and `read_to_end` to read into a 
`Vec`, but note that this changes the alignment of the resulting `Buffer`:
   
   ```rust
       let mut buf = Vec::with_capacity(total_len);
       reader.take(total_len as u64).read_to_end(&mut buf)?;
       if buf.len() != total_len {
           return Err(...)
       }
       Ok(buf.into())
   ```



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