[njs] Added njs_vm_value_string_create() and njs_vm_value_string_create_chb().

Dmitry Volyntsev xeioex at nginx.com
Thu Jan 26 05:59:58 UTC 2023


details:   https://hg.nginx.org/njs/rev/742347841e34
branches:  
changeset: 2027:742347841e34
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Fri Jan 20 20:15:03 2023 -0800
description:
Added njs_vm_value_string_create() and njs_vm_value_string_create_chb().

Unlike njs_vm_value_string_set() the new functions always check the
input string for a valid UTF8 and calculate UTF8 character length by
themselves.

diffstat:

 src/njs.h                |   4 ++++
 src/njs_chb.h            |   2 +-
 src/njs_json.c           |  11 +++--------
 src/njs_regexp.c         |  18 +++---------------
 src/njs_string.c         |  46 ++++++++++++++++++++++++++++++++--------------
 src/njs_string.h         |   2 ++
 src/njs_vm.c             |  16 ++++++++++++++++
 src/test/njs_unit_test.c |  15 ---------------
 8 files changed, 61 insertions(+), 53 deletions(-)

diffs (255 lines):

diff -r 8b9414a6e557 -r 742347841e34 src/njs.h
--- a/src/njs.h	Wed Jan 18 18:33:23 2023 -0800
+++ b/src/njs.h	Fri Jan 20 20:15:03 2023 -0800
@@ -411,6 +411,10 @@ NJS_EXPORT njs_int_t njs_vm_value_string
     const u_char *start, uint32_t size);
 NJS_EXPORT u_char *njs_vm_value_string_alloc(njs_vm_t *vm, njs_value_t *value,
     uint32_t size);
+NJS_EXPORT njs_int_t njs_vm_value_string_create(njs_vm_t *vm,
+    njs_value_t *value, const u_char *start, uint32_t size);
+NJS_EXPORT njs_int_t njs_vm_value_string_create_chb(njs_vm_t *vm,
+    njs_value_t *value, njs_chb_t *chain);
 NJS_EXPORT njs_int_t njs_vm_value_string_copy(njs_vm_t *vm, njs_str_t *retval,
     njs_value_t *value, uintptr_t *next);
 NJS_EXPORT njs_int_t njs_vm_string_compare(const njs_value_t *v1,
diff -r 8b9414a6e557 -r 742347841e34 src/njs_chb.h
--- a/src/njs_chb.h	Wed Jan 18 18:33:23 2023 -0800
+++ b/src/njs_chb.h	Fri Jan 20 20:15:03 2023 -0800
@@ -105,7 +105,7 @@ njs_chb_utf8_length(njs_chb_t *chain)
     while (n != NULL) {
         len = njs_utf8_length(n->start, njs_chb_node_size(n));
         if (njs_slow_path(len < 0)) {
-            return 0;
+            return -1;
         }
 
         length += len;
diff -r 8b9414a6e557 -r 742347841e34 src/njs_json.c
--- a/src/njs_json.c	Wed Jan 18 18:33:23 2023 -0800
+++ b/src/njs_json.c	Fri Jan 20 20:15:03 2023 -0800
@@ -1090,8 +1090,7 @@ static njs_int_t
 njs_json_stringify_iterator(njs_vm_t *vm, njs_json_stringify_t *stringify,
     njs_value_t *object)
 {
-    u_char            *p;
-    int64_t           size, length;
+    int64_t           size;
     njs_int_t         ret;
     njs_chb_t         chain;
     njs_value_t       *key, *value, index, wrapper;
@@ -1224,16 +1223,12 @@ done:
         goto release;
     }
 
-    length = njs_chb_utf8_length(&chain);
-
-    p = njs_string_alloc(vm, &vm->retval, size, length);
-    if (njs_slow_path(p == NULL)) {
+    ret = njs_string_create_chb(vm, &vm->retval, &chain);
+    if (njs_slow_path(ret != NJS_OK)) {
         njs_chb_destroy(&chain);
         goto memory_error;
     }
 
-    njs_chb_join_to(&chain, p);
-
 release:
 
     njs_chb_destroy(&chain);
diff -r 8b9414a6e557 -r 742347841e34 src/njs_regexp.c
--- a/src/njs_regexp.c	Wed Jan 18 18:33:23 2023 -0800
+++ b/src/njs_regexp.c	Fri Jan 20 20:15:03 2023 -0800
@@ -1197,8 +1197,7 @@ static njs_int_t
 njs_regexp_prototype_symbol_replace(njs_vm_t *vm, njs_value_t *args,
     njs_uint_t nargs, njs_index_t unused)
 {
-    u_char             *p;
-    int64_t            n, last_index, ncaptures, pos, next_pos, size, length;
+    int64_t            n, last_index, ncaptures, pos, next_pos, length;
     njs_str_t          rep, m;
     njs_int_t          ret;
     njs_arr_t          results;
@@ -1455,23 +1454,12 @@ njs_regexp_prototype_symbol_replace(njs_
         njs_chb_append(&chain, &s.start[next_pos], s.size - next_pos);
     }
 
-    size = njs_chb_size(&chain);
-    if (njs_slow_path(size < 0)) {
-        njs_memory_error(vm);
+    ret = njs_string_create_chb(vm, &vm->retval, &chain);
+    if (njs_slow_path(ret != NJS_OK)) {
         ret = NJS_ERROR;
         goto exception;
     }
 
-    length = njs_chb_utf8_length(&chain);
-
-    p = njs_string_alloc(vm, &vm->retval, size, length);
-    if (njs_slow_path(p == NULL)) {
-        ret = NJS_ERROR;
-        goto exception;
-    }
-
-    njs_chb_join_to(&chain, p);
-
     ret = NJS_OK;
 
 exception:
diff -r 8b9414a6e557 -r 742347841e34 src/njs_string.c
--- a/src/njs_string.c	Wed Jan 18 18:33:23 2023 -0800
+++ b/src/njs_string.c	Fri Jan 20 20:15:03 2023 -0800
@@ -147,6 +147,35 @@ njs_string_create(njs_vm_t *vm, njs_valu
 
 
 njs_int_t
+njs_string_create_chb(njs_vm_t *vm, njs_value_t *value, njs_chb_t *chain)
+{
+    u_char   *p;
+    ssize_t  size, length;
+
+    size = njs_chb_size(chain);
+    if (njs_slow_path(size < 0)) {
+        njs_memory_error(vm);
+        return NJS_ERROR;
+    }
+
+    length = njs_chb_utf8_length(chain);
+    if (njs_slow_path(length < 0)) {
+        njs_memory_error(vm);
+        return NJS_ERROR;
+    }
+
+    p = njs_string_alloc(vm, value, size, length);
+    if (njs_slow_path(p == NULL)) {
+        return NJS_ERROR;
+    }
+
+    njs_chb_join_to(chain, p);
+
+    return NJS_OK;
+}
+
+
+njs_int_t
 njs_string_new(njs_vm_t *vm, njs_value_t *value, const u_char *start,
     uint32_t size, uint32_t length)
 {
@@ -3489,7 +3518,7 @@ njs_string_get_substitution(njs_vm_t *vm
     njs_value_t *string, int64_t pos, njs_value_t *captures, int64_t ncaptures,
     njs_value_t *groups, njs_value_t *replacement, njs_value_t *retval)
 {
-    int64_t      tail, size, length, n;
+    int64_t      tail, n;
     u_char       c, c2, *p, *r, *end;
     njs_str_t    rep, m, str, cap;
     njs_int_t    ret;
@@ -3620,23 +3649,12 @@ njs_string_get_substitution(njs_vm_t *vm
 
 done:
 
-    size = njs_chb_size(&chain);
-    if (njs_slow_path(size < 0)) {
-        njs_memory_error(vm);
+    ret = njs_string_create_chb(vm, retval, &chain);
+    if (njs_slow_path(ret != NJS_OK)) {
         ret = NJS_ERROR;
         goto exception;
     }
 
-    length = njs_chb_utf8_length(&chain);
-
-    p = njs_string_alloc(vm, retval, size, length);
-    if (njs_slow_path(p == NULL)) {
-        ret = NJS_ERROR;
-        goto exception;
-    }
-
-    njs_chb_join_to(&chain, p);
-
     ret = NJS_OK;
 
 exception:
diff -r 8b9414a6e557 -r 742347841e34 src/njs_string.h
--- a/src/njs_string.h	Wed Jan 18 18:33:23 2023 -0800
+++ b/src/njs_string.h	Fri Jan 20 20:15:03 2023 -0800
@@ -200,6 +200,8 @@ njs_int_t njs_string_new(njs_vm_t *vm, n
     uint32_t size, uint32_t length);
 njs_int_t njs_string_create(njs_vm_t *vm, njs_value_t *value, const char *src,
     size_t size);
+njs_int_t njs_string_create_chb(njs_vm_t *vm, njs_value_t *value,
+    njs_chb_t *chain);
 
 void njs_encode_hex(njs_str_t *dst, const njs_str_t *src);
 size_t njs_encode_hex_length(const njs_str_t *src, size_t *out_size);
diff -r 8b9414a6e557 -r 742347841e34 src/njs_vm.c
--- a/src/njs_vm.c	Wed Jan 18 18:33:23 2023 -0800
+++ b/src/njs_vm.c	Fri Jan 20 20:15:03 2023 -0800
@@ -832,6 +832,22 @@ njs_vm_value_string_alloc(njs_vm_t *vm, 
 }
 
 
+njs_int_t
+njs_vm_value_string_create(njs_vm_t *vm, njs_value_t *value,
+    const u_char *start, uint32_t size)
+{
+    return njs_string_create(vm, value, (const char *) start, size);
+}
+
+
+njs_int_t
+njs_vm_value_string_create_chb(njs_vm_t *vm, njs_value_t *value,
+    njs_chb_t *chain)
+{
+    return njs_string_create_chb(vm, value, chain);
+}
+
+
 njs_function_t *
 njs_vm_function(njs_vm_t *vm, const njs_str_t *path)
 {
diff -r 8b9414a6e557 -r 742347841e34 src/test/njs_unit_test.c
--- a/src/test/njs_unit_test.c	Wed Jan 18 18:33:23 2023 -0800
+++ b/src/test/njs_unit_test.c	Fri Jan 20 20:15:03 2023 -0800
@@ -8984,9 +8984,6 @@ static njs_unit_test_t  njs_test[] =
     { njs_str("'abc'.replace(/b/g, '$0')"),
       njs_str("a$0c") },
 
-    { njs_str("typeof String.bytesFrom(Array(15).fill(0xE3)).replace(/^/g, 1)"),
-      njs_str("string") },
-
     { njs_str("'abc'.replace(/^/g, '|$&|')"),
       njs_str("||abc") },
 
@@ -9060,12 +9057,6 @@ static njs_unit_test_t  njs_test[] =
               "r[Symbol.replace]('foo', function() {m = arguments[0]}); [m, typeof m]"),
       njs_str("undefined,string") },
 
-    { njs_str("String.bytesFrom([253,242,141,10]).replace(/\\s/g, 'X')[3]"),
-      njs_str("X") },
-
-    { njs_str("String.bytesFrom([255,149,15,97,95]).replace(/_/g, 'X')[4]"),
-      njs_str("X") },
-
     { njs_str("var a = [];"
               "a[2] = '';"
               "var re = /any_regexp/;"
@@ -9271,9 +9262,6 @@ static njs_unit_test_t  njs_test[] =
                  "a +' '+ a.length"),
       njs_str("αα 4") },
 
-    { njs_str("typeof String.bytesFrom(Array(15).fill(0xE3)).match(/^/g)"),
-      njs_str("object") },
-
     { njs_str("'abc'.split()"),
       njs_str("abc") },
 
@@ -18082,9 +18070,6 @@ static njs_unit_test_t  njs_test[] =
     { njs_str("JSON.stringify('\\u00CE\\u00B1\\u00C2\\u00B6'.toBytes())"),
       njs_str("\"α¶\"") },
 
-    { njs_str("JSON.stringify('µ§±®'.toBytes())"),
-      njs_str("\"\xB5\xA7\xB1\xAE\"") },
-
     /* Optional arguments. */
 
     { njs_str("JSON.stringify(undefined, undefined, 1)"),


More information about the nginx-devel mailing list