]> git.kaiwu.me - njs.git/commitdiff
Fixed njs_buffer_slot().
authorDmitry Volyntsev <xeioex@nginx.com>
Fri, 17 Sep 2021 18:29:40 +0000 (18:29 +0000)
committerDmitry Volyntsev <xeioex@nginx.com>
Fri, 17 Sep 2021 18:29:40 +0000 (18:29 +0000)
Previously, njs_buffer_slot() might return NULL value without setting
corresponding exception where user code expects it.

In addition the function is split into two functions.  The internal one
does not set anything to vm->retval.  This function has to be used by
property handlers, because they are expected not to modify vm->retval.

src/njs_buffer.c
src/test/njs_unit_test.c

index 86a0b93b527a63c35f602c32451640a37e0eb2d4..2c47657c4dc6319dd1b69f76f8e1d9d1de0ad799 100644 (file)
@@ -572,30 +572,36 @@ njs_buffer_byte_length(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
 
 
 static njs_typed_array_t *
-njs_buffer_slot(njs_vm_t *vm, njs_value_t *value, const char *name)
+njs_buffer_slot_internal(njs_vm_t *vm, njs_value_t *value)
 {
     njs_typed_array_t  *array;
 
-    if (njs_slow_path(!njs_is_object(value))) {
-        goto failed;
+    if (njs_is_object(value)) {
+        array = njs_object_proto_lookup(njs_object(value), NJS_TYPED_ARRAY,
+                                        njs_typed_array_t);
+
+        if (array != NULL && array->type == NJS_OBJ_TYPE_UINT8_ARRAY) {
+            return array;
+        }
     }
 
-    array = njs_object_proto_lookup(njs_object(value), NJS_TYPED_ARRAY,
-                                    njs_typed_array_t);
+    return NULL;
+}
 
-    if (njs_slow_path(array != NULL
-                      && array->type != NJS_OBJ_TYPE_UINT8_ARRAY))
-    {
-        goto failed;
-    }
 
-    return array;
+static njs_typed_array_t *
+njs_buffer_slot(njs_vm_t *vm, njs_value_t *value, const char *name)
+{
+    njs_typed_array_t  *array;
 
-failed:
+    array = njs_buffer_slot_internal(vm, value);
+    if (njs_slow_path(array == NULL)) {
+        njs_type_error(vm, "\"%s\" argument must be an instance "
+                           "of Buffer or Uint8Array", name);
+        return NULL;
+    }
 
-    njs_type_error(vm, "\"%s\" argument must be an instance "
-                       "of Buffer or Uint8Array", name);
-    return NULL;
+    return array;
 }
 
 
@@ -902,7 +908,7 @@ njs_buffer_prototype_length(njs_vm_t *vm, njs_object_prop_t *prop,
 {
     njs_typed_array_t  *array;
 
-    array = njs_buffer_slot(vm, value, "this");
+    array = njs_buffer_slot_internal(vm, value);
     if (njs_slow_path(array == NULL)) {
         njs_set_undefined(retval);
         return NJS_DECLINED;
index 5dd2318c66b99dfce62849b9f3c38d7e1d178b76..72d0181746a8de880defd60ddac6ffdc24a04a31 100644 (file)
@@ -19538,6 +19538,12 @@ static njs_unit_test_t  njs_test[] =
               "})"),
       njs_str("true") },
 
+    { njs_str("Buffer.from([1,2]).equals(new ArrayBuffer(1))"),
+      njs_str("TypeError: \"target\" argument must be an instance of Buffer or Uint8Array") },
+
+    { njs_str("Buffer.from([1,2]).equals(1)"),
+      njs_str("TypeError: \"target\" argument must be an instance of Buffer or Uint8Array") },
+
     { njs_str("var buf = Buffer.alloc(4);"
               "buf.fill('ZXZpbA==', 'base64')"),
       njs_str("evil") },