Skip to content

Commit f967aee

Browse files
gh-102701: Fix overflow in dictobject.c (GH-102750)
(cherry picked from commit 65fb7c4) Co-authored-by: Inada Naoki <songofacandy@gmail.com>
1 parent d025b1d commit f967aee

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

Lib/test/test_bigmem.py

+9
Original file line numberDiff line numberDiff line change
@@ -1248,6 +1248,15 @@ def test_sort(self, size):
12481248
self.assertEqual(l[-10:], [5] * 10)
12491249

12501250

1251+
class DictTest(unittest.TestCase):
1252+
1253+
@bigmemtest(size=357913941, memuse=160)
1254+
def test_dict(self, size):
1255+
# https://github.com/python/cpython/issues/102701
1256+
d = dict.fromkeys(range(size))
1257+
d[size] = 1
1258+
1259+
12511260
if __name__ == '__main__':
12521261
if len(sys.argv) > 1:
12531262
support.set_memlimit(sys.argv[1])
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix overflow when creating very large dict.

Objects/dictobject.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ new_keys_object(uint8_t log2_size, bool unicode)
600600

601601
assert(log2_size >= PyDict_LOG_MINSIZE);
602602

603-
usable = USABLE_FRACTION(1<<log2_size);
603+
usable = USABLE_FRACTION((size_t)1<<log2_size);
604604
if (log2_size < 8) {
605605
log2_bytes = log2_size;
606606
}

0 commit comments

Comments
 (0)