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

Towards soundness of PyByteArray::to_vec #4742

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions newsfragments/4742.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improved soundness of `PyByteArray::to_vec`.
robsdedude marked this conversation as resolved.
Show resolved Hide resolved
5 changes: 3 additions & 2 deletions src/types/bytearray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::err::{PyErr, PyResult};
use crate::ffi_ptr_ext::FfiPtrExt;
use crate::instance::{Borrowed, Bound};
use crate::py_result_ext::PyResultExt;
use crate::sync::with_critical_section;
use crate::types::any::PyAnyMethods;
use crate::{ffi, PyAny, Python};
use std::slice;
Expand Down Expand Up @@ -152,7 +153,7 @@ pub trait PyByteArrayMethods<'py>: crate::sealed::Sealed {
/// using the slice.
///
/// As a result, this slice should only be used for short-lived operations without executing any
/// Python code, such as copying into a Vec.
/// Python code, such as copying into a Vec. For free-threaded Python support see also [`with_critical_section`].
///
/// # Examples
///
Expand Down Expand Up @@ -295,7 +296,7 @@ impl<'py> PyByteArrayMethods<'py> for Bound<'py, PyByteArray> {
}

fn to_vec(&self) -> Vec<u8> {
unsafe { self.as_bytes() }.to_vec()
with_critical_section(self, || unsafe { self.as_bytes() }.to_vec())
}

fn resize(&self, len: usize) -> PyResult<()> {
Expand Down
Loading