[njs] Comprehensive test of "return" statement location.
Igor Sysoev
igor at sysoev.ru
Mon Jan 16 14:51:53 UTC 2017
details: http://hg.nginx.org/njs/rev/6cc16ad934a4
branches:
changeset: 301:6cc16ad934a4
user: Igor Sysoev <igor at sysoev.ru>
date: Mon Jan 16 17:32:10 2017 +0300
description:
Comprehensive test of "return" statement location.
Found with afl-fuzz.
diffstat:
njs/njs_parser.c | 22 ++++++++++++++--------
njs/test/njs_unit_test.c | 3 +++
2 files changed, 17 insertions(+), 8 deletions(-)
diffs (45 lines):
diff -r 07dc20c1206d -r 6cc16ad934a4 njs/njs_parser.c
--- a/njs/njs_parser.c Mon Jan 16 14:51:29 2017 +0300
+++ b/njs/njs_parser.c Mon Jan 16 17:32:10 2017 +0300
@@ -697,14 +697,20 @@ njs_parser_function_lambda(njs_vm_t *vm,
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;
-
- if (parser->scope->type == NJS_SCOPE_GLOBAL) {
- nxt_alert(&vm->trace, NXT_LEVEL_ERROR,
- "SyntaxError: Illegal return statement");
-
- return NXT_ERROR;
+ njs_token_t token;
+ njs_parser_node_t *node;
+ njs_parser_scope_t *scope;
+
+ 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;
+ }
}
node = njs_parser_node_alloc(vm);
diff -r 07dc20c1206d -r 6cc16ad934a4 njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c Mon Jan 16 14:51:29 2017 +0300
+++ b/njs/test/njs_unit_test.c Mon Jan 16 17:32:10 2017 +0300
@@ -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") },
More information about the nginx-devel
mailing list