mirror of
https://github.com/python/cpython.git
synced 2026-01-16 12:20:02 +00:00
[3.13] gh-143635: Fix crash in ga_repr_items_list (GH-143670) (#143852)
(cherry picked from commit bdba5f0db2)
This commit is contained in:
parent
149ecbb9a9
commit
aa5ad50597
3 changed files with 57 additions and 1 deletions
|
|
@ -232,6 +232,56 @@ class MyGeneric:
|
|||
self.assertTrue(repr(MyGeneric[[]]).endswith('MyGeneric[[]]'))
|
||||
self.assertTrue(repr(MyGeneric[[int, str]]).endswith('MyGeneric[[int, str]]'))
|
||||
|
||||
def test_evil_repr1(self):
|
||||
# gh-143635
|
||||
class Zap:
|
||||
def __init__(self, container):
|
||||
self.container = container
|
||||
def __getattr__(self, name):
|
||||
if name == "__origin__":
|
||||
self.container.clear()
|
||||
return None
|
||||
if name == "__args__":
|
||||
return ()
|
||||
raise AttributeError
|
||||
|
||||
params = []
|
||||
params.append(Zap(params))
|
||||
alias = GenericAlias(list, (params,))
|
||||
repr_str = repr(alias)
|
||||
self.assertTrue(repr_str.startswith("list[["), repr_str)
|
||||
|
||||
def test_evil_repr2(self):
|
||||
class Zap:
|
||||
def __init__(self, container):
|
||||
self.container = container
|
||||
def __getattr__(self, name):
|
||||
if name == "__qualname__":
|
||||
self.container.clear()
|
||||
return "abcd"
|
||||
if name == "__module__":
|
||||
return None
|
||||
raise AttributeError
|
||||
|
||||
params = []
|
||||
params.append(Zap(params))
|
||||
alias = GenericAlias(list, (params,))
|
||||
repr_str = repr(alias)
|
||||
self.assertTrue(repr_str.startswith("list[["), repr_str)
|
||||
|
||||
def test_evil_repr3(self):
|
||||
# gh-143823
|
||||
lst = []
|
||||
class X:
|
||||
def __repr__(self):
|
||||
lst.clear()
|
||||
return "x"
|
||||
|
||||
lst += [X(), 1]
|
||||
ga = GenericAlias(int, lst)
|
||||
with self.assertRaises(IndexError):
|
||||
repr(ga)
|
||||
|
||||
def test_exposed_type(self):
|
||||
import types
|
||||
a = types.GenericAlias(list, int)
|
||||
|
|
|
|||
|
|
@ -0,0 +1 @@
|
|||
Fixes a crash in ``ga_repr_items_list`` function.
|
||||
|
|
@ -133,10 +133,15 @@ ga_repr_items_list(_PyUnicodeWriter *writer, PyObject *p)
|
|||
return -1;
|
||||
}
|
||||
}
|
||||
PyObject *item = PyList_GET_ITEM(p, i);
|
||||
PyObject *item = PyList_GetItemRef(p, i);
|
||||
if (item == NULL) {
|
||||
return -1; // list can be mutated in a callback
|
||||
}
|
||||
if (ga_repr_item(writer, item) < 0) {
|
||||
Py_DECREF(item);
|
||||
return -1;
|
||||
}
|
||||
Py_DECREF(item);
|
||||
}
|
||||
|
||||
if (_PyUnicodeWriter_WriteASCIIString(writer, "]", 1) < 0) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue