From 61b1c0de4e5c99a4888c9bf34c928101a5f6f85f Mon Sep 17 00:00:00 2001 From: Adam Reichold Date: Tue, 19 Dec 2023 14:59:38 +0100 Subject: [PATCH] Defend against mutable type objects when extracting their full name. --- src/types/typeobject.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/types/typeobject.rs b/src/types/typeobject.rs index da2ac33034d..a1966ae96fc 100644 --- a/src/types/typeobject.rs +++ b/src/types/typeobject.rs @@ -57,9 +57,16 @@ impl PyType { pub fn name(&self) -> PyResult> { #[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()?; + + #[cfg(Py_3_10)] + if unsafe { ffi::PyType_HasFeature(ptr, ffi::Py_TPFLAGS_IMMUTABLETYPE) } != 0 { + return Ok(Cow::Borrowed(name)); + } + + Ok(Cow::Owned(name.to_owned())) } #[cfg(any(Py_LIMITED_API, PyPy))]