From: Igor Sysoev Date: Mon, 16 Jan 2017 14:32:10 +0000 (+0300) Subject: Comprehensive test of "return" statement location. X-Git-Tag: 0.1.8~3 X-Git-Url: http://git.kaiwu.me/postgresql/log/contrib/postgres_fdw/static/gitweb.js?a=commitdiff_plain;h=9992a4b19de740276015de74c178039011596d3c;p=njs.git Comprehensive test of "return" statement location. Found with afl-fuzz. --- diff --git a/njs/njs_parser.c b/njs/njs_parser.c index 5891f417..4fdbb173 100644 --- a/njs/njs_parser.c +++ b/njs/njs_parser.c @@ -697,14 +697,20 @@ njs_parser_function_lambda(njs_vm_t *vm, njs_function_lambda_t *lambda, static njs_token_t njs_parser_return_statement(njs_vm_t *vm, njs_parser_t *parser) { - njs_token_t token; - njs_parser_node_t *node; + njs_token_t token; + njs_parser_node_t *node; + njs_parser_scope_t *scope; - if (parser->scope->type == NJS_SCOPE_GLOBAL) { - nxt_alert(&vm->trace, NXT_LEVEL_ERROR, - "SyntaxError: Illegal return statement"); + for (scope = parser->scope; + scope->type != NJS_SCOPE_FUNCTION; + scope = scope->parent) + { + if (scope->type == NJS_SCOPE_GLOBAL) { + nxt_alert(&vm->trace, NXT_LEVEL_ERROR, + "SyntaxError: Illegal return statement"); - return NXT_ERROR; + return NXT_ERROR; + } } node = njs_parser_node_alloc(vm); diff --git a/njs/test/njs_unit_test.c b/njs/test/njs_unit_test.c index 95363aee..f8e0471c 100644 --- a/njs/test/njs_unit_test.c +++ b/njs/test/njs_unit_test.c @@ -3976,6 +3976,9 @@ static njs_unit_test_t njs_test[] = { nxt_string("return"), nxt_string("SyntaxError: Illegal return statement in 1") }, + { nxt_string("{return}"), + nxt_string("SyntaxError: Illegal return statement in 1") }, + { nxt_string("function f() { return f() } f()"), nxt_string("RangeError: Maximum call stack size exceeded") },