[njs] Fixed break label for if statement.

Vadim Zhestikov v.zhestikov at f5.com
Thu Oct 20 23:42:43 UTC 2022


details:   https://hg.nginx.org/njs/rev/dc6bc4de1185
branches:  
changeset: 1978:dc6bc4de1185
user:      Vadim Zhestikov <v.zhestikov at f5.com>
date:      Thu Oct 20 16:40:35 2022 -0700
description:
Fixed break label for if statement.

This fixes #591 issue on Github.

diffstat:

 src/njs_generator.c      |  10 ++++++++++
 src/test/njs_unit_test.c |   6 ++++++
 2 files changed, 16 insertions(+), 0 deletions(-)

diffs (43 lines):

diff -r 0b87c0309b37 -r dc6bc4de1185 src/njs_generator.c
--- a/src/njs_generator.c	Mon Oct 10 19:01:56 2022 -0700
+++ b/src/njs_generator.c	Thu Oct 20 16:40:35 2022 -0700
@@ -1127,6 +1127,14 @@ static njs_int_t
 njs_generate_if_statement(njs_vm_t *vm, njs_generator_t *generator,
     njs_parser_node_t *node)
 {
+    njs_int_t  ret;
+
+    ret = njs_generate_start_block(vm, generator, NJS_GENERATOR_BLOCK,
+                                    &node->name);
+    if (njs_slow_path(ret != NJS_OK)) {
+        return ret;
+    }
+
     /* The condition expression. */
 
     njs_generator_next(generator, njs_generate, node->left);
@@ -1231,6 +1239,8 @@ njs_generate_if_statement_else(njs_vm_t 
 
     njs_code_set_offset(generator, label_offset, *jump_offset);
 
+    njs_generate_patch_block_exit(vm, generator);
+
     return njs_generator_stack_pop(vm, generator, generator->context);
 }
 
diff -r 0b87c0309b37 -r dc6bc4de1185 src/test/njs_unit_test.c
--- a/src/test/njs_unit_test.c	Mon Oct 10 19:01:56 2022 -0700
+++ b/src/test/njs_unit_test.c	Thu Oct 20 16:40:35 2022 -0700
@@ -3188,6 +3188,12 @@ static njs_unit_test_t  njs_test[] =
       njs_str("1") },
 #endif
 
+    { njs_str("var r='ok'; a:if(1){break a; r='!ok'}; r"),
+      njs_str("ok") },
+
+    { njs_str("var r='ok'; a:if(0){break a; r='!ok1'} else {break a; r='!ok2'}; r"),
+      njs_str("ok") },
+
     { njs_str("var a = 0; a:{a++}; a"),
       njs_str("1") },
 



More information about the nginx-devel mailing list