From 96f0dd3cbc404b7f96bd7a35247faea8da266638 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 24 Jul 2015 15:05:56 +0000 Subject: [PATCH] py/parse: Fix handling of empty input so it raises an exception. --- py/grammar.h | 2 +- py/parse.c | 5 +++++ tests/basics/parser.py | 24 ++++++++++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 tests/basics/parser.py diff --git a/py/grammar.h b/py/grammar.h index be12b7eb53..3f66688089 100644 --- a/py/grammar.h +++ b/py/grammar.h @@ -38,7 +38,7 @@ // eval_input: testlist NEWLINE* ENDMARKER DEF_RULE(single_input, nc, or(3), tok(NEWLINE), rule(simple_stmt), rule(compound_stmt)) -DEF_RULE(file_input, nc, and(1), opt_rule(file_input_2)) +DEF_RULE(file_input, c(generic_all_nodes), and(1), opt_rule(file_input_2)) DEF_RULE(file_input_2, c(generic_all_nodes), one_or_more, rule(file_input_3)) DEF_RULE(file_input_3, nc, or(2), tok(NEWLINE), rule(stmt)) DEF_RULE(eval_input, nc, and(2), rule(testlist), opt_rule(eval_input_2)) diff --git a/py/parse.c b/py/parse.c index ad13a90309..b20fdaacd2 100644 --- a/py/parse.c +++ b/py/parse.c @@ -720,6 +720,11 @@ memory_error: goto syntax_error; } + // check that parsing resulted in a parse node (can fail on empty input) + if (parser.result_stack_top == 0) { + goto syntax_error; + } + //result_stack_show(parser); //printf("rule stack alloc: %d\n", parser.rule_stack_alloc); //printf("result stack alloc: %d\n", parser.result_stack_alloc); diff --git a/tests/basics/parser.py b/tests/basics/parser.py new file mode 100644 index 0000000000..e12f02599e --- /dev/null +++ b/tests/basics/parser.py @@ -0,0 +1,24 @@ +# parser tests + +# completely empty string +# uPy and CPy differ for this case +#try: +# compile("", "stdin", "single") +#except SyntaxError: +# print("SyntaxError") +try: + compile("", "stdin", "eval") +except SyntaxError: + print("SyntaxError") +compile("", "stdin", "exec") + +# empty continued line +try: + compile("\\\n", "stdin", "single") +except SyntaxError: + print("SyntaxError") +try: + compile("\\\n", "stdin", "eval") +except SyntaxError: + print("SyntaxError") +compile("\\\n", "stdin", "exec")