From 5868c3968dd1c9cc582d2643a0aafe76881cc588 Mon Sep 17 00:00:00 2001 From: Jose Rodriguez Date: Sat, 28 Sep 2019 17:29:55 +0200 Subject: [PATCH] Fix parser crash when assigning substr When assiging a substr to an array string the parser might crash if the array is not declared previously. --- tests/functional/substr_err.bas | 2 ++ zxbparser.py | 9 ++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 tests/functional/substr_err.bas diff --git a/tests/functional/substr_err.bas b/tests/functional/substr_err.bas new file mode 100644 index 000000000..3f9a83b09 --- /dev/null +++ b/tests/functional/substr_err.bas @@ -0,0 +1,2 @@ +IF k$<>s$ THEN REM +IF m$(s(1),s(2))="\b" THEN LET m$(s(1),s(2))="\c" diff --git a/zxbparser.py b/zxbparser.py index 2bba3f678..a3fdb6814 100755 --- a/zxbparser.py +++ b/zxbparser.py @@ -1166,10 +1166,17 @@ def p_substr_assignment(p): if entry.class_ == CLASS.unknown: entry.class_ = CLASS.var - assert entry.class_ == CLASS.var and entry.type_ == TYPE.string + if entry.class_ != CLASS.var: + api.errmsg.syntax_error_cannot_assign_not_a_var(p.lineno(2), p[2]) + return + + if entry.type_ != TYPE.string: + api.errmsg.syntax_error_expected_string(p.lineno(2), entry.type_) + return if p[5].type_ != TYPE.string: api.errmsg.syntax_error_expected_string(p.lineno(4), p[5].type_) + return if len(p[3]) > 1: syntax_error(p.lineno(2), "Accessing string with too many indexes. Expected only one.")