[njs] Improved population of process.env object.

Dmitry Volyntsev xeioex at nginx.com
Sat Oct 1 04:12:01 UTC 2022


details:   https://hg.nginx.org/njs/rev/8517d9593c30
branches:  
changeset: 1973:8517d9593c30
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Thu Sep 29 18:48:09 2022 -0700
description:
Improved population of process.env object.

1) Keys are always casted to upper case.
2) Keys and values are converted to safe Unicode strings.

diffstat:

 src/njs_builtin.c        |  27 +++++++++++++++++++++------
 src/njs_unix.h           |   2 ++
 src/test/njs_unit_test.c |   8 ++++++++
 3 files changed, 31 insertions(+), 6 deletions(-)

diffs (99 lines):

diff -r d6a15aa909cd -r 8517d9593c30 src/njs_builtin.c
--- a/src/njs_builtin.c	Fri Sep 30 17:35:52 2022 -0700
+++ b/src/njs_builtin.c	Thu Sep 29 18:48:09 2022 -0700
@@ -107,9 +107,6 @@ static const njs_object_type_init_t *con
 };
 
 
-extern char  **environ;
-
-
 njs_inline njs_int_t
 njs_object_hash_init(njs_vm_t *vm, njs_lvlhsh_t *hash,
     const njs_object_init_t *init)
@@ -1793,9 +1790,13 @@ static njs_int_t
 njs_env_hash_init(njs_vm_t *vm, njs_lvlhsh_t *hash, char **environment)
 {
     char                **ep;
-    u_char              *val, *entry;
+    u_char              *dst;
+    ssize_t             length;
+    uint32_t            cp;
     njs_int_t           ret;
+    const u_char        *val, *entry, *s, *end;
     njs_object_prop_t   *prop, *prev;
+    njs_string_prop_t   string;
     njs_lvlhsh_query_t  lhq;
 
     lhq.replace = 0;
@@ -1818,14 +1819,28 @@ njs_env_hash_init(njs_vm_t *vm, njs_lvlh
             continue;
         }
 
-        ret = njs_string_set(vm, &prop->name, entry, val - entry);
+        ret = njs_string_create(vm, &prop->name, (char *) entry, val - entry);
         if (njs_slow_path(ret != NJS_OK)) {
             return NJS_ERROR;
         }
 
+        (void) njs_string_prop(&string, &prop->name);
+
+        length = string.length;
+        s = string.start;
+        end = s + string.size;
+        dst = (u_char *) s;
+
+        while (length != 0) {
+            cp = njs_utf8_upper_case(&s, end);
+            dst = njs_utf8_encode(dst, cp);
+            length--;
+        }
+
         val++;
 
-        ret = njs_string_set(vm, &prop->value, val, njs_strlen(val));
+        ret = njs_string_create(vm, &prop->value, (char *) val,
+                                njs_strlen(val));
         if (njs_slow_path(ret != NJS_OK)) {
             return NJS_ERROR;
         }
diff -r d6a15aa909cd -r 8517d9593c30 src/njs_unix.h
--- a/src/njs_unix.h	Fri Sep 30 17:35:52 2022 -0700
+++ b/src/njs_unix.h	Thu Sep 29 18:48:09 2022 -0700
@@ -47,6 +47,8 @@
 
 #include <unistd.h>
 
+extern char  **environ;
+
 #if defined(PATH_MAX)
 #define NJS_MAX_PATH             PATH_MAX
 #else
diff -r d6a15aa909cd -r 8517d9593c30 src/test/njs_unit_test.c
--- a/src/test/njs_unit_test.c	Fri Sep 30 17:35:52 2022 -0700
+++ b/src/test/njs_unit_test.c	Thu Sep 29 18:48:09 2022 -0700
@@ -13877,6 +13877,9 @@ static njs_unit_test_t  njs_test[] =
     { njs_str("Object.values(process)"),
       njs_str("") },
 
+    { njs_str("Object.keys(process.env).sort()"),
+      njs_str("DUP,TZ") },
+
     { njs_str("Object.values()"),
       njs_str("TypeError: cannot convert undefined argument to object") },
 
@@ -24160,9 +24163,14 @@ main(int argc, char **argv)
         return (ret == NJS_DONE) ? EXIT_SUCCESS: EXIT_FAILURE;
     }
 
+    environ = NULL;
+
     (void) putenv((char *) "TZ=UTC");
     tzset();
 
+    (void) putenv((char *) "DUP=bar");
+    (void) putenv((char *) "dup=foo");
+
     njs_mm_denormals(1);
 
     njs_memzero(&stat, sizeof(njs_stat_t));



More information about the nginx-devel mailing list