[njs] Fixed njs_vm_external_bind().

Dmitry Volyntsev xeioex at nginx.com
Thu Apr 26 17:36:55 UTC 2018


details:   http://hg.nginx.org/njs/rev/ea220019d249
branches:  
changeset: 512:ea220019d249
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Thu Apr 26 20:22:04 2018 +0300
description:
Fixed njs_vm_external_bind().

Previously, it could result in misaligned values being returned.

diffstat:

 njs/njs_builtin.c        |   2 +-
 njs/njs_extern.c         |   7 ++++---
 njs/njs_extern.h         |   2 +-
 njs/test/njs_unit_test.c |  16 ++++++----------
 4 files changed, 12 insertions(+), 15 deletions(-)

diffs (106 lines):

diff -r 5776906c23da -r ea220019d249 njs/njs_builtin.c
--- a/njs/njs_builtin.c	Thu Apr 26 20:21:46 2018 +0300
+++ b/njs/njs_builtin.c	Thu Apr 26 20:22:04 2018 +0300
@@ -675,7 +675,7 @@ njs_builtin_completions(njs_vm_t *vm, si
             break;
         }
 
-        ext_proto = ev->value->external.proto;
+        ext_proto = ev->value.external.proto;
 
         nxt_lvlhsh_each_init(&lhe_prop, &njs_extern_hash_proto);
 
diff -r 5776906c23da -r ea220019d249 njs/njs_extern.c
--- a/njs/njs_extern.c	Thu Apr 26 20:21:46 2018 +0300
+++ b/njs/njs_extern.c	Thu Apr 26 20:22:04 2018 +0300
@@ -210,13 +210,14 @@ njs_vm_external_bind(njs_vm_t *vm, const
         return NXT_ERROR;
     }
 
-    ev = nxt_mem_cache_alloc(vm->mem_cache_pool, sizeof(njs_extern_value_t));
+    ev = nxt_mem_cache_align(vm->mem_cache_pool, sizeof(njs_value_t),
+                             sizeof(njs_extern_value_t));
     if (nxt_slow_path(ev == NULL)) {
         return NXT_ERROR;
     }
 
+    ev->value = *value;
     ev->name = *var_name;
-    ev->value = value;
 
     lhq.key = *var_name;
     lhq.key_hash = nxt_djb_hash(lhq.key.start, lhq.key.length);
@@ -246,7 +247,7 @@ njs_parser_external(njs_vm_t *vm, njs_pa
 
     if (nxt_lvlhsh_find(&vm->externals_hash, &lhq) == NXT_OK) {
         ev = (njs_extern_value_t *) lhq.value;
-        return ev->value;
+        return &ev->value;
     }
 
     return NULL;
diff -r 5776906c23da -r ea220019d249 njs/njs_extern.h
--- a/njs/njs_extern.h	Thu Apr 26 20:21:46 2018 +0300
+++ b/njs/njs_extern.h	Thu Apr 26 20:22:04 2018 +0300
@@ -33,8 +33,8 @@ struct njs_extern_s {
 
 
 typedef struct {
+    njs_value_t             value;
     nxt_str_t               name;
-    njs_value_t             *value;
 } njs_extern_value_t;
 
 
diff -r 5776906c23da -r ea220019d249 njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c	Thu Apr 26 20:21:46 2018 +0300
+++ b/njs/test/njs_unit_test.c	Thu Apr 26 20:22:04 2018 +0300
@@ -9384,6 +9384,8 @@ typedef struct {
     uint32_t              a;
     nxt_mem_cache_pool_t  *mem_cache_pool;
     const njs_extern_t    *proto;
+
+    njs_opaque_value_t    value;
 } njs_unit_test_req_t;
 
 
@@ -9719,7 +9721,6 @@ njs_externals_init(njs_vm_t *vm)
     nxt_int_t            ret;
     nxt_uint_t           i;
     const njs_extern_t   *proto;
-    njs_opaque_value_t   *values;
     njs_unit_test_req_t  *requests;
 
     proto = njs_vm_external_prototype(vm, &nxt_test_external[0]);
@@ -9728,13 +9729,6 @@ njs_externals_init(njs_vm_t *vm)
         return NXT_ERROR;
     }
 
-    values = nxt_mem_cache_zalloc(vm->mem_cache_pool,
-                                  nxt_nitems(nxt_test_requests)
-                                  * sizeof(njs_opaque_value_t));
-    if (values == NULL) {
-        return NXT_ERROR;
-    }
-
     requests = nxt_mem_cache_zalloc(vm->mem_cache_pool,
                                     nxt_nitems(nxt_test_requests)
                                     * sizeof(njs_unit_test_req_t));
@@ -9748,13 +9742,15 @@ njs_externals_init(njs_vm_t *vm)
         requests[i].mem_cache_pool = vm->mem_cache_pool;
         requests[i].proto = proto;
 
-        ret = njs_vm_external_create(vm, &values[i], proto, &requests[i]);
+        ret = njs_vm_external_create(vm, &requests[i].value, proto,
+                                     &requests[i]);
         if (ret != NXT_OK) {
             printf("njs_vm_external_create() failed\n");
             return NXT_ERROR;
         }
 
-        ret = njs_vm_external_bind(vm, &nxt_test_requests[i].name, &values[i]);
+        ret = njs_vm_external_bind(vm, &nxt_test_requests[i].name,
+                                   &requests[i].value);
         if (ret != NXT_OK) {
             printf("njs_vm_external_bind() failed\n");
             return NXT_ERROR;


More information about the nginx-devel mailing list