Skip to content

Commit 8d22b6f

Browse files
authored
Merge pull request #913 from boriel-basic/fix/crash_in_opt_evaluator
Fix/crash in opt evaluator
2 parents ecd161b + 97dd410 commit 8d22b6f

File tree

3 files changed

+47
-27
lines changed

3 files changed

+47
-27
lines changed

src/arch/z80/peephole/evaluator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ def __init__(self, expression):
195195
expression[2] = Evaluator(expression[2])
196196
else: # It's a list
197197
assert len(expression) % 2 # Must be odd length
198-
assert all(x == FN.OP_COMMA for i, x in enumerate(expression) if i % 2)
198+
assert all(x == FN.OP_COMMA for i, x in enumerate(expression) if i % 2), f"Invalid expression {expression}"
199199
self.expression = [Evaluator(x) if not i % 2 else x for i, x in enumerate(expression)]
200200

201201
@staticmethod

src/arch/z80/peephole/parser.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,10 @@ def parse_ifline(if_line: str, lineno: int) -> TreeType | None:
161161
errmsg.warning(lineno, "missing element in list")
162162
return None
163163

164+
if any(x != FN.OP_COMMA for i, x in enumerate(expr) if i % 2):
165+
errmsg.warning(lineno, f"Invalid list {expr}")
166+
return None
167+
164168
stack[-1].append(expr)
165169
expr = stack.pop()
166170
else:

tests/arch/zx48k/peephole/test_parser.py

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -399,14 +399,8 @@ def test_parse_if_must_start_in_a_new_line(self):
399399
def test_parse_with_ending_binary_error(self):
400400
result = parser.parse_str(
401401
"""
402-
;; Remove the boolean normalization if it's done after calling
403-
;; certain routines that return the bool result already normalized.
404-
405-
;; The sequence
406-
;; sub 1
407-
;; sbc a, a
408-
;; inc a
409-
;; can be removed
402+
;; Sample Comment
403+
;;
410404
411405
OLEVEL: 1
412406
OFLAG: 20
@@ -428,15 +422,6 @@ def test_parse_with_ending_binary_error(self):
428422
def test_parse_with_comma_error(self):
429423
result = parser.parse_str(
430424
"""
431-
;; Remove the boolean normalization if it's done after calling
432-
;; certain routines that return the bool result already normalized.
433-
434-
;; The sequence
435-
;; sub 1
436-
;; sbc a, a
437-
;; inc a
438-
;; can be removed
439-
440425
OLEVEL: 1
441426
OFLAG: 20
442427
@@ -458,15 +443,6 @@ def test_parse_with_comma_error(self):
458443
def test_parse_with_nested_comma_error(self):
459444
result = parser.parse_str(
460445
"""
461-
;; Remove the boolean normalization if it's done after calling
462-
;; certain routines that return the bool result already normalized.
463-
464-
;; The sequence
465-
;; sub 1
466-
;; sbc a, a
467-
;; inc a
468-
;; can be removed
469-
470446
OLEVEL: 1
471447
OFLAG: 20
472448
@@ -484,3 +460,43 @@ def test_parse_with_nested_comma_error(self):
484460
"""
485461
)
486462
assert result is None
463+
464+
def test_parse_with_list_error(self):
465+
result = parser.parse_str(
466+
"""
467+
OLEVEL: 1
468+
OFLAG: 20
469+
470+
REPLACE {{
471+
$1
472+
}}
473+
474+
WITH {{
475+
}}
476+
477+
IF {{
478+
$1 IN ("x", "y" . "pera")
479+
}}
480+
"""
481+
)
482+
assert result is None
483+
484+
def test_parse_with_list_error2(self):
485+
result = parser.parse_str(
486+
"""
487+
OLEVEL: 1
488+
OFLAG: 20
489+
490+
REPLACE {{
491+
$1
492+
}}
493+
494+
WITH {{
495+
}}
496+
497+
IF {{
498+
$1 IN ("x", , , "pera")
499+
}}
500+
"""
501+
)
502+
assert result is None

0 commit comments

Comments
 (0)