jhorstmann commented on code in PR #9971:
URL: https://github.com/apache/arrow-rs/pull/9971#discussion_r3247152351
##########
arrow-buffer/src/buffer/immutable.rs:
##########
@@ -84,6 +86,89 @@ pub struct Buffer {
length: usize,
}
+/// An aligned byte buffer that can be filled through `Read::read_exact` and
+/// converted into [`Buffer`] without copying.
+///
+/// This is useful for readers that need Arrow buffer alignment without
+/// first zero-initializing the allocation.
+pub struct AlignedVec {
+ ptr: NonNull<u8>,
+ len: usize,
+ layout: Layout,
+}
+
+impl AlignedVec {
+ /// Allocates `len` bytes with the requested alignment.
+ pub fn new(len: usize, align: usize) -> Self {
+ let layout =
+ Layout::from_size_align(len, align).expect("failed to create
layout for AlignedVec");
+
+ let ptr = match layout.size() {
+ 0 => dangling_ptr(),
+ _ => {
+ // Safety: `layout` has non-zero size and was constructed
above.
+ let raw_ptr = unsafe { std::alloc::alloc(layout) };
+ NonNull::new(raw_ptr).unwrap_or_else(||
handle_alloc_error(layout))
+ }
+ };
+
+ Self { ptr, len, layout }
+ }
+}
+
+// Allows callers such as `Read::read_exact` to view the allocated region as
+// bytes after it has been filled.
+impl Deref for AlignedVec {
+ type Target = [u8];
+
+ fn deref(&self) -> &[u8] {
+ // Safety: `ptr` points to `len` bytes owned by this AlignedVec.
+ unsafe { std::slice::from_raw_parts(self.ptr.as_ptr(), self.len) }
Review Comment:
This looks like it could expose uninitialized data from safe code. I think
this implementation has the same soundness issue as the one using
`Vec::with_capactiy` and `Vec::set_len`.
--
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]