]> git.kaiwu.me - njs.git/commitdiff
Setting exception values where appropriate.
authorDmitry Volyntsev <xeioex@nginx.com>
Thu, 2 Aug 2018 16:39:55 +0000 (19:39 +0300)
committerDmitry Volyntsev <xeioex@nginx.com>
Thu, 2 Aug 2018 16:39:55 +0000 (19:39 +0300)
This fixes #42 issue on Github.

15 files changed:
njs/njs_array.c
njs/njs_crypto.c
njs/njs_date.c
njs/njs_error.c
njs/njs_extern.c
njs/njs_fs.c
njs/njs_function.c
njs/njs_json.c
njs/njs_object.c
njs/njs_regexp.c
njs/njs_string.c
njs/njs_time.c
njs/njs_variable.c
njs/njs_vm.c
njs/test/njs_unit_test.c

index c64dc10bce17cbf66b804fbc2d07e39f9c7c3d7a..b1fac68b1398f04341e237594c5e9d5129ef573c 100644 (file)
@@ -109,20 +109,24 @@ static njs_ret_t njs_array_prototype_sort_continuation(njs_vm_t *vm,
 nxt_noinline njs_array_t *
 njs_array_alloc(njs_vm_t *vm, uint32_t length, uint32_t spare)
 {
-    uint32_t     size;
+    size_t       size;
     njs_array_t  *array;
 
     array = nxt_mem_cache_alloc(vm->mem_cache_pool, sizeof(njs_array_t));
     if (nxt_slow_path(array == NULL)) {
-        return NULL;
+        goto memory_error;
     }
 
     size = length + spare;
 
+    if (nxt_slow_path(size * sizeof(njs_value_t) < size)) {
+        goto memory_error;
+    }
+
     array->data = nxt_mem_cache_align(vm->mem_cache_pool, sizeof(njs_value_t),
                                       size * sizeof(njs_value_t));
     if (nxt_slow_path(array->data == NULL)) {
-        return NULL;
+        goto memory_error;
     }
 
     array->start = array->data;
@@ -136,6 +140,12 @@ njs_array_alloc(njs_vm_t *vm, uint32_t length, uint32_t spare)
     array->length = length;
 
     return array;
+
+memory_error:
+
+    njs_memory_error(vm);
+
+    return NULL;
 }
 
 
@@ -194,6 +204,7 @@ njs_array_expand(njs_vm_t *vm, njs_array_t *array, uint32_t prepend,
     start = nxt_mem_cache_align(vm->mem_cache_pool, sizeof(njs_value_t),
                                 (prepend + size) * sizeof(njs_value_t));
     if (nxt_slow_path(start == NULL)) {
+        njs_memory_error(vm);
         return NXT_ERROR;
     }
 
@@ -230,7 +241,7 @@ njs_array_constructor(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs,
         size = (uint32_t) num;
 
         if ((double) size != num) {
-            njs_range_error(vm, NULL);
+            njs_range_error(vm, "Invalid array length");
             return NXT_ERROR;
         }
 
@@ -393,7 +404,6 @@ njs_array_prototype_length(njs_vm_t *vm, njs_value_t *value,
         if (size > 0) {
             ret = njs_array_expand(vm, array, 0, size);
             if (nxt_slow_path(ret != NXT_OK)) {
-                njs_memory_error(vm);
                 return NJS_ERROR;
             }
 
@@ -846,6 +856,7 @@ njs_array_prototype_join(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs,
         values = nxt_mem_cache_align(vm->mem_cache_pool, sizeof(njs_value_t),
                                      sizeof(njs_value_t) * max);
         if (nxt_slow_path(values == NULL)) {
+            njs_memory_error(vm);
             return NXT_ERROR;
         }
 
index de075708c7d828d389e988da92dc05bf228df8c2..ef1318b8d15cd3815dfa4b3c02395a7e7816cb35 100644 (file)
@@ -142,9 +142,12 @@ njs_crypto_object_value_alloc(njs_vm_t *vm, nxt_uint_t proto)
         ov->object.extensible = 1;
 
         ov->object.__proto__ = &vm->prototypes[proto].object;
+        return ov;
     }
 
-    return ov;
+    njs_memory_error(vm);
+
+    return NULL;
 }
 
 
@@ -171,12 +174,13 @@ njs_crypto_create_hash(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs,
 
     hash = njs_crypto_object_value_alloc(vm, NJS_PROTOTYPE_CRYPTO_HASH);
     if (nxt_slow_path(hash == NULL)) {
-        goto memory_error;
+        return NJS_ERROR;
     }
 
     dgst = nxt_mem_cache_alloc(vm->mem_cache_pool, sizeof(njs_digest_t));
     if (nxt_slow_path(dgst == NULL)) {
-        goto memory_error;
+        njs_memory_error(vm);
+        return NJS_ERROR;
     }
 
     dgst->alg = alg;
@@ -190,12 +194,6 @@ njs_crypto_create_hash(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs,
     vm->retval.data.truth = 1;
 
     return NJS_OK;
-
-memory_error:
-
-    njs_memory_error(vm);
-
-    return NJS_ERROR;
 }
 
 
@@ -412,7 +410,8 @@ njs_crypto_create_hmac(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs,
 
     ctx = nxt_mem_cache_alloc(vm->mem_cache_pool, sizeof(njs_hmac_t));
     if (nxt_slow_path(ctx == NULL)) {
-        goto memory_error;
+        njs_memory_error(vm);
+        return NJS_ERROR;
     }
 
     ctx->alg = alg;
@@ -443,7 +442,7 @@ njs_crypto_create_hmac(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs,
 
     hmac = njs_crypto_object_value_alloc(vm, NJS_PROTOTYPE_CRYPTO_HMAC);
     if (nxt_slow_path(hmac == NULL)) {
-        goto memory_error;
+        return NJS_ERROR;
     }
 
     njs_value_data_set(&hmac->value, ctx);
@@ -453,12 +452,6 @@ njs_crypto_create_hmac(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs,
     vm->retval.data.truth = 1;
 
     return NJS_OK;
-
-memory_error:
-
-    njs_memory_error(vm);
-
-    return NJS_ERROR;
 }
 
 
index dc5bef478af50e205a703025f484f59cccc77ecb..b08c75c9bab8419b2c26ea3632d7880b19431225 100644 (file)
@@ -129,6 +129,7 @@ njs_date_constructor(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs,
 
         date = nxt_mem_cache_alloc(vm->mem_cache_pool, sizeof(njs_date_t));
         if (nxt_slow_path(date == NULL)) {
+            njs_memory_error(vm);
             return NXT_ERROR;
         }
 
@@ -1052,9 +1053,9 @@ njs_date_to_string(njs_vm_t *vm, njs_value_t *retval, const njs_value_t *date)
         return njs_string_new(vm, retval, buf, size, size);
     }
 
-    njs_range_error(vm, NULL);
+    vm->retval = njs_string_invalid_date;
 
-    return NXT_ERROR;
+    return NXT_OK;
 }
 
 
index 2b426f8fe0dc7a880272c436d97aacb44a0de938..2cdc48e11f47223dc5eac7bc8a6569df801fa436 100644 (file)
@@ -36,23 +36,15 @@ njs_exception_error_create(njs_vm_t *vm, njs_value_type_t type,
 
     ret = njs_string_new(vm, &string, (const u_char *) buf, size, size);
     if (nxt_slow_path(ret != NXT_OK)) {
-        goto memory_error;
+        return;
     }
 
     error = njs_error_alloc(vm, type, NULL, &string);
-    if (nxt_slow_path(error == NULL)) {
-        goto memory_error;
+    if (nxt_fast_path(error != NULL)) {
+        vm->retval.data.u.object = error;
+        vm->retval.type = type;
+        vm->retval.data.truth = 1;
     }
-
-    vm->retval.data.u.object = error;
-    vm->retval.type = type;
-    vm->retval.data.truth = 1;
-
-    return;
-
-memory_error:
-
-    njs_memory_error(vm);
 }
 
 
@@ -67,6 +59,7 @@ njs_error_alloc(njs_vm_t *vm, njs_value_type_t type, const njs_value_t *name,
 
     error = nxt_mem_cache_alloc(vm->mem_cache_pool, sizeof(njs_object_t));
     if (nxt_slow_path(error == NULL)) {
+        njs_memory_error(vm);
         return NULL;
     }
 
@@ -94,6 +87,7 @@ njs_error_alloc(njs_vm_t *vm, njs_value_type_t type, const njs_value_t *name,
 
         ret = nxt_lvlhsh_insert(&error->hash, &lhq);
         if (nxt_slow_path(ret != NXT_OK)) {
+            njs_internal_error(vm, NULL);
             return NULL;
         }
     }
@@ -114,6 +108,7 @@ njs_error_alloc(njs_vm_t *vm, njs_value_type_t type, const njs_value_t *name,
 
         ret = nxt_lvlhsh_insert(&error->hash, &lhq);
         if (nxt_slow_path(ret != NXT_OK)) {
+            njs_internal_error(vm, NULL);
             return NULL;
         }
     }
@@ -138,7 +133,6 @@ njs_error_create(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs,
 
     error = njs_error_alloc(vm, type, NULL, value);
     if (nxt_slow_path(error == NULL)) {
-        njs_memory_error(vm);
         return NXT_ERROR;
     }
 
index 53352350e7215447d0c2a4f1395051d3c41cf14c..07dcccdb6fe073cda07f2e122a6de12dae61266b 100644 (file)
@@ -81,7 +81,7 @@ njs_vm_external_add(njs_vm_t *vm, nxt_lvlhsh_t *hash, njs_external_t *external,
     do {
         ext = nxt_mem_cache_alloc(vm->mem_cache_pool, sizeof(njs_extern_t));
         if (nxt_slow_path(ext == NULL)) {
-            return NULL;
+            goto memory_error;
         }
 
         ext->name = external->name;
@@ -100,7 +100,7 @@ njs_vm_external_add(njs_vm_t *vm, nxt_lvlhsh_t *hash, njs_external_t *external,
             function = nxt_mem_cache_zalloc(vm->mem_cache_pool,
                                             sizeof(njs_function_t));
             if (nxt_slow_path(function == NULL)) {
-                return NULL;
+                goto memory_error;
             }
 
             /*
@@ -129,7 +129,7 @@ njs_vm_external_add(njs_vm_t *vm, nxt_lvlhsh_t *hash, njs_external_t *external,
             child = njs_vm_external_add(vm, &ext->hash, external->properties,
                                         external->nproperties);
             if (nxt_slow_path(child == NULL)) {
-                return NULL;
+                goto memory_error;
             }
         }
 
@@ -144,6 +144,7 @@ njs_vm_external_add(njs_vm_t *vm, nxt_lvlhsh_t *hash, njs_external_t *external,
 
             ret = nxt_lvlhsh_insert(hash, &lhq);
             if (nxt_slow_path(ret != NXT_OK)) {
+                njs_internal_error(vm, NULL);
                 return NULL;
             }
         }
@@ -154,6 +155,12 @@ njs_vm_external_add(njs_vm_t *vm, nxt_lvlhsh_t *hash, njs_external_t *external,
     } while (n != 0);
 
     return ext;
+
+memory_error:
+
+    njs_memory_error(vm);
+
+    return NULL;
 }
 
 
@@ -206,6 +213,7 @@ njs_vm_external_bind(njs_vm_t *vm, const nxt_str_t *var_name,
     ev = nxt_mem_cache_align(vm->mem_cache_pool, sizeof(njs_value_t),
                              sizeof(njs_extern_value_t));
     if (nxt_slow_path(ev == NULL)) {
+        njs_memory_error(vm);
         return NXT_ERROR;
     }
 
@@ -221,6 +229,7 @@ njs_vm_external_bind(njs_vm_t *vm, const nxt_str_t *var_name,
 
     ret = nxt_lvlhsh_insert(&vm->externals_hash, &lhq);
     if (nxt_slow_path(ret != NXT_OK)) {
+        njs_internal_error(vm, NULL);
         return ret;
     }
 
index 23a9a35e5bead2ed9da0b7a480470d2bfbd4fc2d..112f3db5e7a8f4e54bf31c5761731241242c55a1 100644 (file)
@@ -221,7 +221,7 @@ njs_fs_read_file(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs,
 
     start = njs_string_alloc(vm, &arguments[2], sb.st_size, length);
     if (nxt_slow_path(start == NULL)) {
-        goto memory_error;
+        goto fail;
     }
 
     p = start;
@@ -286,14 +286,12 @@ done:
     return njs_function_apply(vm, callback->data.u.function,
                               arguments, 3, (njs_index_t) &vm->retval);
 
-memory_error:
+fail:
 
     if (fd != -1) {
         (void) close(fd);
     }
 
-    njs_memory_error(vm);
-
     return NJS_ERROR;
 }
 
@@ -420,7 +418,7 @@ njs_fs_read_file_sync(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs,
 
     start = njs_string_alloc(vm, &vm->retval, sb.st_size, length);
     if (nxt_slow_path(start == NULL)) {
-        goto memory_error;
+        goto fail;
     }
 
     p = start;
@@ -472,14 +470,12 @@ done:
 
     return NJS_OK;
 
-memory_error:
+fail:
 
     if (fd != -1) {
         (void) close(fd);
     }
 
-    njs_memory_error(vm);
-
     return NJS_ERROR;
 }
 
@@ -898,12 +894,12 @@ static njs_ret_t njs_fs_error(njs_vm_t *vm, const char *syscall,
 
     ret = njs_string_new(vm, &string, (u_char *) description, size, size);
     if (nxt_slow_path(ret != NXT_OK)) {
-        goto memory_error;
+        return NJS_ERROR;
     }
 
     error = njs_error_alloc(vm, NJS_OBJECT_ERROR, NULL, &string);
     if (nxt_slow_path(error == NULL)) {
-        goto memory_error;
+        return NJS_ERROR;
     }
 
     lhq.replace = 0;
@@ -920,7 +916,7 @@ static njs_ret_t njs_fs_error(njs_vm_t *vm, const char *syscall,
 
         prop = njs_object_prop_alloc(vm, &njs_fs_errno_string, &value, 1);
         if (nxt_slow_path(prop == NULL)) {
-            goto memory_error;
+            return NJS_ERROR;
         }
 
         lhq.value = prop;
@@ -939,7 +935,7 @@ static njs_ret_t njs_fs_error(njs_vm_t *vm, const char *syscall,
 
         prop = njs_object_prop_alloc(vm, &njs_fs_path_string, path, 1);
         if (nxt_slow_path(prop == NULL)) {
-            goto memory_error;
+            return NJS_ERROR;
         }
 
         lhq.value = prop;
@@ -955,7 +951,7 @@ static njs_ret_t njs_fs_error(njs_vm_t *vm, const char *syscall,
         size = strlen(syscall);
         ret = njs_string_new(vm, &string, (u_char *) syscall, size, size);
         if (nxt_slow_path(ret != NXT_OK)) {
-            goto memory_error;
+            return NJS_ERROR;
         }
 
         lhq.key = nxt_string_value("sycall");
@@ -964,7 +960,7 @@ static njs_ret_t njs_fs_error(njs_vm_t *vm, const char *syscall,
 
         prop = njs_object_prop_alloc(vm, &njs_fs_syscall_string, &string, 1);
         if (nxt_slow_path(prop == NULL)) {
-            goto memory_error;
+            return NJS_ERROR;
         }
 
         lhq.value = prop;
@@ -981,12 +977,6 @@ static njs_ret_t njs_fs_error(njs_vm_t *vm, const char *syscall,
     retval->data.truth = 1;
 
     return NJS_OK;
-
-memory_error:
-
-    njs_memory_error(vm);
-
-    return NJS_ERROR;
 }
 
 
index 8a77c2d20390a0c181db3e3ec5bde559d86f2b7e..2b18a883ce45aa2cb28b6d3d4a473814f2ec19e3 100644 (file)
@@ -35,11 +35,16 @@ njs_function_alloc(njs_vm_t *vm)
         function->u.lambda = nxt_mem_cache_zalloc(vm->mem_cache_pool,
                                                  sizeof(njs_function_lambda_t));
         if (nxt_slow_path(function->u.lambda == NULL)) {
+            njs_memory_error(vm);
             return NULL;
         }
+
+        return function;
     }
 
-    return function;
+    njs_memory_error(vm);
+
+    return NULL;
 }
 
 
@@ -62,7 +67,8 @@ njs_function_value_copy(njs_vm_t *vm, njs_value_t *value)
 
     copy = nxt_mem_cache_alloc(vm->mem_cache_pool, size);
     if (nxt_slow_path(copy == NULL)) {
-        return copy;
+        njs_memory_error(vm);
+        return NULL;
     }
 
     value->data.u.function = copy;
@@ -247,6 +253,7 @@ njs_function_frame_alloc(njs_vm_t *vm, size_t size)
         frame = nxt_mem_cache_align(vm->mem_cache_pool, sizeof(njs_value_t),
                                     spare_size);
         if (nxt_slow_path(frame == NULL)) {
+            njs_memory_error(vm);
             return NULL;
         }
 
@@ -365,6 +372,7 @@ njs_function_call(njs_vm_t *vm, njs_index_t retval, size_t advance)
             closure = nxt_mem_cache_align(vm->mem_cache_pool,
                                           sizeof(njs_value_t), size);
             if (nxt_slow_path(closure == NULL)) {
+                njs_memory_error(vm);
                 return NXT_ERROR;
             }
 
@@ -618,6 +626,7 @@ njs_function_prototype_bind(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs,
 
     function = nxt_mem_cache_alloc(vm->mem_cache_pool, sizeof(njs_function_t));
     if (nxt_slow_path(function == NULL)) {
+        njs_memory_error(vm);
         return NXT_ERROR;
     }
 
@@ -640,6 +649,7 @@ njs_function_prototype_bind(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs,
 
     values = nxt_mem_cache_alloc(vm->mem_cache_pool, size);
     if (nxt_slow_path(values == NULL)) {
+        njs_memory_error(vm);
         nxt_mem_cache_free(vm->mem_cache_pool, function);
         return NXT_ERROR;
     }
index 9e2a571ba0526fe64eb2d8e829e159ed2c4cb66a..3c33da87e06c6bce14f600d397104ee6765f478f 100644 (file)
@@ -535,7 +535,6 @@ njs_json_parse_array(njs_json_parse_ctx_t *ctx, njs_value_t *value,
 
     array = njs_array_alloc(ctx->vm, 0, 0);
     if (nxt_slow_path(array == NULL)) {
-        njs_memory_error(ctx->vm);
         return NULL;
     }
 
@@ -812,7 +811,6 @@ njs_json_parse_string(njs_json_parse_ctx_t *ctx, njs_value_t *value,
 
     ret = njs_string_create(ctx->vm, value, (u_char *) start, size, length);
     if (nxt_slow_path(ret != NXT_OK)) {
-        njs_memory_error(ctx->vm);
         return NULL;
     }
 
index d63e853ab0b075f3d8f14cacfe83ec7924763efe..5cfbae731f4eafdaaa583db7a543c0fefd0e99d1 100644 (file)
@@ -34,9 +34,12 @@ njs_object_alloc(njs_vm_t *vm)
         object->type = NJS_OBJECT;
         object->shared = 0;
         object->extensible = 1;
+        return object;
     }
 
-    return object;
+    njs_memory_error(vm);
+
+    return NULL;
 }
 
 
@@ -58,9 +61,12 @@ njs_object_value_copy(njs_vm_t *vm, njs_value_t *value)
         object->__proto__ = &vm->prototypes[NJS_PROTOTYPE_OBJECT].object;
         object->shared = 0;
         value->data.u.object = object;
+        return object;
     }
 
-    return object;
+    njs_memory_error(vm);
+
+    return NULL;
 }
 
 
@@ -83,9 +89,13 @@ njs_object_value_alloc(njs_vm_t *vm, const njs_value_t *value, nxt_uint_t type)
         ov->object.__proto__ = &vm->prototypes[index].object;
 
         ov->value = *value;
+
+        return &ov->object;
     }
 
-    return &ov->object;
+    njs_memory_error(vm);
+
+    return NULL;
 }
 
 
@@ -107,6 +117,7 @@ njs_object_hash_create(njs_vm_t *vm, nxt_lvlhsh_t *hash,
 
         ret = nxt_lvlhsh_insert(hash, &lhq);
         if (nxt_slow_path(ret != NXT_OK)) {
+            njs_internal_error(vm, NULL);
             return NXT_ERROR;
         }
 
@@ -183,9 +194,12 @@ njs_object_prop_alloc(njs_vm_t *vm, const njs_value_t *name,
         prop->enumerable = attributes;
         prop->writable = attributes;
         prop->configurable = attributes;
+        return prop;
     }
 
-    return prop;
+    njs_memory_error(vm);
+
+    return NULL;
 }
 
 
@@ -988,6 +1002,7 @@ njs_object_get_own_property_descriptor(njs_vm_t *vm, njs_value_t *args,
 
     ret = nxt_lvlhsh_insert(&descriptor->hash, &lhq);
     if (nxt_slow_path(ret != NXT_OK)) {
+        njs_internal_error(vm, NULL);
         return NXT_ERROR;
     }
 
@@ -1005,6 +1020,7 @@ njs_object_get_own_property_descriptor(njs_vm_t *vm, njs_value_t *args,
 
     ret = nxt_lvlhsh_insert(&descriptor->hash, &lhq);
     if (nxt_slow_path(ret != NXT_OK)) {
+        njs_internal_error(vm, NULL);
         return NXT_ERROR;
     }
 
@@ -1022,6 +1038,7 @@ njs_object_get_own_property_descriptor(njs_vm_t *vm, njs_value_t *args,
 
     ret = nxt_lvlhsh_insert(&descriptor->hash, &lhq);
     if (nxt_slow_path(ret != NXT_OK)) {
+        njs_internal_error(vm, NULL);
         return NXT_ERROR;
     }
 
@@ -1039,6 +1056,7 @@ njs_object_get_own_property_descriptor(njs_vm_t *vm, njs_value_t *args,
 
     ret = nxt_lvlhsh_insert(&descriptor->hash, &lhq);
     if (nxt_slow_path(ret != NXT_OK)) {
+        njs_internal_error(vm, NULL);
         return NXT_ERROR;
     }
 
@@ -1395,7 +1413,6 @@ njs_property_prototype_create(njs_vm_t *vm, nxt_lvlhsh_t *hash,
         return &prop->value;
     }
 
-    /* Memory allocation or NXT_DECLINED error. */
     njs_internal_error(vm, NULL);
 
     return NULL;
@@ -1638,7 +1655,6 @@ njs_property_constructor_create(njs_vm_t *vm, nxt_lvlhsh_t *hash,
         return &prop->value;
     }
 
-    /* Memory allocation or NXT_DECLINED error. */
     njs_internal_error(vm, NULL);
 
     return NULL;
index 5294ba0a09102f66cbb1a92ee6e78ddb017bde32..cf8068b5a3bd452570983c5f8a56242ea7956839 100644 (file)
@@ -34,11 +34,13 @@ njs_regexp_init(njs_vm_t *vm)
     vm->regex_context = nxt_regex_context_create(njs_regexp_malloc,
                                           njs_regexp_free, vm->mem_cache_pool);
     if (nxt_slow_path(vm->regex_context == NULL)) {
+        njs_memory_error(vm);
         return NXT_ERROR;
     }
 
     vm->single_match_data = nxt_regex_match_data(NULL, vm->regex_context);
     if (nxt_slow_path(vm->single_match_data == NULL)) {
+        njs_memory_error(vm);
         return NXT_ERROR;
     }
 
@@ -66,6 +68,7 @@ njs_ret_t
 njs_regexp_constructor(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs,
     njs_index_t unused)
 {
+    u_char              *start;
     nxt_str_t           string;
     njs_regexp_flags_t  flags;
 
@@ -81,9 +84,12 @@ njs_regexp_constructor(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs,
     default:
         njs_string_get(&args[2], &string);
 
-        flags = njs_regexp_flags(&string.start, string.start + string.length,
-                                 1);
+        start = string.start;
+
+        flags = njs_regexp_flags(&start, start + string.length, 1);
         if (nxt_slow_path(flags < 0)) {
+            njs_syntax_error(vm, "Invalid RegExp flags \"%.*s\"",
+                             (int) string.length, string.start);
             return NXT_ERROR;
         }
 
@@ -267,6 +273,7 @@ njs_regexp_pattern_create(njs_vm_t *vm, u_char *start, size_t length,
                                    sizeof(njs_regexp_pattern_t)
                                    + 1 + length + size + 1);
     if (nxt_slow_path(pattern == NULL)) {
+        njs_memory_error(vm);
         return NULL;
     }
 
@@ -434,9 +441,12 @@ njs_regexp_alloc(njs_vm_t *vm, njs_regexp_pattern_t *pattern)
         regexp->object.extensible = 1;
         regexp->last_index = 0;
         regexp->pattern = pattern;
+        return regexp;
     }
 
-    return regexp;
+    njs_memory_error(vm);
+
+    return NULL;
 }
 
 
@@ -655,6 +665,7 @@ njs_regexp_prototype_exec(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs,
         match_data = nxt_regex_match_data(&pattern->regex[type],
                                           vm->regex_context);
         if (nxt_slow_path(match_data == NULL)) {
+            njs_memory_error(vm);
             return NXT_ERROR;
         }
 
@@ -744,6 +755,7 @@ njs_regexp_exec_result(njs_vm_t *vm, njs_regexp_t *regexp, njs_utf8_t utf8,
 
     ret = nxt_lvlhsh_insert(&array->object.hash, &lhq);
     if (nxt_slow_path(ret != NXT_OK)) {
+        njs_internal_error(vm, NULL);
         goto fail;
     }
 
index 0f95eb26283b07586106a03fb28050b81e790901..1e8c21d221453b91355dbc2ff59b65a7e7f26125 100644 (file)
@@ -147,6 +147,7 @@ njs_string_create(njs_vm_t *vm, njs_value_t *value, u_char *start,
 
         string = nxt_mem_cache_alloc(vm->mem_cache_pool, sizeof(njs_string_t));
         if (nxt_slow_path(string == NULL)) {
+            njs_memory_error(vm);
             return NXT_ERROR;
         }
 
@@ -174,8 +175,6 @@ njs_string_new(njs_vm_t *vm, njs_value_t *value, const u_char *start,
         return NXT_OK;
     }
 
-    njs_memory_error(vm);
-
     return NXT_ERROR;
 }
 
@@ -293,8 +292,6 @@ njs_string_hex(njs_vm_t *vm, njs_value_t *value, const nxt_str_t *src)
         return NXT_OK;
     }
 
-    njs_memory_error(vm);
-
     return NXT_ERROR;
 }
 
@@ -386,7 +383,6 @@ njs_string_base64(njs_vm_t *vm, njs_value_t *value, const nxt_str_t *src)
 
     dst.start = njs_string_alloc(vm, &vm->retval, dst.length, dst.length);
     if (nxt_slow_path(dst.start == NULL)) {
-        njs_memory_error(vm);
         return NXT_ERROR;
     }
 
@@ -418,7 +414,6 @@ njs_string_base64url(njs_vm_t *vm, njs_value_t *value, const nxt_str_t *src)
 
     dst.start = njs_string_alloc(vm, &vm->retval, dst.length, dst.length);
     if (nxt_slow_path(dst.start == NULL)) {
-        njs_memory_error(vm);
         return NXT_ERROR;
     }
 
@@ -491,6 +486,7 @@ njs_string_validate(njs_vm_t *vm, njs_string_prop_t *string, njs_value_t *value)
 
                     start = nxt_mem_cache_alloc(vm->mem_cache_pool, new_size);
                     if (nxt_slow_path(start == NULL)) {
+                        njs_memory_error(vm);
                         return NXT_ERROR;
                     }
 
index 42642dfab5aacc54bfdf20f176e12d556b04ea16..54e9ddb409549e1aeb0efa94af3c2d6ba0d8d939 100644 (file)
@@ -71,6 +71,7 @@ njs_set_timeout(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs,
 memory_error:
 
     njs_memory_error(vm);
+
     return NJS_ERROR;
 }
 
index 8c1be25e4d60ed3b4cba307fe329b65fee414262..569e8215ba84a05f09b212047355aad28bbb940c 100644 (file)
@@ -83,6 +83,7 @@ njs_builtin_add(njs_vm_t *vm, njs_parser_t *parser)
     ret = nxt_lvlhsh_insert(&scope->variables, &lhq);
 
     if (nxt_fast_path(ret == NXT_OK)) {
+        njs_internal_error(vm, NULL);
         return var;
     }
 
@@ -397,6 +398,7 @@ njs_variable_get(njs_vm_t *vm, njs_parser_node_t *node)
         value = nxt_mem_cache_align(vm->mem_cache_pool, sizeof(njs_value_t),
                                     sizeof(njs_value_t));
         if (nxt_slow_path(value == NULL)) {
+            njs_memory_error(vm);
             return NULL;
         }
 
@@ -503,6 +505,7 @@ njs_variable_alloc(njs_vm_t *vm, nxt_str_t *name, njs_variable_type_t type)
 
     var = nxt_mem_cache_zalloc(vm->mem_cache_pool, sizeof(njs_variable_t));
     if (nxt_slow_path(var == NULL)) {
+        njs_memory_error(vm);
         return NULL;
     }
 
@@ -533,6 +536,8 @@ njs_name_copy(njs_vm_t *vm, nxt_str_t *dst, nxt_str_t *src)
         return NXT_OK;
     }
 
+    njs_memory_error(vm);
+
     return NXT_ERROR;
 }
 
index f9afea4aa072d212a023fcb14485ca2153fe1bfb..72eb5ec8a620fe9461d5cc5901a1c8cd314bf24c 100644 (file)
@@ -383,6 +383,7 @@ njs_vmcode_function(njs_vm_t *vm, njs_value_t *invld1, njs_value_t *invld2)
 
     function = nxt_mem_cache_zalloc(vm->mem_cache_pool, size);
     if (nxt_slow_path(function == NULL)) {
+        njs_memory_error(vm);
         return NXT_ERROR;
     }
 
@@ -935,6 +936,7 @@ njs_method_private_copy(njs_vm_t *vm, njs_property_query_t *pq)
 
     prop = nxt_mem_cache_alloc(vm->mem_cache_pool, sizeof(njs_object_prop_t));
     if (nxt_slow_path(prop == NULL)) {
+        njs_memory_error(vm);
         return NXT_ERROR;
     }
 
@@ -968,6 +970,7 @@ njs_vmcode_property_foreach(njs_vm_t *vm, njs_value_t *object,
         next = nxt_mem_cache_alloc(vm->mem_cache_pool,
                                    sizeof(njs_property_next_t));
         if (nxt_slow_path(next == NULL)) {
+            njs_memory_error(vm);
             return NXT_ERROR;
         }
 
@@ -2721,6 +2724,7 @@ njs_vmcode_try_start(njs_vm_t *vm, njs_value_t *value, njs_value_t *offset)
     if (vm->top_frame->exception.catch != NULL) {
         e = nxt_mem_cache_alloc(vm->mem_cache_pool, sizeof(njs_exception_t));
         if (nxt_slow_path(e == NULL)) {
+            njs_memory_error(vm);
             return NXT_ERROR;
         }
 
@@ -3298,6 +3302,7 @@ again:
             if (size != NJS_STRING_LONG) {
                 start = nxt_mem_cache_alloc(vm->mem_cache_pool, size);
                 if (nxt_slow_path(start == NULL)) {
+                    njs_memory_error(vm);
                     return NXT_ERROR;
                 }
 
@@ -3332,6 +3337,7 @@ again:
 
                 p = nxt_mem_cache_alloc(vm->mem_cache_pool, len);
                 if (p == NULL) {
+                    njs_memory_error(vm);
                     return NXT_ERROR;
                 }
 
index 94107de72e50fdfda57a93a379e52f039cf45cfc..acba11960f5dc94416364ae54f7d109609acf7da 100644 (file)
@@ -5566,6 +5566,9 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("var r = new RegExp('abc', 'i'); r.test('00ABC11')"),
       nxt_string("true") },
 
+    { nxt_string("new RegExp('', 'x')"),
+      nxt_string("SyntaxError: Invalid RegExp flags \"x\"") },
+
     { nxt_string("[0].map(RegExp().toString)"),
       nxt_string("TypeError: 'this' argument is not a regexp") },
 
@@ -6081,16 +6084,19 @@ static njs_unit_test_t  njs_test[] =
       nxt_string("1,two,3") },
 
     { nxt_string("var a = Array(-1)"),
-      nxt_string("RangeError") },
+      nxt_string("RangeError: Invalid array length") },
 
     { nxt_string("var a = Array(2.5)"),
-      nxt_string("RangeError") },
+      nxt_string("RangeError: Invalid array length") },
 
     { nxt_string("var a = Array(NaN)"),
-      nxt_string("RangeError") },
+      nxt_string("RangeError: Invalid array length") },
 
     { nxt_string("var a = Array(Infinity)"),
-      nxt_string("RangeError") },
+      nxt_string("RangeError: Invalid array length") },
+
+    { nxt_string("var a = Array(1111111111)"),
+      nxt_string("MemoryError") },
 
     { nxt_string("var a = new Array(3); a"),
       nxt_string(",,") },
@@ -7533,6 +7539,9 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("var d = new Date(); d.__proto__ === Date.prototype"),
       nxt_string("true") },
 
+    { nxt_string("new Date(NaN)"),
+      nxt_string("Invalid Date") },
+
     { nxt_string("[0].map(new Date().getDate)"),
       nxt_string("TypeError: cannot convert void to date") },