Skip to content

Commit dfe1ccc

Browse files
correctmostDanielNoord
authored andcommitted
Fix crashes with large positive and negative list multipliers
Closes #2521 Closes #2523
1 parent ba7df4a commit dfe1ccc

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

ChangeLog

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ What's New in astroid 4.0.0?
77
============================
88
Release date: TBA
99

10+
* Fix crashes with large positive and negative list multipliers.
11+
12+
Closes #2521
13+
Closes #2523
1014

1115

1216
What's New in astroid 3.3.5?

astroid/protocols.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,10 @@ def _multiply_seq_by_int(
142142
context: InferenceContext,
143143
) -> _TupleListNodeT:
144144
node = self.__class__(parent=opnode)
145-
if value > 1e8:
145+
if value <= 0:
146+
node.elts = []
147+
return node
148+
if len(self.elts) * value > 1e8:
146149
node.elts = [util.Uninferable]
147150
return node
148151
filtered_elts = (

tests/test_protocols.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,23 @@ def test_uninferable_list_multiplication() -> None:
286286
element = parsed.inferred()[0].elts[0]
287287
assert element.value is Uninferable
288288

289+
@staticmethod
290+
def test_uninferable_list_multiplication_with_multiple_operands() -> None:
291+
"""Attempting to calculate the result is prohibitively expensive."""
292+
parsed = extract_node("[0] * 825 * 16547118")
293+
element = parsed.inferred()[0].elts[0]
294+
assert element.value is Uninferable
295+
296+
@staticmethod
297+
def test_list_multiplication_with_zero_multiplier() -> None:
298+
parsed = extract_node("[0] * 0")
299+
assert parsed.inferred()[0].elts == []
300+
301+
@staticmethod
302+
def test_list_multiplication_with_negative_multiplier() -> None:
303+
parsed = extract_node("[0] * -9223372036854775809")
304+
assert parsed.inferred()[0].elts == []
305+
289306

290307
def test_named_expr_inference() -> None:
291308
code = """

0 commit comments

Comments
 (0)