Skip to content

Commit 1e1f1f4

Browse files
committed
fix UB in to_npy_dims
1 parent a7fa4c8 commit 1e1f1f4

File tree

3 files changed

+9
-8
lines changed

3 files changed

+9
-8
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- v0.17.1
66
- Fix use-after-free in `PyArray::resize`, `PyArray::reshape` and `PyArray::reshape_with_order`. ([#341](https://github.com/PyO3/rust-numpy/pull/341))
7+
- Fix UB in `ToNpyDims::as_dims_ptr` with dimensions of dynamic size (-1). ([#344](https://github.com/PyO3/rust-numpy/pull/344))
78

89
- v0.17.0
910
- Add dynamic borrow checking to safely construct references into the interior of NumPy arrays. ([#274](https://github.com/PyO3/rust-numpy/pull/274))

src/array.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
464464
where
465465
ID: IntoDimension<Dim = D>,
466466
{
467-
let dims = dims.into_dimension();
467+
let mut dims = dims.into_dimension();
468468
let ptr = PY_ARRAY_API.PyArray_NewFromDescr(
469469
py,
470470
PY_ARRAY_API.get_type_object(py, npyffi::NpyTypes::PyArray_Type),
@@ -490,7 +490,7 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
490490
where
491491
ID: IntoDimension<Dim = D>,
492492
{
493-
let dims = dims.into_dimension();
493+
let mut dims = dims.into_dimension();
494494
let ptr = PY_ARRAY_API.PyArray_NewFromDescr(
495495
py,
496496
PY_ARRAY_API.get_type_object(py, npyffi::NpyTypes::PyArray_Type),
@@ -612,7 +612,7 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
612612
where
613613
ID: IntoDimension<Dim = D>,
614614
{
615-
let dims = dims.into_dimension();
615+
let mut dims = dims.into_dimension();
616616
unsafe {
617617
let ptr = PY_ARRAY_API.PyArray_Zeros(
618618
py,
@@ -1379,7 +1379,7 @@ impl<T: Element, D> PyArray<T, D> {
13791379
dims: ID,
13801380
order: NPY_ORDER,
13811381
) -> PyResult<&'py PyArray<T, ID::Dim>> {
1382-
let dims = dims.into_dimension();
1382+
let mut dims = dims.into_dimension();
13831383
let mut dims = dims.to_npy_dims();
13841384
let ptr = unsafe {
13851385
PY_ARRAY_API.PyArray_Newshape(
@@ -1437,7 +1437,7 @@ impl<T: Element, D> PyArray<T, D> {
14371437
/// [ndarray-resize]: https://numpy.org/doc/stable/reference/generated/numpy.ndarray.resize.html
14381438
/// [PyArray_Resize]: https://numpy.org/doc/stable/reference/c-api/array.html#c.PyArray_Resize
14391439
pub unsafe fn resize<ID: IntoDimension>(&self, dims: ID) -> PyResult<()> {
1440-
let dims = dims.into_dimension();
1440+
let mut dims = dims.into_dimension();
14411441
let mut dims = dims.to_npy_dims();
14421442
let res = PY_ARRAY_API.PyArray_Resize(
14431443
self.py(),

src/convert.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,11 @@ pub trait ToNpyDims: Dimension + Sealed {
213213
self.ndim() as c_int
214214
}
215215
#[doc(hidden)]
216-
fn as_dims_ptr(&self) -> *mut npyffi::npy_intp {
217-
self.slice().as_ptr() as *mut npyffi::npy_intp
216+
fn as_dims_ptr(&mut self) -> *mut npyffi::npy_intp {
217+
self.slice_mut().as_ptr() as *mut npyffi::npy_intp
218218
}
219219
#[doc(hidden)]
220-
fn to_npy_dims(&self) -> npyffi::PyArray_Dims {
220+
fn to_npy_dims(&mut self) -> npyffi::PyArray_Dims {
221221
npyffi::PyArray_Dims {
222222
ptr: self.as_dims_ptr(),
223223
len: self.ndim_cint(),

0 commit comments

Comments
 (0)