Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix use-after-free when calling to_npy_dims helper method. #341

Merged
merged 1 commit into from
Aug 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

- Unreleased

- v0.17.1
- Fix use-after-free in `PyArray::resize`, `PyArray::reshape` and `PyArray::reshape_with_order`. ([#341](https://github.com/PyO3/rust-numpy/pull/341))

- v0.17.0
- Add dynamic borrow checking to safely construct references into the interior of NumPy arrays. ([#274](https://github.com/PyO3/rust-numpy/pull/274))
- The deprecated iterator builders `NpySingleIterBuilder::{readonly,readwrite}` and `NpyMultiIterBuilder::add_{readonly,readwrite}` now take referencces to `PyReadonlyArray` and `PyReadwriteArray` instead of consuming them.
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "numpy"
version = "0.17.0"
version = "0.17.1"
authors = [
"The rust-numpy Project Developers",
"PyO3 Project and Contributors <https://github.com/PyO3>"
Expand Down
6 changes: 4 additions & 2 deletions src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1379,7 +1379,8 @@ impl<T: Element, D> PyArray<T, D> {
dims: ID,
order: NPY_ORDER,
) -> PyResult<&'py PyArray<T, ID::Dim>> {
let mut dims = dims.into_dimension().to_npy_dims();
let dims = dims.into_dimension();
let mut dims = dims.to_npy_dims();
let ptr = unsafe {
PY_ARRAY_API.PyArray_Newshape(
self.py(),
Expand Down Expand Up @@ -1436,7 +1437,8 @@ impl<T: Element, D> PyArray<T, D> {
/// [ndarray-resize]: https://numpy.org/doc/stable/reference/generated/numpy.ndarray.resize.html
/// [PyArray_Resize]: https://numpy.org/doc/stable/reference/c-api/array.html#c.PyArray_Resize
pub unsafe fn resize<ID: IntoDimension>(&self, dims: ID) -> PyResult<()> {
let mut dims = dims.into_dimension().to_npy_dims();
let dims = dims.into_dimension();
let mut dims = dims.to_npy_dims();
let res = PY_ARRAY_API.PyArray_Resize(
self.py(),
self.as_array_ptr(),
Expand Down
21 changes: 19 additions & 2 deletions tests/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use std::mem::size_of;
use half::f16;
use ndarray::{array, s, Array1, Dim};
use numpy::{
dtype, get_array_module, pyarray, PyArray, PyArray1, PyArray2, PyArrayDescr, PyArrayDyn,
ToPyArray,
dtype, get_array_module, npyffi::NPY_ORDER, pyarray, PyArray, PyArray1, PyArray2, PyArrayDescr,
PyArrayDyn, ToPyArray,
};
use pyo3::{
py_run, pyclass, pymethods,
Expand Down Expand Up @@ -508,6 +508,23 @@ fn get_works() {
});
}

#[test]
fn reshape() {
Python::with_gil(|py| {
let array = PyArray::from_iter(py, 0..9)
.reshape_with_order([3, 3], NPY_ORDER::NPY_FORTRANORDER)
.unwrap();

assert_eq!(
array.readonly().as_array(),
array![[0, 3, 6], [1, 4, 7], [2, 5, 8]]
);
assert!(array.is_fortran_contiguous());

assert!(array.reshape([5]).is_err());
});
}

#[cfg(feature = "half")]
#[test]
fn half_works() {
Expand Down