From: Dmitry Volyntsev Date: Thu, 9 Jun 2022 04:06:16 +0000 (-0700) Subject: Fixed template literal from producing byte-strings. X-Git-Tag: 0.7.5~12 X-Git-Url: http://www.kaiwu.me/postgresql/commit/?a=commitdiff_plain;h=2e84f1ae83b1c85fff4c36a42bbe27ad12a3dd37;p=njs.git 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. --- diff --git a/src/njs_generator.c b/src/njs_generator.c index db108929..6ba3c067 100644 --- a/src/njs_generator.c +++ b/src/njs_generator.c @@ -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 *vm, njs_generator_t *generator, 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 --git a/src/test/njs_unit_test.c b/src/test/njs_unit_test.c index e7006761..9c19745a 100644 --- a/src/test/njs_unit_test.c +++ b/src/test/njs_unit_test.c @@ -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") },