]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-102701: Fix overflow in dictobject.c (GH-102750)
authorInada Naoki <songofacandy@gmail.com>
Fri, 17 Mar 2023 13:39:09 +0000 (22:39 +0900)
committerGitHub <noreply@github.com>
Fri, 17 Mar 2023 13:39:09 +0000 (22:39 +0900)
Lib/test/test_bigmem.py
Misc/NEWS.d/next/Core and Builtins/2023-03-16-17-24-44.gh-issue-102701.iNGVaS.rst [new file with mode: 0644]
Objects/dictobject.c

index 859f1539e20b804970108f54687c0ba5e8d7b245..c9ab1c1de9e1865ac17ca55b510b36dc55f78565 100644 (file)
@@ -1248,6 +1248,15 @@ class ListTest(unittest.TestCase):
         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 (file)
index 0000000..4e1f318
--- /dev/null
@@ -0,0 +1 @@
+Fix overflow when creating very large dict.
index 227e438a8dfffce6714721849506404a316c2bf3..53f9a380346a0de16d35561eeac995a0d2c60de5 100644 (file)
@@ -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;
     }