[njs] Fixed parsing of environ variables when duplicate keys are present.

Dmitry Volyntsev xeioex at nginx.com
Sat Oct 1 04:11:59 UTC 2022


details:   https://hg.nginx.org/njs/rev/d6a15aa909cd
branches:  
changeset: 1972:d6a15aa909cd
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Fri Sep 30 17:35:52 2022 -0700
description:
Fixed parsing of environ variables when duplicate keys are present.

This closes #581 issue on Github.

diffstat:

 src/njs_builtin.c |  22 +++++++++++++++++++---
 1 files changed, 19 insertions(+), 3 deletions(-)

diffs (39 lines):

diff -r b61a7a4f286e -r d6a15aa909cd src/njs_builtin.c
--- a/src/njs_builtin.c	Thu Sep 29 16:32:52 2022 -0700
+++ b/src/njs_builtin.c	Fri Sep 30 17:35:52 2022 -0700
@@ -1795,7 +1795,7 @@ njs_env_hash_init(njs_vm_t *vm, njs_lvlh
     char                **ep;
     u_char              *val, *entry;
     njs_int_t           ret;
-    njs_object_prop_t   *prop;
+    njs_object_prop_t   *prop, *prev;
     njs_lvlhsh_query_t  lhq;
 
     lhq.replace = 0;
@@ -1836,8 +1836,24 @@ njs_env_hash_init(njs_vm_t *vm, njs_lvlh
 
         ret = njs_lvlhsh_insert(hash, &lhq);
         if (njs_slow_path(ret != NJS_OK)) {
-            njs_internal_error(vm, "lvlhsh insert failed");
-            return NJS_ERROR;
+            if (ret == NJS_ERROR) {
+                njs_internal_error(vm, "lvlhsh insert failed");
+                return NJS_ERROR;
+            }
+
+            /* ret == NJS_DECLINED: entry already exists */
+
+            /*
+             * Always using the first element among the duplicates
+             * and ignoring the rest.
+             */
+
+            prev = lhq.value;
+
+            if (!njs_values_same(&prop->value, &prev->value)) {
+                njs_vm_warn(vm, "environment variable \"%V\" has more than one"
+                            " value\n", &lhq.key);
+            }
         }
     }
 



More information about the nginx-devel mailing list