bpo-27946: Fix possible crash in ElementTree.Element (GH-29915)

Getting an attribute via attrib.get() simultaneously with replacing
the attrib dict can lead to access to deallocated dict.
This commit is contained in:
Serhiy Storchaka 2021-12-05 14:22:54 +02:00 committed by GitHub
parent f42a06ba27
commit d15cdb2f32
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 13 deletions

View file

@ -169,6 +169,18 @@ class MiscTests(unittest.TestCase):
del parser
support.gc_collect()
def test_dict_disappearing_during_get_item(self):
# test fix for seg fault reported in issue 27946
class X:
def __hash__(self):
e.attrib = {} # this frees e->extra->attrib
[{i: i} for i in range(1000)] # exhaust the dict keys cache
return 13
e = cET.Element("elem", {1: 2})
r = e.get(X())
self.assertIsNone(r)
@unittest.skipUnless(cET, 'requires _elementtree')
class TestAliasWorking(unittest.TestCase):