Content-Length: 333446 | pFad | http://github.com/RustPython/RustPython/commit/e21ec550d4cfa65e85f1ebe2696c1998b8865790

56 Fix set___name__ and set___qualname__ deadlock (#5956) · RustPython/RustPython@e21ec55 · GitHub
Skip to content

Commit e21ec55

Browse files
authored
Fix set___name__ and set___qualname__ deadlock (#5956)
1 parent ac20b00 commit e21ec55

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

Lib/test/test_descr.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4261,7 +4261,6 @@ class C(object):
42614261
C.__name__ = 'D.E'
42624262
self.assertEqual((C.__module__, C.__name__), (mod, 'D.E'))
42634263

4264-
@unittest.skip("TODO: RUSTPYTHON, rustpython hang")
42654264
def test_evil_type_name(self):
42664265
# A badly placed Py_DECREF in type_set_name led to arbitrary code
42674266
# execution while the type structure was not in a sane state, and a

vm/src/builtins/type.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,15 @@ impl PyType {
680680
.heaptype_ext
681681
.as_ref()
682682
.expect("HEAPTYPE should have heaptype_ext");
683-
*heap_type.qualname.write() = str_value;
683+
684+
// Use std::mem::replace to swap the new value in and get the old value out,
685+
// then drop the old value after releasing the lock
686+
let _old_qualname = {
687+
let mut qualname_guard = heap_type.qualname.write();
688+
std::mem::replace(&mut *qualname_guard, str_value)
689+
};
690+
// old_qualname is dropped here, outside the lock scope
691+
684692
Ok(())
685693
}
686694

@@ -837,7 +845,13 @@ impl PyType {
837845
return Err(vm.new_value_error("type name must not contain null characters"));
838846
}
839847

840-
*self.heaptype_ext.as_ref().unwrap().name.write() = name;
848+
// Use std::mem::replace to swap the new value in and get the old value out,
849+
// then drop the old value after releasing the lock (similar to CPython's Py_SETREF)
850+
let _old_name = {
851+
let mut name_guard = self.heaptype_ext.as_ref().unwrap().name.write();
852+
std::mem::replace(&mut *name_guard, name)
853+
};
854+
// old_name is dropped here, outside the lock scope
841855

842856
Ok(())
843857
}

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/RustPython/RustPython/commit/e21ec550d4cfa65e85f1ebe2696c1998b8865790

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy