[njs] Fixed njs_key_string_get() usage.

Dmitry Volyntsev xeioex at nginx.com
Wed Feb 5 14:58:23 UTC 2020


details:   https://hg.nginx.org/njs/rev/d228145f0178
branches:  
changeset: 1318:d228145f0178
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Wed Feb 05 15:54:56 2020 +0300
description:
Fixed njs_key_string_get() usage.

njs_key_string_get() converts Symbol instances to strings,
while Symbols should never be converted to strings implicitly.
The function is expected to be used only while generating Error
messages.

Found by Clang static analyzer.

diffstat:

 src/njs_object_prop.c |  10 +++++++---
 src/njs_value.c       |   2 --
 2 files changed, 7 insertions(+), 5 deletions(-)

diffs (47 lines):

diff -r 53cad4cfc9cb -r d228145f0178 src/njs_object_prop.c
--- a/src/njs_object_prop.c	Wed Feb 05 15:26:18 2020 +0300
+++ b/src/njs_object_prop.c	Wed Feb 05 15:54:56 2020 +0300
@@ -100,6 +100,7 @@ njs_object_property_add(njs_vm_t *vm, nj
     njs_bool_t replace)
 {
     njs_int_t           ret;
+    njs_value_t         key_value;
     njs_object_prop_t   *prop;
     njs_lvlhsh_query_t  lhq;
 
@@ -108,8 +109,13 @@ njs_object_property_add(njs_vm_t *vm, nj
         return NULL;
     }
 
+    ret = njs_value_to_key(vm, &key_value, key);
+    if (njs_slow_path(ret != NJS_OK)) {
+        return NULL;
+    }
+
     lhq.proto = &njs_object_hash_proto;
-    njs_key_string_get(vm, key, &lhq.key);
+    njs_string_get(&key_value, &lhq.key);
     lhq.key_hash = njs_djb_hash(lhq.key.start, lhq.key.length);
     lhq.value = prop;
     lhq.replace = replace;
@@ -432,8 +438,6 @@ done:
 
         } else {
             if (njs_slow_path(pq.lhq.key_hash == NJS_LENGTH_HASH)) {
-                njs_key_string_get(vm, &pq.key, &pq.lhq.key);
-
                 if (njs_strstr_eq(&pq.lhq.key, &length_key)) {
                     ret = njs_array_length_set(vm, object, prev, &prop->value);
                     if (ret != NJS_DECLINED) {
diff -r 53cad4cfc9cb -r d228145f0178 src/njs_value.c
--- a/src/njs_value.c	Wed Feb 05 15:26:18 2020 +0300
+++ b/src/njs_value.c	Wed Feb 05 15:54:56 2020 +0300
@@ -1288,8 +1288,6 @@ slow_path:
             switch (prop->type) {
             case NJS_PROPERTY:
                 if (njs_slow_path(pq.lhq.key_hash == NJS_LENGTH_HASH)) {
-                    njs_key_string_get(vm, &pq.key, &pq.lhq.key);
-
                     if (njs_strstr_eq(&pq.lhq.key, &length_key)) {
                         ret = njs_array_length_set(vm, value, prop, setval);
                         if (ret != NJS_DECLINED) {


More information about the nginx-devel mailing list