From 65fb7c4055f280caaa970939d16dd947e6df8a8d Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Fri, 17 Mar 2023 22:39:09 +0900 Subject: [PATCH] gh-102701: Fix overflow in dictobject.c (GH-102750) --- Lib/test/test_bigmem.py | 9 +++++++++ .../2023-03-16-17-24-44.gh-issue-102701.iNGVaS.rst | 1 + Objects/dictobject.c | 2 +- 3 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2023-03-16-17-24-44.gh-issue-102701.iNGVaS.rst diff --git a/Lib/test/test_bigmem.py b/Lib/test/test_bigmem.py index 859f1539e20b80..c9ab1c1de9e186 100644 --- a/Lib/test/test_bigmem.py +++ b/Lib/test/test_bigmem.py @@ -1248,6 +1248,15 @@ def test_sort(self, size): self.assertEqual(l[-10:], [5] * 10) +class DictTest(unittest.TestCase): + + @bigmemtest(size=357913941, memuse=160) + def test_dict(self, size): + # https://github.com/python/cpython/issues/102701 + d = dict.fromkeys(range(size)) + d[size] = 1 + + if __name__ == '__main__': if len(sys.argv) > 1: support.set_memlimit(sys.argv[1]) diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-03-16-17-24-44.gh-issue-102701.iNGVaS.rst b/Misc/NEWS.d/next/Core and Builtins/2023-03-16-17-24-44.gh-issue-102701.iNGVaS.rst new file mode 100644 index 00000000000000..4e1f31893377ba --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2023-03-16-17-24-44.gh-issue-102701.iNGVaS.rst @@ -0,0 +1 @@ +Fix overflow when creating very large dict. diff --git a/Objects/dictobject.c b/Objects/dictobject.c index 227e438a8dfffc..53f9a380346a0d 100644 --- a/Objects/dictobject.c +++ b/Objects/dictobject.c @@ -596,7 +596,7 @@ new_keys_object(PyInterpreterState *interp, uint8_t log2_size, bool unicode) assert(log2_size >= PyDict_LOG_MINSIZE); - usable = USABLE_FRACTION(1<