[njs] Change: removed byte strings API.

Dmitry Volyntsev xeioex at nginx.com
Sat May 18 04:55:26 UTC 2024


details:   https://hg.nginx.org/njs/rev/4e0553f7ea68
branches:  
changeset: 2329:4e0553f7ea68
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Fri May 17 21:54:50 2024 -0700
description:
Change: removed byte strings API.

These functions are unsafe because they produce byte strings.
Byte strings may not work as expected with the existing JS methods.

The following functions were removed:
    - njs_vm_value_string_set() use njs_vm_value_string_create() as a
    drop-in replacement.
    - njs_vm_value_string_alloc() use njs_chb_t and
    njs_vm_value_string_create_chb() instead.

This fixes #710 on Github.

diffstat:

 external/njs_query_string_module.c |    2 +-
 external/njs_shell.c               |    4 +-
 external/njs_webcrypto_module.c    |   54 ++++++------
 external/njs_xml_module.c          |   20 ++--
 nginx/ngx_http_js_module.c         |  150 ++++++++++++++----------------------
 nginx/ngx_js.c                     |   34 ++++----
 nginx/ngx_js.h                     |    2 +-
 nginx/ngx_js_fetch.c               |   32 +++---
 nginx/ngx_js_shared_dict.c         |   25 +++--
 nginx/ngx_stream_js_module.c       |    4 +-
 src/njs.h                          |    8 -
 src/njs_string.c                   |    2 +-
 src/njs_vm.c                       |   19 +----
 src/test/njs_benchmark.c           |   12 +-
 src/test/njs_externals_test.c      |   38 ++++----
 src/test/njs_unit_test.c           |   32 +++---
 16 files changed, 193 insertions(+), 245 deletions(-)

diffs (truncated from 1114 to 1000 lines):

diff -r dec46ad52e9a -r 4e0553f7ea68 external/njs_query_string_module.c
--- a/external/njs_query_string_module.c	Fri May 17 21:54:49 2024 -0700
+++ b/external/njs_query_string_module.c	Fri May 17 21:54:50 2024 -0700
@@ -694,7 +694,7 @@ njs_query_string_stringify(njs_vm_t *vm,
     object = njs_arg(args, nargs, 1);
 
     if (njs_slow_path(!njs_value_is_object(object))) {
-        njs_vm_value_string_set(vm, retval, (u_char *) "", 0);
+        njs_vm_value_string_create(vm, retval, (u_char *) "", 0);
         return NJS_OK;
     }
 
diff -r dec46ad52e9a -r 4e0553f7ea68 external/njs_shell.c
--- a/external/njs_shell.c	Fri May 17 21:54:49 2024 -0700
+++ b/external/njs_shell.c	Fri May 17 21:54:50 2024 -0700
@@ -1579,8 +1579,8 @@ njs_engine_njs_complete(njs_engine_t *en
 
         while (p < end && *p != '.') { p++; }
 
-        ret = njs_vm_value_string_set(vm, njs_value_arg(&key), start,
-                                      p - start);
+        ret = njs_vm_value_string_create(vm, njs_value_arg(&key), start,
+                                         p - start);
         if (njs_slow_path(ret != NJS_OK)) {
             return NULL;
         }
diff -r dec46ad52e9a -r 4e0553f7ea68 external/njs_webcrypto_module.c
--- a/external/njs_webcrypto_module.c	Fri May 17 21:54:49 2024 -0700
+++ b/external/njs_webcrypto_module.c	Fri May 17 21:54:50 2024 -0700
@@ -1855,7 +1855,7 @@ njs_export_jwk_rsa(njs_vm_t *vm, njs_web
         return NJS_ERROR;
     }
 
-    njs_vm_value_string_set(vm, njs_value_arg(&rsa_s), (u_char *) "RSA", 3);
+    njs_vm_value_string_create(vm, njs_value_arg(&rsa_s), (u_char *) "RSA", 3);
 
     ret = njs_vm_object_prop_set(vm, retval, &string_kty, &rsa_s);
     if (ret != NJS_OK) {
@@ -1909,8 +1909,8 @@ njs_export_jwk_rsa(njs_vm_t *vm, njs_web
 
     nm = &njs_webcrypto_alg_name[key->alg->type][key->hash];
 
-    (void) njs_vm_value_string_set(vm, njs_value_arg(&alg), nm->start,
-                                   nm->length);
+    (void) njs_vm_value_string_create(vm, njs_value_arg(&alg), nm->start,
+                                      nm->length);
 
     return njs_vm_object_prop_set(vm, retval, &string_alg, &alg);
 }
@@ -1975,8 +1975,8 @@ njs_export_jwk_ec(njs_vm_t *vm, njs_webc
     nid = EC_GROUP_get_curve_name(group);
 
     cname = njs_algorithm_curve_name(nid);
-    (void) njs_vm_value_string_set(vm, njs_value_arg(&name),
-                                   cname->start, cname->length);
+    (void) njs_vm_value_string_create(vm, njs_value_arg(&name),
+                                      cname->start, cname->length);
 
     if (cname->length == 0) {
         njs_vm_type_error(vm, "Unsupported JWK EC curve: %s", OBJ_nid2sn(nid));
@@ -1988,7 +1988,7 @@ njs_export_jwk_ec(njs_vm_t *vm, njs_webc
         goto fail;
     }
 
-    njs_vm_value_string_set(vm, njs_value_arg(&ec_s), (u_char *) "EC", 2);
+    njs_vm_value_string_create(vm, njs_value_arg(&ec_s), (u_char *) "EC", 2);
 
     ret = njs_vm_object_prop_set(vm, retval, &string_kty, &ec_s);
     if (ret != NJS_OK) {
@@ -2154,8 +2154,8 @@ njs_export_jwk_oct(njs_vm_t *vm, njs_web
 
     if (key->alg->type == NJS_ALGORITHM_HMAC) {
         nm = &njs_webcrypto_alg_name[type][key->hash];
-        (void) njs_vm_value_string_set(vm, njs_value_arg(&alg), nm->start,
-                                       nm->length);
+        (void) njs_vm_value_string_create(vm, njs_value_arg(&alg), nm->start,
+                                          nm->length);
 
     } else {
         switch (key->u.s.raw.length) {
@@ -2164,8 +2164,8 @@ njs_export_jwk_oct(njs_vm_t *vm, njs_web
         case 32:
             nm = &njs_webcrypto_alg_aes_name
                  [type - NJS_ALGORITHM_AES_GCM][(key->u.s.raw.length - 16) / 8];
-            (void) njs_vm_value_string_set(vm, njs_value_arg(&alg), nm->start,
-                                           nm->length);
+            (void) njs_vm_value_string_create(vm, njs_value_arg(&alg),
+                                              nm->start, nm->length);
             break;
 
         default:
@@ -2186,7 +2186,7 @@ njs_export_jwk_oct(njs_vm_t *vm, njs_web
         return NJS_ERROR;
     }
 
-    njs_vm_value_string_set(vm, njs_value_arg(&oct_s), (u_char *) "oct", 3);
+    njs_vm_value_string_create(vm, njs_value_arg(&oct_s), (u_char *) "oct", 3);
 
     ret = njs_vm_object_prop_set(vm, retval, &string_kty, &oct_s);
     if (ret != NJS_OK) {
@@ -4150,14 +4150,14 @@ njs_key_ext_algorithm(njs_vm_t *vm, njs_
     }
 
     name = &njs_webcrypto_alg[key->alg->type].name;
-    ret = njs_vm_value_string_set(vm, njs_value_arg(&alg), name->start,
-                                  name->length);
+    ret = njs_vm_value_string_create(vm, njs_value_arg(&alg), name->start,
+                                     name->length);
     if (njs_slow_path(ret != NJS_OK)) {
         return NJS_ERROR;
     }
 
-    (void) njs_vm_value_string_set(vm, njs_value_arg(&name_s),
-                                   (u_char *) "name", njs_length("name"));
+    (void) njs_vm_value_string_create(vm, njs_value_arg(&name_s),
+                                     (u_char *) "name", njs_length("name"));
 
     ret = njs_vm_object_alloc(vm, retval, &name_s, &alg, NULL);
     if (njs_slow_path(ret != NJS_OK)) {
@@ -4203,8 +4203,8 @@ njs_key_ext_algorithm(njs_vm_t *vm, njs_
         }
 
         name = njs_algorithm_hash_name(key->hash);
-        ret = njs_vm_value_string_set(vm, njs_value_arg(&hash), name->start,
-                                      name->length);
+        ret = njs_vm_value_string_create(vm, njs_value_arg(&hash), name->start,
+                                         name->length);
         if (njs_slow_path(ret != NJS_OK)) {
             return NJS_ERROR;
         }
@@ -4252,8 +4252,8 @@ njs_key_ext_algorithm(njs_vm_t *vm, njs_
 
         name = njs_algorithm_curve_name(EC_GROUP_get_curve_name(group));
 
-        ret = njs_vm_value_string_set(vm, njs_value_arg(&val), name->start,
-                                      name->length);
+        ret = njs_vm_value_string_create(vm, njs_value_arg(&val), name->start,
+                                         name->length);
         if (njs_slow_path(ret != NJS_OK)) {
             return NJS_ERROR;
         }
@@ -4270,8 +4270,8 @@ njs_key_ext_algorithm(njs_vm_t *vm, njs_
         /* HmacKeyGenParams */
 
         name = njs_algorithm_hash_name(key->hash);
-        ret = njs_vm_value_string_set(vm, njs_value_arg(&val), name->start,
-                                      name->length);
+        ret = njs_vm_value_string_create(vm, njs_value_arg(&val), name->start,
+                                         name->length);
         if (njs_slow_path(ret != NJS_OK)) {
             return NJS_ERROR;
         }
@@ -4320,12 +4320,12 @@ njs_key_ext_type(njs_vm_t *vm, njs_objec
     }
 
     if (key->alg->raw) {
-        (void) njs_vm_value_string_set(vm, retval, (u_char *) "secret",
-                                       njs_length("secret"));
+        (void) njs_vm_value_string_create(vm, retval, (u_char *) "secret",
+                                          njs_length("secret"));
     } else {
         type = key->u.a.privat ? "private": "public";
-        (void) njs_vm_value_string_set(vm, retval, (u_char *) type,
-                                       key->u.a.privat ? 7 : 6);
+        (void) njs_vm_value_string_create(vm, retval, (u_char *) type,
+                                          key->u.a.privat ? 7 : 6);
     }
 
     return NJS_OK;
@@ -4544,8 +4544,8 @@ njs_key_ops(njs_vm_t *vm, njs_value_t *r
                 return NJS_ERROR;
             }
 
-            ret = njs_vm_value_string_set(vm, value, e->name.start,
-                                          e->name.length);
+            ret = njs_vm_value_string_create(vm, value, e->name.start,
+                                             e->name.length);
             if (ret != NJS_OK) {
                 return NJS_ERROR;
             }
diff -r dec46ad52e9a -r 4e0553f7ea68 external/njs_xml_module.c
--- a/external/njs_xml_module.c	Fri May 17 21:54:49 2024 -0700
+++ b/external/njs_xml_module.c	Fri May 17 21:54:50 2024 -0700
@@ -584,8 +584,8 @@ njs_xml_node_ext_prop_keys(njs_vm_t *vm,
             return NJS_ERROR;
         }
 
-        ret = njs_vm_value_string_set(vm, push, (u_char *) "$name",
-                                      njs_length("$name"));
+        ret = njs_vm_value_string_create(vm, push, (u_char *) "$name",
+                                         njs_length("$name"));
         if (njs_slow_path(ret != NJS_OK)) {
             return NJS_ERROR;
         }
@@ -597,8 +597,8 @@ njs_xml_node_ext_prop_keys(njs_vm_t *vm,
             return NJS_ERROR;
         }
 
-        ret = njs_vm_value_string_set(vm, push, (u_char *) "$ns",
-                                      njs_length("$ns"));
+        ret = njs_vm_value_string_create(vm, push, (u_char *) "$ns",
+                                         njs_length("$ns"));
         if (njs_slow_path(ret != NJS_OK)) {
             return NJS_ERROR;
         }
@@ -610,8 +610,8 @@ njs_xml_node_ext_prop_keys(njs_vm_t *vm,
             return NJS_ERROR;
         }
 
-        ret = njs_vm_value_string_set(vm, push, (u_char *) "$attrs",
-                                      njs_length("$attrs"));
+        ret = njs_vm_value_string_create(vm, push, (u_char *) "$attrs",
+                                         njs_length("$attrs"));
         if (njs_slow_path(ret != NJS_OK)) {
             return NJS_ERROR;
         }
@@ -623,8 +623,8 @@ njs_xml_node_ext_prop_keys(njs_vm_t *vm,
             return NJS_ERROR;
         }
 
-        ret = njs_vm_value_string_set(vm, push, (u_char *) "$text",
-                                      njs_length("$text"));
+        ret = njs_vm_value_string_create(vm, push, (u_char *) "$text",
+                                         njs_length("$text"));
         if (njs_slow_path(ret != NJS_OK)) {
             return NJS_ERROR;
         }
@@ -640,8 +640,8 @@ njs_xml_node_ext_prop_keys(njs_vm_t *vm,
             return NJS_ERROR;
         }
 
-        ret = njs_vm_value_string_set(vm, push, (u_char *) "$tags",
-                                      njs_length("$tags"));
+        ret = njs_vm_value_string_create(vm, push, (u_char *) "$tags",
+                                         njs_length("$tags"));
         if (njs_slow_path(ret != NJS_OK)) {
             return NJS_ERROR;
         }
diff -r dec46ad52e9a -r 4e0553f7ea68 nginx/ngx_http_js_module.c
--- a/nginx/ngx_http_js_module.c	Fri May 17 21:54:49 2024 -0700
+++ b/nginx/ngx_http_js_module.c	Fri May 17 21:54:50 2024 -0700
@@ -1144,8 +1144,8 @@ ngx_http_js_body_filter(ngx_http_request
 
     njs_value_assign(&arguments[0], &ctx->request);
 
-    njs_vm_value_string_set(ctx->vm, njs_value_arg(&last_key),
-                            last_str.start, last_str.length);
+    njs_vm_value_string_create(ctx->vm, njs_value_arg(&last_key),
+                               last_str.start, last_str.length);
 
     while (in != NULL) {
         ctx->buf = in->buf;
@@ -1474,7 +1474,7 @@ ngx_http_js_ext_keys_header(njs_vm_t *vm
                 return NJS_ERROR;
             }
 
-            rc = njs_vm_value_string_set(vm, value, h->key.data, h->key.len);
+            rc = njs_vm_value_string_create(vm, value, h->key.data, h->key.len);
             if (rc != NJS_OK) {
                 return NJS_ERROR;
             }
@@ -1586,7 +1586,7 @@ ngx_http_js_ext_raw_header(njs_vm_t *vm,
             return NJS_ERROR;
         }
 
-        rc = njs_vm_value_string_set(vm, elem, h->key.data, h->key.len);
+        rc = njs_vm_value_string_create(vm, elem, h->key.data, h->key.len);
         if (rc != NJS_OK) {
             return NJS_ERROR;
         }
@@ -1596,7 +1596,7 @@ ngx_http_js_ext_raw_header(njs_vm_t *vm,
             return NJS_ERROR;
         }
 
-        rc = njs_vm_value_string_set(vm, elem, h->value.data, h->value.len);
+        rc = njs_vm_value_string_create(vm, elem, h->value.data, h->value.len);
         if (rc != NJS_OK) {
             return NJS_ERROR;
         }
@@ -1703,7 +1703,8 @@ ngx_http_js_header_single(njs_vm_t *vm, 
             return NJS_DECLINED;
         }
 
-        rc = njs_vm_value_string_set(vm, retval, h->value.data, h->value.len);
+        rc = njs_vm_value_string_create(vm, retval, h->value.data,
+                                        h->value.len);
         if (rc != NJS_OK) {
             return NJS_ERROR;
         }
@@ -1845,8 +1846,8 @@ ngx_http_js_header_array(njs_vm_t *vm, n
                 return NJS_ERROR;
             }
 
-            rc = njs_vm_value_string_set(vm, value, h->value.data,
-                                         h->value.len);
+            rc = njs_vm_value_string_create(vm, value, h->value.data,
+                                            h->value.len);
             if (rc != NJS_OK) {
                 return NJS_ERROR;
             }
@@ -1934,7 +1935,7 @@ ngx_http_js_header_generic(njs_vm_t *vm,
             return NJS_DECLINED;
         }
 
-        return njs_vm_value_string_set(vm, retval, start, p - start);
+        return njs_vm_value_string_create(vm, retval, start, p - start);
     }
 
     header = part->elts;
@@ -2110,8 +2111,8 @@ ngx_http_js_ext_keys_header_out(njs_vm_t
             return NJS_ERROR;
         }
 
-        rc = njs_vm_value_string_set(vm, value, (u_char *) "Content-Type",
-                                     njs_length("Content-Type"));
+        rc = njs_vm_value_string_create(vm, value, (u_char *) "Content-Type",
+                                        njs_length("Content-Type"));
         if (rc != NJS_OK) {
             return NJS_ERROR;
         }
@@ -2125,8 +2126,8 @@ ngx_http_js_ext_keys_header_out(njs_vm_t
             return NJS_ERROR;
         }
 
-        rc = njs_vm_value_string_set(vm, value, (u_char *) "Content-Length",
-                                     njs_length("Content-Length"));
+        rc = njs_vm_value_string_create(vm, value, (u_char *) "Content-Length",
+                                        njs_length("Content-Length"));
         if (rc != NJS_OK) {
             return NJS_ERROR;
         }
@@ -2573,7 +2574,7 @@ ngx_http_js_ext_get_http_version(njs_vm_
         break;
     }
 
-    return njs_vm_value_string_set(vm, retval, v.data, v.len);
+    return njs_vm_value_string_create(vm, retval, v.data, v.len);
 }
 
 
@@ -2610,8 +2611,8 @@ ngx_http_js_ext_get_remote_address(njs_v
 
     c = r->connection;
 
-    return njs_vm_value_string_set(vm, retval, c->addr_text.data,
-                                   c->addr_text.len);
+    return njs_vm_value_string_create(vm, retval, c->addr_text.data,
+                                      c->addr_text.len);
 }
 
 
@@ -2819,52 +2820,36 @@ static njs_int_t
 ngx_http_js_header_in_array(njs_vm_t *vm, ngx_http_request_t *r,
     ngx_array_t *array, u_char sep, njs_value_t *retval)
 {
-    u_char            *p, *end;
-    size_t             len;
+    njs_chb_t          chain;
+    njs_int_t          ret;
     ngx_uint_t         i, n;
     ngx_table_elt_t  **hh;
 
     n = array->nelts;
     hh = array->elts;
 
-    len = 0;
-
-    for (i = 0; i < n; i++) {
-        len += hh[i]->value.len + 1;
-    }
-
-    if (len == 0) {
+    if (n == 0) {
         njs_value_undefined_set(retval);
         return NJS_DECLINED;
     }
 
-    len -= 1;
-
     if (n == 1) {
-        return njs_vm_value_string_set(vm, retval, (*hh)->value.data,
-                                       (*hh)->value.len);
-    }
-
-    p = njs_vm_value_string_alloc(vm, retval, len);
-    if (p == NULL) {
-        return NJS_ERROR;
-    }
-
-    end = p + len;
-
-
-    for (i = 0; /* void */ ; i++) {
-
-        p = ngx_copy(p, hh[i]->value.data, hh[i]->value.len);
-
-        if (p == end) {
-            break;
-        }
-
-        *p++ = sep;
-    }
-
-    return NJS_OK;
+        return njs_vm_value_string_create(vm, retval, (*hh)->value.data,
+                                              (*hh)->value.len);
+    }
+
+    NJS_CHB_MP_INIT(&chain, vm);
+
+    for (i = 0; i < n; i++) {
+        njs_chb_append(&chain, hh[i]->value.data, hh[i]->value.len);
+        njs_chb_append(&chain, &sep, 1);
+    }
+
+    ret = njs_vm_value_string_create_chb(vm, retval, &chain);
+
+    njs_chb_destroy(&chain);
+
+    return ret;
 }
 #else
 static njs_int_t
@@ -3889,9 +3874,9 @@ ngx_http_js_header_generic(njs_vm_t *vm,
     ngx_list_t *headers, ngx_table_elt_t **ph, unsigned flags, njs_str_t *name,
     njs_value_t *retval)
 {
-    u_char           *p, sep;
-    ssize_t           size;
-    njs_int_t         rc;
+    u_char            sep;
+    njs_chb_t         chain;
+    njs_int_t         rc, ret;
     ngx_uint_t        i;
     njs_value_t      *value;
     ngx_list_part_t  *part;
@@ -3949,8 +3934,8 @@ ngx_http_js_header_generic(njs_vm_t *vm,
                 return NJS_ERROR;
             }
 
-            rc = njs_vm_value_string_set(vm, value, h->value.data,
-                                         h->value.len);
+            rc = njs_vm_value_string_create(vm, value, h->value.data,
+                                            h->value.len);
             if (rc != NJS_OK) {
                 return NJS_ERROR;
             }
@@ -3960,34 +3945,25 @@ ngx_http_js_header_generic(njs_vm_t *vm,
     }
 
     if ((*ph)->next == NULL || flags & NJS_HEADER_SINGLE) {
-        return njs_vm_value_string_set(vm, retval, (*ph)->value.data,
-                                       (*ph)->value.len);
-    }
-
-    size = - (ssize_t) njs_length("; ");
-
-    for (h = *ph; h; h = h->next) {
-        size += h->value.len + njs_length("; ");
-    }
-
-    p = njs_vm_value_string_alloc(vm, retval, size);
-    if (p == NULL) {
-        return NJS_ERROR;
-    }
+        return njs_vm_value_string_create(vm, retval, (*ph)->value.data,
+                                          (*ph)->value.len);
+    }
+
+    NJS_CHB_MP_INIT(&chain, vm);
 
     sep = flags & NJS_HEADER_SEMICOLON ? ';' : ',';
 
     for (h = *ph; h; h = h->next) {
-        p = ngx_copy(p, h->value.data, h->value.len);
-
-        if (h->next == NULL) {
-            break;
-        }
-
-        *p++ = sep; *p++ = ' ';
-    }
-
-    return NJS_OK;
+        njs_chb_append(&chain, h->value.data, h->value.len);
+        njs_chb_append(&chain, &sep, 1);
+        njs_chb_append_literal(&chain, " ");
+    }
+
+    ret = njs_vm_value_string_create_chb(vm, retval, &chain);
+
+    njs_chb_destroy(&chain);
+
+    return ret;
 }
 #endif
 
@@ -4016,7 +3992,7 @@ static njs_int_t
 ngx_http_js_content_length(njs_vm_t *vm, ngx_http_request_t *r,
     unsigned flags, njs_str_t *v, njs_value_t *setval, njs_value_t *retval)
 {
-    u_char           *p, *start;
+    u_char           *p;
     njs_int_t         rc;
     ngx_int_t         n;
     ngx_table_elt_t  *h;
@@ -4028,14 +4004,8 @@ ngx_http_js_content_length(njs_vm_t *vm,
         {
             p = ngx_sprintf(content_len, "%O", r->headers_out.content_length_n);
 
-            start = njs_vm_value_string_alloc(vm, retval, p - content_len);
-            if (start == NULL) {
-                return NJS_ERROR;
-            }
-
-            ngx_memcpy(start, content_len, p - content_len);
-
-            return NJS_OK;
+            return njs_vm_value_string_create(vm, retval, content_len,
+                                              p - content_len);
         }
     }
 
@@ -4084,7 +4054,7 @@ ngx_http_js_content_type(njs_vm_t *vm, n
             return NJS_OK;
         }
 
-        return njs_vm_value_string_set(vm, retval, hdr->data, hdr->len);
+        return njs_vm_value_string_create(vm, retval, hdr->data, hdr->len);
     }
 
     if (setval != NULL && njs_value_is_array(setval)) {
diff -r dec46ad52e9a -r 4e0553f7ea68 nginx/ngx_js.c
--- a/nginx/ngx_js.c	Fri May 17 21:54:49 2024 -0700
+++ b/nginx/ngx_js.c	Fri May 17 21:54:50 2024 -0700
@@ -668,7 +668,7 @@ ngx_js_ext_string(njs_vm_t *vm, njs_obje
 
     field = (ngx_str_t *) (p + njs_vm_prop_magic32(prop));
 
-    return njs_vm_value_string_set(vm, retval, field->data, field->len);
+    return njs_vm_value_string_create(vm, retval, field->data, field->len);
 }
 
 
@@ -745,15 +745,15 @@ njs_int_t
 ngx_js_ext_build(njs_vm_t *vm, njs_object_prop_t *prop, njs_value_t *value,
     njs_value_t *setval, njs_value_t *retval)
 {
-    return njs_vm_value_string_set(vm, retval,
+    return njs_vm_value_string_create(vm, retval,
 #ifdef NGX_BUILD
-                                   (u_char *) NGX_BUILD,
-                                   njs_strlen(NGX_BUILD)
+                                      (u_char *) NGX_BUILD,
+                                      njs_strlen(NGX_BUILD)
 #else
-                                   (u_char *) "",
-                                   0
+                                      (u_char *) "",
+                                      0
 #endif
-                                   );
+                                     );
 }
 
 
@@ -761,8 +761,8 @@ njs_int_t
 ngx_js_ext_conf_file_path(njs_vm_t *vm, njs_object_prop_t *prop,
     njs_value_t *value, njs_value_t *setval, njs_value_t *retval)
 {
-    return njs_vm_value_string_set(vm, retval, ngx_cycle->conf_file.data,
-                                   ngx_cycle->conf_file.len);
+    return njs_vm_value_string_create(vm, retval, ngx_cycle->conf_file.data,
+                                      ngx_cycle->conf_file.len);
 }
 
 
@@ -770,8 +770,8 @@ njs_int_t
 ngx_js_ext_conf_prefix(njs_vm_t *vm, njs_object_prop_t *prop,
     njs_value_t *value, njs_value_t *setval, njs_value_t *retval)
 {
-    return njs_vm_value_string_set(vm, retval, ngx_cycle->conf_prefix.data,
-                                   ngx_cycle->conf_prefix.len);
+    return njs_vm_value_string_create(vm, retval, ngx_cycle->conf_prefix.data,
+                                      ngx_cycle->conf_prefix.len);
 }
 
 
@@ -779,8 +779,8 @@ njs_int_t
 ngx_js_ext_error_log_path(njs_vm_t *vm, njs_object_prop_t *prop,
     njs_value_t *value, njs_value_t *setval, njs_value_t *retval)
 {
-    return njs_vm_value_string_set(vm, retval, ngx_cycle->error_log.data,
-                                   ngx_cycle->error_log.len);
+    return njs_vm_value_string_create(vm, retval, ngx_cycle->error_log.data,
+                                      ngx_cycle->error_log.len);
 }
 
 
@@ -788,8 +788,8 @@ njs_int_t
 ngx_js_ext_prefix(njs_vm_t *vm, njs_object_prop_t *prop, njs_value_t *value,
     njs_value_t *setval, njs_value_t *retval)
 {
-    return njs_vm_value_string_set(vm, retval, ngx_cycle->prefix.data,
-                                   ngx_cycle->prefix.len);
+    return njs_vm_value_string_create(vm, retval, ngx_cycle->prefix.data,
+                                      ngx_cycle->prefix.len);
 }
 
 
@@ -797,8 +797,8 @@ njs_int_t
 ngx_js_ext_version(njs_vm_t *vm, njs_object_prop_t *prop, njs_value_t *value,
     njs_value_t *setval, njs_value_t *retval)
 {
-    return njs_vm_value_string_set(vm, retval, (u_char *) NGINX_VERSION,
-                                   njs_strlen(NGINX_VERSION));
+    return njs_vm_value_string_create(vm, retval, (u_char *) NGINX_VERSION,
+                                      njs_strlen(NGINX_VERSION));
 }
 
 
diff -r dec46ad52e9a -r 4e0553f7ea68 nginx/ngx_js.h
--- a/nginx/ngx_js.h	Fri May 17 21:54:49 2024 -0700
+++ b/nginx/ngx_js.h	Fri May 17 21:54:50 2024 -0700
@@ -175,7 +175,7 @@ struct ngx_js_ctx_s {
 
 
 #define ngx_js_prop(vm, type, value, start, len)                              \
-    ((type == NGX_JS_STRING) ? njs_vm_value_string_set(vm, value, start, len) \
+    ((type == NGX_JS_STRING) ? njs_vm_value_string_create(vm, value, start, len) \
                              : njs_vm_value_buffer_set(vm, value, start, len))
 
 
diff -r dec46ad52e9a -r 4e0553f7ea68 nginx/ngx_js_fetch.c
--- a/nginx/ngx_js_fetch.c	Fri May 17 21:54:49 2024 -0700
+++ b/nginx/ngx_js_fetch.c	Fri May 17 21:54:50 2024 -0700
@@ -3237,8 +3237,8 @@ ngx_headers_js_get(njs_vm_t *vm, njs_val
                 return NJS_ERROR;
             }
 
-            rc = njs_vm_value_string_set(vm, value, ph->value.data,
-                                         ph->value.len);
+            rc = njs_vm_value_string_create(vm, value, ph->value.data,
+                                            ph->value.len);
             if (rc != NJS_OK) {
                 return NJS_ERROR;
             }
@@ -3283,7 +3283,7 @@ ngx_headers_js_get(njs_vm_t *vm, njs_val
         h = h->next;
     }
 
-    return njs_vm_value_string_set(vm, retval, data, p - data);
+    return njs_vm_value_string_create(vm, retval, data, p - data);
 }
 
 
@@ -3574,8 +3574,8 @@ ngx_headers_js_ext_keys(njs_vm_t *vm, nj
                 return NJS_ERROR;
             }
 
-            rc = njs_vm_value_string_set(vm, value, h[i].key.data,
-                                         h[i].key.len);
+            rc = njs_vm_value_string_create(vm, value, h[i].key.data,
+                                            h[i].key.len);
             if (rc != NJS_OK) {
                 return NJS_ERROR;
             }
@@ -3709,9 +3709,9 @@ ngx_request_js_ext_body(njs_vm_t *vm, nj
     case NGX_JS_BODY_JSON:
     case NGX_JS_BODY_TEXT:
     default:
-        ret = njs_vm_value_string_set(vm, njs_value_arg(&result),
-                                      request->body.start,
-                                      request->body.length);
+        ret = njs_vm_value_string_create(vm, njs_value_arg(&result),
+                                         request->body.start,
+                                         request->body.length);
         if (ret != NJS_OK) {
             njs_vm_memory_error(vm);
             return NJS_ERROR;
@@ -3869,8 +3869,8 @@ ngx_response_js_ext_body(njs_vm_t *vm, n
     case NGX_JS_BODY_JSON:
     case NGX_JS_BODY_TEXT:
     default:
-        ret = njs_vm_value_string_set(vm, njs_value_arg(&result),
-                                      string.start, string.length);
+        ret = njs_vm_value_string_create(vm, njs_value_arg(&result),
+                                         string.start, string.length);
         if (ret != NJS_OK) {
             njs_vm_memory_error(vm);
             return NJS_ERROR;
@@ -3985,8 +3985,8 @@ ngx_response_js_ext_status_text(njs_vm_t
         return NJS_DECLINED;
     }
 
-    njs_vm_value_string_set(vm, retval, response->status_text.start,
-                            response->status_text.length);
+    njs_vm_value_string_create(vm, retval, response->status_text.start,
+                               response->status_text.length);
 
     return NJS_OK;
 }
@@ -4004,8 +4004,8 @@ ngx_response_js_ext_type(njs_vm_t *vm, n
         return NJS_DECLINED;
     }
 
-    return njs_vm_value_string_set(vm, retval, (u_char *) "basic",
-                                   njs_length("basic"));
+    return njs_vm_value_string_create(vm, retval, (u_char *) "basic",
+                                      njs_length("basic"));
 }
 
 
@@ -4017,8 +4017,8 @@ ngx_fetch_flag(njs_vm_t *vm, const ngx_j
 
     for (e = entries; e->name.length != 0; e++) {
         if (e->value == value) {
-            return njs_vm_value_string_set(vm, retval, e->name.start,
-                                           e->name.length);
+            return njs_vm_value_string_create(vm, retval, e->name.start,
+                                              e->name.length);
         }
     }
 
diff -r dec46ad52e9a -r 4e0553f7ea68 nginx/ngx_js_shared_dict.c
--- a/nginx/ngx_js_shared_dict.c	Fri May 17 21:54:49 2024 -0700
+++ b/nginx/ngx_js_shared_dict.c	Fri May 17 21:54:50 2024 -0700
@@ -423,8 +423,8 @@ njs_js_ext_global_shared_keys(njs_vm_t *
             return NJS_ERROR;
         }
 
-        rc = njs_vm_value_string_set(vm, value, shm_zone->shm.name.data,
-                                     shm_zone->shm.name.len);
+        rc = njs_vm_value_string_create(vm, value, shm_zone->shm.name.data,
+                                        shm_zone->shm.name.len);
         if (rc != NJS_OK) {
             return NJS_ERROR;
         }
@@ -698,8 +698,8 @@ njs_js_ext_shared_dict_keys(njs_vm_t *vm
             goto fail;
         }
 
-        rc = njs_vm_value_string_set(vm, value, node->sn.str.data,
-                                     node->sn.str.len);
+        rc = njs_vm_value_string_create(vm, value, node->sn.str.data,
+                                        node->sn.str.len);
         if (rc != NJS_OK) {
             goto fail;
         }
@@ -853,8 +853,8 @@ njs_js_ext_shared_dict_items(njs_vm_t *v
             goto fail;
         }
 
-        rc = njs_vm_value_string_set(vm, value, node->sn.str.data,
-                                     node->sn.str.len);
+        rc = njs_vm_value_string_create(vm, value, node->sn.str.data,
+                                        node->sn.str.len);
         if (rc != NJS_OK) {
             goto fail;
         }
@@ -896,8 +896,8 @@ njs_js_ext_shared_dict_name(njs_vm_t *vm
         return NJS_DECLINED;
     }
 
-    return njs_vm_value_string_set(vm, retval, shm_zone->shm.name.data,
-                                   shm_zone->shm.name.len);
+    return njs_vm_value_string_create(vm, retval, shm_zone->shm.name.data,
+                                      shm_zone->shm.name.len);
 }
 
 
@@ -1063,7 +1063,7 @@ njs_js_ext_shared_dict_type(njs_vm_t *vm
         break;
     }
 
-    return njs_vm_value_string_set(vm, retval, type.start, type.length);
+    return njs_vm_value_string_create(vm, retval, type.start, type.length);
 }
 
 
@@ -1406,7 +1406,8 @@ ngx_js_dict_copy_value_locked(njs_vm_t *
             return NGX_ERROR;
         }
 
-        ret = njs_vm_value_string_set(vm, retval, string.start, string.length);
+        ret = njs_vm_value_string_create(vm, retval, string.start,
+                                         string.length);
         if (ret != NJS_OK) {
             return NGX_ERROR;
         }
@@ -1493,8 +1494,8 @@ static njs_int_t
 ngx_js_dict_shared_error_name(njs_vm_t *vm, njs_object_prop_t *prop,
     njs_value_t *value, njs_value_t *setval, njs_value_t *retval)
 {
-    return njs_vm_value_string_set(vm, retval, (u_char *) "SharedMemoryError",
-                                   17);
+    return njs_vm_value_string_create(vm, retval,
+                                      (u_char *) "SharedMemoryError", 17);
 }
 
 
diff -r dec46ad52e9a -r 4e0553f7ea68 nginx/ngx_stream_js_module.c
--- a/nginx/ngx_stream_js_module.c	Fri May 17 21:54:49 2024 -0700
+++ b/nginx/ngx_stream_js_module.c	Fri May 17 21:54:50 2024 -0700
@@ -1241,8 +1241,8 @@ ngx_stream_js_ext_get_remote_address(njs
 
     c = s->connection;
 
-    return njs_vm_value_string_set(vm, retval, c->addr_text.data,
-                                   c->addr_text.len);
+    return njs_vm_value_string_create(vm, retval, c->addr_text.data,
+                                      c->addr_text.len);
 }
 
 
diff -r dec46ad52e9a -r 4e0553f7ea68 src/njs.h
--- a/src/njs.h	Fri May 17 21:54:49 2024 -0700
+++ b/src/njs.h	Fri May 17 21:54:50 2024 -0700
@@ -400,14 +400,6 @@ NJS_EXPORT njs_int_t njs_value_to_intege
 
 /*  Gets string value, no copy. */
 NJS_EXPORT void njs_value_string_get(njs_value_t *value, njs_str_t *dst);
-/*
- * Sets a byte string value.
- *   start data is not copied and should not be freed.
- */
-NJS_EXPORT njs_int_t njs_vm_value_string_set(njs_vm_t *vm, njs_value_t *value,
-    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,
diff -r dec46ad52e9a -r 4e0553f7ea68 src/njs_string.c
--- a/src/njs_string.c	Fri May 17 21:54:49 2024 -0700
+++ b/src/njs_string.c	Fri May 17 21:54:50 2024 -0700
@@ -3152,7 +3152,7 @@ njs_string_get_substitution(njs_vm_t *vm
 
             p += 2;
 
-            ret = njs_vm_value_string_set(vm, &name, p, r - p);
+            ret = njs_string_create(vm, &name, (const char *) p, r - p);
             if (njs_slow_path(ret != NJS_OK)) {
                 goto exception;
             }
diff -r dec46ad52e9a -r 4e0553f7ea68 src/njs_vm.c
--- a/src/njs_vm.c	Fri May 17 21:54:49 2024 -0700
+++ b/src/njs_vm.c	Fri May 17 21:54:50 2024 -0700
@@ -946,14 +946,6 @@ njs_value_string_get(njs_value_t *value,
 
 
 njs_int_t
-njs_vm_value_string_set(njs_vm_t *vm, njs_value_t *value, const u_char *start,
-    uint32_t size)
-{
-    return njs_string_set(vm, value, start, size);
-}
-
-
-njs_int_t
 njs_vm_value_array_buffer_set(njs_vm_t *vm, njs_value_t *value,
     const u_char *start, uint32_t size)
 {
@@ -981,13 +973,6 @@ njs_vm_value_buffer_set(njs_vm_t *vm, nj
 }
 
 
-u_char *
-njs_vm_value_string_alloc(njs_vm_t *vm, njs_value_t *value, uint32_t size)
-{
-    return njs_string_alloc(vm, value, size, 0);
-}
-
-
 njs_int_t
 njs_vm_value_string_create(njs_vm_t *vm, njs_value_t *value,
     const u_char *start, uint32_t size)
@@ -1342,7 +1327,7 @@ njs_vm_object_prop(njs_vm_t *vm, njs_val
         return NULL;
     }
 
-    ret = njs_vm_value_string_set(vm, &key, prop->start, prop->length);
+    ret = njs_vm_value_string_create(vm, &key, prop->start, prop->length);
     if (njs_slow_path(ret != NJS_OK)) {
         return NULL;
     }
@@ -1368,7 +1353,7 @@ njs_vm_object_prop_set(njs_vm_t *vm, njs
         return NJS_ERROR;
     }
 
-    ret = njs_vm_value_string_set(vm, &key, prop->start, prop->length);
+    ret = njs_vm_value_string_create(vm, &key, prop->start, prop->length);
     if (njs_slow_path(ret != NJS_OK)) {
         return NJS_ERROR;
     }
diff -r dec46ad52e9a -r 4e0553f7ea68 src/test/njs_benchmark.c
--- a/src/test/njs_benchmark.c	Fri May 17 21:54:49 2024 -0700
+++ b/src/test/njs_benchmark.c	Fri May 17 21:54:50 2024 -0700
@@ -188,10 +188,10 @@ njs_benchmark_test(njs_vm_t *parent, njs
         goto done;
     }
 
-    ret = njs_vm_value_string_set(parent, &name, (u_char *) test->name,
-                                  njs_strlen(test->name));
+    ret = njs_vm_value_string_create(parent, &name, (u_char *) test->name,
+                                     njs_strlen(test->name));
     if (ret != NJS_OK) {
-        njs_printf("njs_vm_value_string_set() failed\n");
+        njs_printf("njs_vm_value_string_create() failed\n");
         goto done;
     }
 
@@ -595,10 +595,10 @@ main(int argc, char **argv)
     }
 
     if (opts.previous) {
-        ret = njs_vm_value_string_set(vm, &args[0], (u_char *) opts.previous,
-                                      njs_strlen(opts.previous));
+        ret = njs_vm_value_string_create(vm, &args[0], (u_char *) opts.previous,
+                                         njs_strlen(opts.previous));
         if (ret != NJS_OK) {
-            njs_printf("njs_vm_value_string_set() failed\n");
+            njs_printf("njs_vm_value_string_create() failed\n");
             goto done;
         }
 
diff -r dec46ad52e9a -r 4e0553f7ea68 src/test/njs_externals_test.c
--- a/src/test/njs_externals_test.c	Fri May 17 21:54:49 2024 -0700
+++ b/src/test/njs_externals_test.c	Fri May 17 21:54:50 2024 -0700
@@ -158,7 +158,7 @@ njs_unit_test_r_uri(njs_vm_t *vm, njs_ob
         return njs_vm_value_to_bytes(vm, field, setval);
     }
 
-    return njs_vm_value_string_set(vm, retval, field->start, field->length);
+    return njs_vm_value_string_create(vm, retval, field->start, field->length);
 }
 
 
@@ -178,7 +178,7 @@ njs_unit_test_r_a(njs_vm_t *vm, njs_obje
 
     p = njs_sprintf(buf, buf + njs_length(buf), "%uD", r->a);
 
-    return njs_vm_value_string_set(vm, retval, buf, p - buf);
+    return njs_vm_value_string_create(vm, retval, buf, p - buf);
 }
 
 
@@ -214,7 +214,7 @@ static njs_int_t
 njs_unit_test_r_host(njs_vm_t *vm, njs_object_prop_t *prop,
     njs_value_t *value, njs_value_t *setval, njs_value_t *retval)
 {
-    return njs_vm_value_string_set(vm, retval, (u_char *) "АБВГДЕЁЖЗИЙ", 22);
+    return njs_vm_value_string_create(vm, retval, (u_char *) "АБВГДЕЁЖЗИЙ", 22);
 }
 
 
@@ -264,7 +264,7 @@ njs_unit_test_r_vars(njs_vm_t *vm, njs_o
 
     if (setval != NULL) {
         /* Set. */
-        njs_vm_value_string_set(vm, &name, lhq.key.start, lhq.key.length);
+        njs_vm_value_string_create(vm, &name, lhq.key.start, lhq.key.length);
         prop = lvlhsh_unit_test_alloc(vm->mem_pool, &name, setval);
         if (prop == NULL) {
             njs_memory_error(vm);
@@ -313,24 +313,22 @@ static njs_int_t
 njs_unit_test_r_header(njs_vm_t *vm, njs_object_prop_t *prop,
     njs_value_t *value, njs_value_t *unused, njs_value_t *retval)
 {
-    u_char     *p;
-    uint32_t   size;
     njs_int_t  ret;
     njs_str_t  h;
+    njs_chb_t  chain;
 
     ret = njs_vm_prop_name(vm, prop, &h);
     if (ret == NJS_OK) {
-        size = 7 + h.length;
+        NJS_CHB_MP_INIT(&chain, vm);
+
+        njs_chb_append(&chain, h.start, h.length);
+        njs_chb_append(&chain, (u_char *) "|АБВ", njs_length("|АБВ"));
 
-        p = njs_vm_value_string_alloc(vm, retval, size);
-        if (p == NULL) {
-            return NJS_ERROR;
-        }
+        ret = njs_vm_value_string_create_chb(vm, retval, &chain);
 
-        p = njs_cpymem(p, h.start, h.length);
-        *p++ = '|';
-        memcpy(p, "АБВ", njs_length("АБВ"));
-        return NJS_OK;
+        njs_chb_destroy(&chain);
+
+        return ret;
     }
 
     njs_value_undefined_set(retval);
@@ -360,7 +358,7 @@ njs_unit_test_r_header_keys(njs_vm_t *vm


More information about the nginx-devel mailing list