From 2017c344b2ccf57e5d525317f646ad8a2b800121 Mon Sep 17 00:00:00 2001 From: Igor Sysoev Date: Mon, 8 Feb 2016 18:18:24 +0300 Subject: [PATCH] Segfault has been fixed when "if" operator has an empty "then" branch. --- njs/njs_generator.c | 2 +- njs/test/njs_unit_test.c | 23 ++++++++++++++++++++--- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/njs/njs_generator.c b/njs/njs_generator.c index 305521c0..f5680cf9 100644 --- a/njs/njs_generator.c +++ b/njs/njs_generator.c @@ -361,7 +361,7 @@ njs_generate_if_statement(njs_vm_t *vm, njs_parser_t *parser, previous = (u_char *) cond_jump; label = &cond_jump->offset; - if (node->right->token == NJS_TOKEN_BRANCHING) { + if (node->right != NULL && node->right->token == NJS_TOKEN_BRANCHING) { /* The "then" branch in a case of "if/then/else" statement. */ diff --git a/njs/test/njs_unit_test.c b/njs/test/njs_unit_test.c index 176d30d3..ceb17dda 100644 --- a/njs/test/njs_unit_test.c +++ b/njs/test/njs_unit_test.c @@ -1387,13 +1387,30 @@ static njs_unit_test_t njs_test[] = { nxt_string("var a \n if (!a) a = 3; a"), nxt_string("3") }, - { nxt_string("a = 1; if (true) a = 2; else a = 3; a"), + /* if. */ + + { nxt_string("if (0);"), + nxt_string("undefined") }, + + { nxt_string("if (0) {}"), + nxt_string("undefined") }, + + { nxt_string("if (0);else;"), + nxt_string("undefined") }, + + { nxt_string("var a = 1; if (true); else a = 2; a"), + nxt_string("1") }, + + { nxt_string("var a = 1; if (false); else a = 2; a"), + nxt_string("2") }, + + { nxt_string("var a = 1; if (true) a = 2; else a = 3; a"), nxt_string("2") }, - { nxt_string("a = 3; if (true) if (false); else a = 2; a"), + { nxt_string("var a = 3; if (true) if (false); else a = 2; a"), nxt_string("2") }, - { nxt_string("a = 3; if (true) if (false); else; a = 2; a"), + { nxt_string("var a = 3; if (true) if (false); else; a = 2; a"), nxt_string("2") }, /* switch. */ -- 2.47.3