[njs] Fixed template literal from producing byte-strings.

Dmitry Volyntsev xeioex at nginx.com
Thu Jun 9 07:07:57 UTC 2022


details:   https://hg.nginx.org/njs/rev/d4cdb9085e4d
branches:  
changeset: 1880:d4cdb9085e4d
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Wed Jun 08 21:06:16 2022 -0700
description:
Fixed template literal from producing byte-strings.

Previously, as a side effect of creating a key for the values hash a
byte-string was created.  This byte-string was reused internally and
might appear in template literal.  As a result a byte-string was
produced as a value for a template literal.  Byte-strings are obsolete
and are scheduled for removal because they can cause issues with
internal routines not prepared for them.

diffstat:

 src/njs_generator.c      |  10 ++++++++--
 src/test/njs_unit_test.c |   4 ++++
 2 files changed, 12 insertions(+), 2 deletions(-)

diffs (41 lines):

diff -r cf267407eabe -r d4cdb9085e4d src/njs_generator.c
--- a/src/njs_generator.c	Tue Jun 07 21:30:57 2022 -0700
+++ b/src/njs_generator.c	Wed Jun 08 21:06:16 2022 -0700
@@ -4756,6 +4756,7 @@ static njs_int_t
 njs_generate_global_reference(njs_vm_t *vm, njs_generator_t *generator,
     njs_parser_node_t *node, njs_bool_t exception)
 {
+    ssize_t                  length;
     njs_int_t                ret;
     njs_index_t              index;
     njs_value_t              property;
@@ -4783,8 +4784,13 @@ njs_generate_global_reference(njs_vm_t *
         return NJS_ERROR;
     }
 
-    ret = njs_string_set(vm, &property, lex_entry->name.start,
-                         lex_entry->name.length);
+    length = njs_utf8_length(lex_entry->name.start, lex_entry->name.length);
+    if (njs_slow_path(length < 0)) {
+        return NJS_ERROR;
+    }
+
+    ret = njs_string_new(vm, &property, lex_entry->name.start,
+                         lex_entry->name.length, length);
     if (njs_slow_path(ret != NJS_OK)) {
         return NJS_ERROR;
     }
diff -r cf267407eabe -r d4cdb9085e4d src/test/njs_unit_test.c
--- a/src/test/njs_unit_test.c	Tue Jun 07 21:30:57 2022 -0700
+++ b/src/test/njs_unit_test.c	Wed Jun 08 21:06:16 2022 -0700
@@ -7174,6 +7174,10 @@ static njs_unit_test_t  njs_test[] =
     { njs_str("`\\${a}bc"),
       njs_str("SyntaxError: Unterminated template literal in 1") },
 
+    { njs_str("var v = undefined; var u8 = 'α';"
+              "[`undefined${u8}`.length, `undefineQ${u8}`.length]"),
+      njs_str("10,10") },
+
     { njs_str("`text1\ntext2`;"),
       njs_str("text1\ntext2") },
 


More information about the nginx-devel mailing list