From 542ebb94d6fe414b3e296b174a6b1bbaf3b36d81 Mon Sep 17 00:00:00 2001 From: Adam Reichold Date: Tue, 22 Mar 2022 22:58:54 +0100 Subject: [PATCH] Make the internal PyArray::data function safe While doing anything with the resulting pointer requires unsafe, producing the pointer should not. (Dereferencing `self.as_array_ptr()` should be safe for the same reasons that dereferencing it in say `PyArray::shape` is, i.e. we ensure that `PyArray` is only constructed for NumPy arrays.) --- src/array.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/array.rs b/src/array.rs index b5aed2b43..ff15c5a2b 100644 --- a/src/array.rs +++ b/src/array.rs @@ -340,9 +340,9 @@ impl PyArray { } /// Returns the pointer to the first element of the inner array. - pub(crate) unsafe fn data(&self) -> *mut T { + pub(crate) fn data(&self) -> *mut T { let ptr = self.as_array_ptr(); - (*ptr).data as *mut _ + unsafe { (*ptr).data as *mut _ } } } @@ -381,7 +381,7 @@ impl PyArray { let strides = self.strides(); let mut new_strides = D::zeros(strides.len()); - let mut data_ptr = unsafe { self.data() }; + let mut data_ptr = self.data(); let mut inverted_axes = InvertedAxes::new(strides.len()); for i in 0..strides.len() {