Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/crash in opt evaluator #913

Merged
merged 2 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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