[Python-checkins] gh-102701: Fix overflow in dictobject.c (GH-102750)

methane webhook-mailer at python.org
Fri Mar 17 09:39:51 EDT 2023


https://github.com/python/cpython/commit/65fb7c4055f280caaa970939d16dd947e6df8a8d
commit: 65fb7c4055f280caaa970939d16dd947e6df8a8d
branch: main
author: Inada Naoki <songofacandy at gmail.com>
committer: methane <songofacandy at gmail.com>
date: 2023-03-17T22:39:09+09:00
summary:

gh-102701: Fix overflow in dictobject.c (GH-102750)

files:
A Misc/NEWS.d/next/Core and Builtins/2023-03-16-17-24-44.gh-issue-102701.iNGVaS.rst
M Lib/test/test_bigmem.py
M Objects/dictobject.c

diff --git a/Lib/test/test_bigmem.py b/Lib/test/test_bigmem.py
index 859f1539e20b..c9ab1c1de9e1 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 000000000000..4e1f31893377
--- /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 227e438a8dff..53f9a380346a 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<<log2_size);
+    usable = USABLE_FRACTION((size_t)1<<log2_size);
     if (log2_size < 8) {
         log2_bytes = log2_size;
     }



More information about the Python-checkins mailing list