Skip to content

Commit

Permalink
pythongh-102701: Fix overflow in dictobject.c (pythonGH-102750)
Browse files Browse the repository at this point in the history
(cherry picked from commit 65fb7c4)

Co-authored-by: Inada Naoki <songofacandy@gmail.com>
  • Loading branch information
methane authored and miss-islington committed Mar 17, 2023
1 parent d025b1d commit fcc7807
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
9 changes: 9 additions & 0 deletions Lib/test/test_bigmem.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix overflow when creating very large dict.
2 changes: 1 addition & 1 deletion Objects/dictobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ new_keys_object(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;
}
Expand Down

0 comments on commit fcc7807

Please sign in to comment.