Skip to content

Commit

Permalink
Merge pull request #913 from boriel-basic/fix/crash_in_opt_evaluator
Browse files Browse the repository at this point in the history
Fix/crash in opt evaluator
  • Loading branch information
boriel authored Nov 26, 2024
2 parents ecd161b + 97dd410 commit 8d22b6f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/arch/z80/peephole/evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def __init__(self, expression):
expression[2] = Evaluator(expression[2])
else: # It's a list
assert len(expression) % 2 # Must be odd length
assert all(x == FN.OP_COMMA for i, x in enumerate(expression) if i % 2)
assert all(x == FN.OP_COMMA for i, x in enumerate(expression) if i % 2), f"Invalid expression {expression}"
self.expression = [Evaluator(x) if not i % 2 else x for i, x in enumerate(expression)]

@staticmethod
Expand Down
4 changes: 4 additions & 0 deletions src/arch/z80/peephole/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ def parse_ifline(if_line: str, lineno: int) -> TreeType | None:
errmsg.warning(lineno, "missing element in list")
return None

if any(x != FN.OP_COMMA for i, x in enumerate(expr) if i % 2):
errmsg.warning(lineno, f"Invalid list {expr}")
return None

stack[-1].append(expr)
expr = stack.pop()
else:
Expand Down
68 changes: 42 additions & 26 deletions tests/arch/zx48k/peephole/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,14 +399,8 @@ def test_parse_if_must_start_in_a_new_line(self):
def test_parse_with_ending_binary_error(self):
result = parser.parse_str(
"""
;; Remove the boolean normalization if it's done after calling
;; certain routines that return the bool result already normalized.
;; The sequence
;; sub 1
;; sbc a, a
;; inc a
;; can be removed
;; Sample Comment
;;
OLEVEL: 1
OFLAG: 20
Expand All @@ -428,15 +422,6 @@ def test_parse_with_ending_binary_error(self):
def test_parse_with_comma_error(self):
result = parser.parse_str(
"""
;; Remove the boolean normalization if it's done after calling
;; certain routines that return the bool result already normalized.
;; The sequence
;; sub 1
;; sbc a, a
;; inc a
;; can be removed
OLEVEL: 1
OFLAG: 20
Expand All @@ -458,15 +443,6 @@ def test_parse_with_comma_error(self):
def test_parse_with_nested_comma_error(self):
result = parser.parse_str(
"""
;; Remove the boolean normalization if it's done after calling
;; certain routines that return the bool result already normalized.
;; The sequence
;; sub 1
;; sbc a, a
;; inc a
;; can be removed
OLEVEL: 1
OFLAG: 20
Expand All @@ -484,3 +460,43 @@ def test_parse_with_nested_comma_error(self):
"""
)
assert result is None

def test_parse_with_list_error(self):
result = parser.parse_str(
"""
OLEVEL: 1
OFLAG: 20
REPLACE {{
$1
}}
WITH {{
}}
IF {{
$1 IN ("x", "y" . "pera")
}}
"""
)
assert result is None

def test_parse_with_list_error2(self):
result = parser.parse_str(
"""
OLEVEL: 1
OFLAG: 20
REPLACE {{
$1
}}
WITH {{
}}
IF {{
$1 IN ("x", , , "pera")
}}
"""
)
assert result is None

0 comments on commit 8d22b6f

Please sign in to comment.