This fixes #42 issue on Github.
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;
array->length = length;
return array;
+
+memory_error:
+
+ njs_memory_error(vm);
+
+ return NULL;
}
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;
}
size = (uint32_t) num;
if ((double) size != num) {
- njs_range_error(vm, NULL);
+ njs_range_error(vm, "Invalid array length");
return NXT_ERROR;
}
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;
}
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;
}
ov->object.extensible = 1;
ov->object.__proto__ = &vm->prototypes[proto].object;
+ return ov;
}
- return ov;
+ njs_memory_error(vm);
+
+ return NULL;
}
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;
vm->retval.data.truth = 1;
return NJS_OK;
-
-memory_error:
-
- njs_memory_error(vm);
-
- return NJS_ERROR;
}
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;
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);
vm->retval.data.truth = 1;
return NJS_OK;
-
-memory_error:
-
- njs_memory_error(vm);
-
- return NJS_ERROR;
}
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;
}
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;
}
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);
}
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;
}
ret = nxt_lvlhsh_insert(&error->hash, &lhq);
if (nxt_slow_path(ret != NXT_OK)) {
+ njs_internal_error(vm, NULL);
return NULL;
}
}
ret = nxt_lvlhsh_insert(&error->hash, &lhq);
if (nxt_slow_path(ret != NXT_OK)) {
+ njs_internal_error(vm, NULL);
return NULL;
}
}
error = njs_error_alloc(vm, type, NULL, value);
if (nxt_slow_path(error == NULL)) {
- njs_memory_error(vm);
return NXT_ERROR;
}
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;
function = nxt_mem_cache_zalloc(vm->mem_cache_pool,
sizeof(njs_function_t));
if (nxt_slow_path(function == NULL)) {
- return NULL;
+ goto memory_error;
}
/*
child = njs_vm_external_add(vm, &ext->hash, external->properties,
external->nproperties);
if (nxt_slow_path(child == NULL)) {
- return NULL;
+ goto memory_error;
}
}
ret = nxt_lvlhsh_insert(hash, &lhq);
if (nxt_slow_path(ret != NXT_OK)) {
+ njs_internal_error(vm, NULL);
return NULL;
}
}
} while (n != 0);
return ext;
+
+memory_error:
+
+ njs_memory_error(vm);
+
+ return NULL;
}
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;
}
ret = nxt_lvlhsh_insert(&vm->externals_hash, &lhq);
if (nxt_slow_path(ret != NXT_OK)) {
+ njs_internal_error(vm, NULL);
return ret;
}
start = njs_string_alloc(vm, &arguments[2], sb.st_size, length);
if (nxt_slow_path(start == NULL)) {
- goto memory_error;
+ goto fail;
}
p = start;
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;
}
start = njs_string_alloc(vm, &vm->retval, sb.st_size, length);
if (nxt_slow_path(start == NULL)) {
- goto memory_error;
+ goto fail;
}
p = start;
return NJS_OK;
-memory_error:
+fail:
if (fd != -1) {
(void) close(fd);
}
- njs_memory_error(vm);
-
return NJS_ERROR;
}
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;
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;
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;
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");
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;
retval->data.truth = 1;
return NJS_OK;
-
-memory_error:
-
- njs_memory_error(vm);
-
- return NJS_ERROR;
}
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;
}
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;
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;
}
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;
}
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;
}
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;
}
array = njs_array_alloc(ctx->vm, 0, 0);
if (nxt_slow_path(array == NULL)) {
- njs_memory_error(ctx->vm);
return NULL;
}
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;
}
object->type = NJS_OBJECT;
object->shared = 0;
object->extensible = 1;
+ return object;
}
- return object;
+ njs_memory_error(vm);
+
+ return NULL;
}
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;
}
ov->object.__proto__ = &vm->prototypes[index].object;
ov->value = *value;
+
+ return &ov->object;
}
- return &ov->object;
+ njs_memory_error(vm);
+
+ return NULL;
}
ret = nxt_lvlhsh_insert(hash, &lhq);
if (nxt_slow_path(ret != NXT_OK)) {
+ njs_internal_error(vm, NULL);
return NXT_ERROR;
}
prop->enumerable = attributes;
prop->writable = attributes;
prop->configurable = attributes;
+ return prop;
}
- return prop;
+ njs_memory_error(vm);
+
+ return NULL;
}
ret = nxt_lvlhsh_insert(&descriptor->hash, &lhq);
if (nxt_slow_path(ret != NXT_OK)) {
+ njs_internal_error(vm, NULL);
return NXT_ERROR;
}
ret = nxt_lvlhsh_insert(&descriptor->hash, &lhq);
if (nxt_slow_path(ret != NXT_OK)) {
+ njs_internal_error(vm, NULL);
return NXT_ERROR;
}
ret = nxt_lvlhsh_insert(&descriptor->hash, &lhq);
if (nxt_slow_path(ret != NXT_OK)) {
+ njs_internal_error(vm, NULL);
return NXT_ERROR;
}
ret = nxt_lvlhsh_insert(&descriptor->hash, &lhq);
if (nxt_slow_path(ret != NXT_OK)) {
+ njs_internal_error(vm, NULL);
return NXT_ERROR;
}
return &prop->value;
}
- /* Memory allocation or NXT_DECLINED error. */
njs_internal_error(vm, NULL);
return NULL;
return &prop->value;
}
- /* Memory allocation or NXT_DECLINED error. */
njs_internal_error(vm, NULL);
return NULL;
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;
}
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;
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;
}
sizeof(njs_regexp_pattern_t)
+ 1 + length + size + 1);
if (nxt_slow_path(pattern == NULL)) {
+ njs_memory_error(vm);
return NULL;
}
regexp->object.extensible = 1;
regexp->last_index = 0;
regexp->pattern = pattern;
+ return regexp;
}
- return regexp;
+ njs_memory_error(vm);
+
+ return NULL;
}
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;
}
ret = nxt_lvlhsh_insert(&array->object.hash, &lhq);
if (nxt_slow_path(ret != NXT_OK)) {
+ njs_internal_error(vm, NULL);
goto fail;
}
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;
}
return NXT_OK;
}
- njs_memory_error(vm);
-
return NXT_ERROR;
}
return NXT_OK;
}
- njs_memory_error(vm);
-
return NXT_ERROR;
}
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;
}
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;
}
start = nxt_mem_cache_alloc(vm->mem_cache_pool, new_size);
if (nxt_slow_path(start == NULL)) {
+ njs_memory_error(vm);
return NXT_ERROR;
}
memory_error:
njs_memory_error(vm);
+
return NJS_ERROR;
}
ret = nxt_lvlhsh_insert(&scope->variables, &lhq);
if (nxt_fast_path(ret == NXT_OK)) {
+ njs_internal_error(vm, NULL);
return var;
}
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;
}
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;
}
return NXT_OK;
}
+ njs_memory_error(vm);
+
return NXT_ERROR;
}
function = nxt_mem_cache_zalloc(vm->mem_cache_pool, size);
if (nxt_slow_path(function == NULL)) {
+ njs_memory_error(vm);
return NXT_ERROR;
}
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;
}
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;
}
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;
}
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;
}
p = nxt_mem_cache_alloc(vm->mem_cache_pool, len);
if (p == NULL) {
+ njs_memory_error(vm);
return NXT_ERROR;
}
{ 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") },
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(",,") },
{ 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") },