[ 
https://issues.apache.org/jira/browse/ARROW-2382?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16425590#comment-16425590
 ] 

ASF GitHub Bot commented on ARROW-2382:
---------------------------------------

xhochy closed pull request #1827: ARROW-2382: [Rust] Bug fix: List was not 
using aligned mem
URL: https://github.com/apache/arrow/pull/1827
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/rust/src/buffer.rs b/rust/src/buffer.rs
index 72b2a27d8..6c4f1c7fd 100644
--- a/rust/src/buffer.rs
+++ b/rust/src/buffer.rs
@@ -15,6 +15,7 @@
 // specific language governing permissions and limitations
 // under the License.
 
+use bytes::Bytes;
 use libc;
 use std::mem;
 use std::slice;
@@ -27,10 +28,6 @@ pub struct Buffer<T> {
 }
 
 impl<T> Buffer<T> {
-    pub fn new(data: *const T, len: i32) -> Self {
-        Buffer { data, len }
-    }
-
     pub fn len(&self) -> i32 {
         self.len
     }
@@ -40,6 +37,9 @@ impl<T> Buffer<T> {
     }
 
     pub fn slice(&self, start: usize, end: usize) -> &[T] {
+        assert!(start <= end);
+        assert!(start < self.len as usize);
+        assert!(end <= self.len as usize);
         unsafe { slice::from_raw_parts(self.data.offset(start as isize), (end 
- start) as usize) }
     }
 
@@ -128,6 +128,27 @@ array_from_primitive!(i16);
 array_from_primitive!(i32);
 array_from_primitive!(i64);
 
+impl From<Bytes> for Buffer<u8> {
+    fn from(bytes: Bytes) -> Self {
+        // allocate aligned
+        let len = bytes.len();
+        let sz = mem::size_of::<u8>();
+        let buf_mem = allocate_aligned((len * sz) as i64).unwrap();
+        Buffer {
+            len: len as i32,
+            data: unsafe {
+                let dst = mem::transmute::<*const u8, *mut 
libc::c_void>(buf_mem);
+                libc::memcpy(
+                    dst,
+                    mem::transmute::<*const u8, *const 
libc::c_void>(bytes.as_ptr()),
+                    len * sz,
+                );
+                mem::transmute::<*mut libc::c_void, *const u8>(dst)
+            },
+        }
+    }
+}
+
 #[cfg(test)]
 mod tests {
     use super::*;
diff --git a/rust/src/list.rs b/rust/src/list.rs
index aee8c763a..461f8e645 100644
--- a/rust/src/list.rs
+++ b/rust/src/list.rs
@@ -47,11 +47,8 @@ impl From<Vec<String>> for List<u8> {
             buf.put(s.as_bytes());
             offsets.push(buf.len() as i32);
         });
-        let bytes = buf.freeze();
-        let buffer = Buffer::new(bytes.as_ptr(), bytes.len() as i32);
-
         List {
-            data: buffer,
+            data: Buffer::from(buf.freeze()),
             offsets: Buffer::from(offsets),
         }
     }


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


> [Rust] List<T> was not using memory safely
> ------------------------------------------
>
>                 Key: ARROW-2382
>                 URL: https://issues.apache.org/jira/browse/ARROW-2382
>             Project: Apache Arrow
>          Issue Type: Bug
>          Components: Rust
>            Reporter: Andy Grove
>            Assignee: Andy Grove
>            Priority: Major
>              Labels: pull-request-available
>             Fix For: 0.10.0
>
>
> During testing, I discovered some bugs in List<T> where it was not using 
> memory safely. These are fixed in the following PR:
> https://github.com/apache/arrow/pull/1827



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to