Skip to content

Commit

Permalink
Defend against mutable type objects when extracting their full name.
Browse files Browse the repository at this point in the history
  • Loading branch information
adamreichold committed Dec 19, 2023
1 parent 7b7834e commit d521542
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/types/typeobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,15 @@ impl PyType {
pub fn name(&self) -> PyResult<Cow<'_, str>> {
#[cfg(not(any(Py_LIMITED_API, PyPy)))]
{
let name = unsafe { CStr::from_ptr((*self.as_type_ptr()).tp_name) }.to_str()?;
let ptr = self.as_type_ptr();

Ok(Cow::Borrowed(name))
let name = unsafe { CStr::from_ptr((*ptr).tp_name) }.to_str()?;

if unsafe { ffi::PyType_HasFeature(ptr, ffi::Py_TPFLAGS_IMMUTABLETYPE) } != 0 {
Ok(Cow::Borrowed(name))
} else {
Ok(Cow::Owned(name.to_owned()))
}
}

#[cfg(any(Py_LIMITED_API, PyPy))]
Expand Down

0 comments on commit d521542

Please sign in to comment.