From de8285210a58f1ed129dff5168b0171c7dda0a3c Mon Sep 17 00:00:00 2001 From: correctmost <134317971+correctmost@users.noreply.github.com> Date: Wed, 2 Oct 2024 07:16:33 -0400 Subject: [PATCH] Fix crashes with large positive and negative list multipliers Closes #2521 Closes #2523 --- ChangeLog | 4 ++++ astroid/protocols.py | 5 ++++- tests/test_protocols.py | 17 +++++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index 56d97ad39..52d830903 100644 --- a/ChangeLog +++ b/ChangeLog @@ -7,6 +7,10 @@ What's New in astroid 4.0.0? ============================ Release date: TBA +* Fix crashes with large positive and negative list multipliers. + + Closes #2521 + Closes #2523 What's New in astroid 3.3.5? diff --git a/astroid/protocols.py b/astroid/protocols.py index 0bac32a29..4688f7471 100644 --- a/astroid/protocols.py +++ b/astroid/protocols.py @@ -142,7 +142,10 @@ def _multiply_seq_by_int( context: InferenceContext, ) -> _TupleListNodeT: node = self.__class__(parent=opnode) - if value > 1e8: + if value <= 0: + node.elts = [] + return node + if len(self.elts) * value > 1e8: node.elts = [util.Uninferable] return node filtered_elts = ( diff --git a/tests/test_protocols.py b/tests/test_protocols.py index 72b91a115..f718fd1e9 100644 --- a/tests/test_protocols.py +++ b/tests/test_protocols.py @@ -286,6 +286,23 @@ def test_uninferable_list_multiplication() -> None: element = parsed.inferred()[0].elts[0] assert element.value is Uninferable + @staticmethod + def test_uninferable_list_multiplication_with_multiple_operands() -> None: + """Attempting to calculate the result is prohibitively expensive.""" + parsed = extract_node("[0] * 825 * 16547118") + element = parsed.inferred()[0].elts[0] + assert element.value is Uninferable + + @staticmethod + def test_list_multiplication_with_zero_multiplier() -> None: + parsed = extract_node("[0] * 0") + assert parsed.inferred()[0].elts == [] + + @staticmethod + def test_list_multiplication_with_negative_multiplier() -> None: + parsed = extract_node("[0] * -9223372036854775809") + assert parsed.inferred()[0].elts == [] + def test_named_expr_inference() -> None: code = """