Skip to content

Commit 993eda8

Browse files
Do not double free
1 parent fb5071b commit 993eda8

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

src/byteswriter.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ use crate::{
1515
use crate::{Bound, IntoPyObject, PyErr, PyResult, Python};
1616
use std::io::IoSlice;
1717
#[cfg(not(Py_LIMITED_API))]
18-
use std::ptr::{self, NonNull};
18+
use std::{
19+
mem::ManuallyDrop,
20+
ptr::{self, NonNull},
21+
};
1922

2023
pub struct PyBytesWriter<'py> {
2124
python: Python<'py>,
@@ -88,9 +91,10 @@ impl<'py> TryFrom<PyBytesWriter<'py>> for Bound<'py, PyBytes> {
8891

8992
#[inline]
9093
fn try_from(value: PyBytesWriter<'py>) -> Result<Self, Self::Error> {
94+
let py = value.python;
9195
unsafe {
92-
PyBytesWriter_Finish(value.writer.as_ptr())
93-
.assume_owned_or_err(value.python)
96+
PyBytesWriter_Finish(ManuallyDrop::new(value).writer.as_ptr())
97+
.assume_owned_or_err(py)
9498
.cast_into_unchecked()
9599
}
96100
}

0 commit comments

Comments
 (0)