Skip to content

Commit fb3ff8c

Browse files
authored
Merge pull request #341 from PyO3/fix-use-after-free-to-npy-dims
Fix use-after-free when calling to_npy_dims helper method.
2 parents 52d70ca + c433442 commit fb3ff8c

File tree

4 files changed

+27
-5
lines changed

4 files changed

+27
-5
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
- Unreleased
44

5+
- v0.17.1
6+
- Fix use-after-free in `PyArray::resize`, `PyArray::reshape` and `PyArray::reshape_with_order`. ([#341](https://github.com/PyO3/rust-numpy/pull/341))
7+
58
- v0.17.0
69
- Add dynamic borrow checking to safely construct references into the interior of NumPy arrays. ([#274](https://github.com/PyO3/rust-numpy/pull/274))
710
- The deprecated iterator builders `NpySingleIterBuilder::{readonly,readwrite}` and `NpyMultiIterBuilder::add_{readonly,readwrite}` now take referencces to `PyReadonlyArray` and `PyReadwriteArray` instead of consuming them.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "numpy"
3-
version = "0.17.0"
3+
version = "0.17.1"
44
authors = [
55
"The rust-numpy Project Developers",
66
"PyO3 Project and Contributors <https://github.com/PyO3>"

src/array.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1379,7 +1379,8 @@ impl<T: Element, D> PyArray<T, D> {
13791379
dims: ID,
13801380
order: NPY_ORDER,
13811381
) -> PyResult<&'py PyArray<T, ID::Dim>> {
1382-
let mut dims = dims.into_dimension().to_npy_dims();
1382+
let dims = dims.into_dimension();
1383+
let mut dims = dims.to_npy_dims();
13831384
let ptr = unsafe {
13841385
PY_ARRAY_API.PyArray_Newshape(
13851386
self.py(),
@@ -1436,7 +1437,8 @@ impl<T: Element, D> PyArray<T, D> {
14361437
/// [ndarray-resize]: https://numpy.org/doc/stable/reference/generated/numpy.ndarray.resize.html
14371438
/// [PyArray_Resize]: https://numpy.org/doc/stable/reference/c-api/array.html#c.PyArray_Resize
14381439
pub unsafe fn resize<ID: IntoDimension>(&self, dims: ID) -> PyResult<()> {
1439-
let mut dims = dims.into_dimension().to_npy_dims();
1440+
let dims = dims.into_dimension();
1441+
let mut dims = dims.to_npy_dims();
14401442
let res = PY_ARRAY_API.PyArray_Resize(
14411443
self.py(),
14421444
self.as_array_ptr(),

tests/array.rs

+19-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use std::mem::size_of;
44
use half::f16;
55
use ndarray::{array, s, Array1, Dim};
66
use numpy::{
7-
dtype, get_array_module, pyarray, PyArray, PyArray1, PyArray2, PyArrayDescr, PyArrayDyn,
8-
ToPyArray,
7+
dtype, get_array_module, npyffi::NPY_ORDER, pyarray, PyArray, PyArray1, PyArray2, PyArrayDescr,
8+
PyArrayDyn, ToPyArray,
99
};
1010
use pyo3::{
1111
py_run, pyclass, pymethods,
@@ -508,6 +508,23 @@ fn get_works() {
508508
});
509509
}
510510

511+
#[test]
512+
fn reshape() {
513+
Python::with_gil(|py| {
514+
let array = PyArray::from_iter(py, 0..9)
515+
.reshape_with_order([3, 3], NPY_ORDER::NPY_FORTRANORDER)
516+
.unwrap();
517+
518+
assert_eq!(
519+
array.readonly().as_array(),
520+
array![[0, 3, 6], [1, 4, 7], [2, 5, 8]]
521+
);
522+
assert!(array.is_fortran_contiguous());
523+
524+
assert!(array.reshape([5]).is_err());
525+
});
526+
}
527+
511528
#[cfg(feature = "half")]
512529
#[test]
513530
fn half_works() {

0 commit comments

Comments
 (0)