Skip to content

Commit

Permalink
Avoiding exlitit length calculation for strings.
Browse files Browse the repository at this point in the history
  • Loading branch information
xeioex committed Jun 27, 2024
1 parent 9d4bf6c commit 89e5afb
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 196 deletions.
8 changes: 1 addition & 7 deletions src/njs_error.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,11 @@ void
njs_error_new(njs_vm_t *vm, njs_value_t *dst, njs_object_t *proto,
u_char *start, size_t size)
{
ssize_t length;
njs_int_t ret;
njs_value_t string;
njs_object_t *error;

length = njs_utf8_length(start, size);
if (njs_slow_path(length < 0)) {
length = 0;
}

ret = njs_string_new(vm, &string, start, size, length);
ret = njs_string_create(vm, &string, start, size);
if (njs_slow_path(ret != NJS_OK)) {
return;
}
Expand Down
7 changes: 1 addition & 6 deletions src/njs_extern.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ njs_external_add(njs_vm_t *vm, njs_arr_t *protos,
const njs_external_t *external, njs_uint_t n)
{
size_t size;
ssize_t length;
njs_int_t ret;
njs_lvlhsh_t *hash;
const u_char *start;
Expand Down Expand Up @@ -119,12 +118,8 @@ njs_external_add(njs_vm_t *vm, njs_arr_t *protos,
} else {
start = (u_char *) external->u.property.value;
size = njs_strlen(start);
length = njs_utf8_length(start, size);
if (njs_slow_path(length < 0)) {
length = 0;
}

ret = njs_string_new(vm, &prop->u.value, start, size, length);
ret = njs_string_create(vm, &prop->u.value, start, size);
if (njs_slow_path(ret != NJS_OK)) {
return NJS_ERROR;
}
Expand Down
51 changes: 10 additions & 41 deletions src/njs_generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -1062,7 +1062,6 @@ static njs_int_t
njs_generate_var_statement_after(njs_vm_t *vm, njs_generator_t *generator,
njs_parser_node_t *node)
{
ssize_t length;
njs_int_t ret;
njs_variable_t *var;
njs_parser_node_t *lvalue, *expr;
Expand Down Expand Up @@ -1104,14 +1103,8 @@ njs_generate_var_statement_after(njs_vm_t *vm, njs_generator_t *generator,
return NJS_ERROR;
}

length = njs_utf8_length(lex_entry->name.start, lex_entry->name.length);
if (njs_slow_path(length < 0)) {
return NJS_ERROR;
}

ret = njs_string_new(vm, &expr->u.value.data.u.lambda->name,
lex_entry->name.start, lex_entry->name.length,
length);
ret = njs_string_create(vm, &expr->u.value.data.u.lambda->name,
lex_entry->name.start, lex_entry->name.length);
if (njs_slow_path(ret != NJS_OK)) {
return NJS_ERROR;
}
Expand Down Expand Up @@ -3030,7 +3023,6 @@ static njs_int_t
njs_generate_global_property_set(njs_vm_t *vm, njs_generator_t *generator,
njs_parser_node_t *node_dst, njs_parser_node_t *node_src)
{
ssize_t length;
njs_int_t ret;
njs_value_t property;
njs_variable_t *var;
Expand All @@ -3050,13 +3042,8 @@ njs_generate_global_property_set(njs_vm_t *vm, njs_generator_t *generator,
return NJS_ERROR;
}

length = njs_utf8_length(lex_entry->name.start, lex_entry->name.length);
if (njs_slow_path(length < 0)) {
return NJS_ERROR;
}

ret = njs_string_new(vm, &property, lex_entry->name.start,
lex_entry->name.length, length);
ret = njs_string_create(vm, &property, lex_entry->name.start,
lex_entry->name.length);
if (njs_slow_path(ret != NJS_OK)) {
return NJS_ERROR;
}
Expand Down Expand Up @@ -3641,7 +3628,6 @@ static njs_int_t
njs_generate_function_expression(njs_vm_t *vm, njs_generator_t *generator,
njs_parser_node_t *node)
{
ssize_t length;
njs_int_t ret;
njs_variable_t *var;
njs_function_lambda_t *lambda;
Expand Down Expand Up @@ -3671,13 +3657,8 @@ njs_generate_function_expression(njs_vm_t *vm, njs_generator_t *generator,
return ret;
}

length = njs_utf8_length(lex_entry->name.start, lex_entry->name.length);
if (njs_slow_path(length < 0)) {
return NJS_ERROR;
}

ret = njs_string_new(vm, &lambda->name, lex_entry->name.start,
lex_entry->name.length, length);
ret = njs_string_create(vm, &lambda->name, lex_entry->name.start,
lex_entry->name.length);
if (njs_slow_path(ret != NJS_OK)) {
return NJS_ERROR;
}
Expand Down Expand Up @@ -4207,7 +4188,6 @@ static njs_int_t
njs_generate_function_declaration(njs_vm_t *vm, njs_generator_t *generator,
njs_parser_node_t *node)
{
ssize_t length;
njs_int_t ret;
njs_bool_t async;
njs_variable_t *var;
Expand All @@ -4232,13 +4212,8 @@ njs_generate_function_declaration(njs_vm_t *vm, njs_generator_t *generator,
return NJS_ERROR;
}

length = njs_utf8_length(lex_entry->name.start, lex_entry->name.length);
if (njs_slow_path(length < 0)) {
return NJS_ERROR;
}

ret = njs_string_new(vm, &lambda->name, lex_entry->name.start,
lex_entry->name.length, length);
ret = njs_string_create(vm, &lambda->name, lex_entry->name.start,
lex_entry->name.length);
if (njs_slow_path(ret != NJS_OK)) {
return NJS_ERROR;
}
Expand Down Expand Up @@ -5413,7 +5388,6 @@ static njs_int_t
njs_generate_global_reference(njs_vm_t *vm, njs_generator_t *generator,
njs_parser_node_t *node, njs_bool_t exception)
{
ssize_t length;
njs_int_t ret;
njs_index_t index;
njs_value_t property;
Expand Down Expand Up @@ -5441,13 +5415,8 @@ njs_generate_global_reference(njs_vm_t *vm, njs_generator_t *generator,
return NJS_ERROR;
}

length = njs_utf8_length(lex_entry->name.start, lex_entry->name.length);
if (njs_slow_path(length < 0)) {
return NJS_ERROR;
}

ret = njs_string_new(vm, &property, lex_entry->name.start,
lex_entry->name.length, length);
ret = njs_string_create(vm, &property, lex_entry->name.start,
lex_entry->name.length);
if (njs_slow_path(ret != NJS_OK)) {
return NJS_ERROR;
}
Expand Down
2 changes: 1 addition & 1 deletion src/njs_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -1798,7 +1798,7 @@ njs_dump_terminal(njs_json_stringify_t *stringify, njs_chb_t *chain,

njs_chb_append_literal(chain, "[");

(void) njs_typed_array_to_chain(stringify->vm, chain, array, NULL);
njs_typed_array_to_chain(stringify->vm, chain, array, NULL);

njs_chb_append_literal(chain, "]");

Expand Down
24 changes: 2 additions & 22 deletions src/njs_regexp.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ static njs_int_t njs_regexp_exec(njs_vm_t *vm, njs_value_t *r, njs_value_t *s,
unsigned flags, njs_value_t *retval);
static njs_array_t *njs_regexp_exec_result(njs_vm_t *vm, njs_value_t *r,
njs_utf8_t utf8, njs_string_prop_t *string, njs_regex_match_data_t *data);
static njs_int_t njs_regexp_string_create(njs_vm_t *vm, njs_value_t *value,
u_char *start, uint32_t size, int32_t length);


const njs_value_t njs_string_lindex = njs_string("lastIndex");
Expand Down Expand Up @@ -1016,7 +1014,7 @@ njs_regexp_exec_result(njs_vm_t *vm, njs_value_t *r, njs_utf8_t utf8,
{
u_char *start;
size_t c;
int32_t size, length;
int32_t size;
uint32_t index;
njs_int_t ret;
njs_uint_t i, n;
Expand Down Expand Up @@ -1050,15 +1048,7 @@ njs_regexp_exec_result(njs_vm_t *vm, njs_value_t *r, njs_utf8_t utf8,
start = &string->start[c];
size = njs_regex_capture(match_data, n + 1) - c;

if (utf8 == NJS_STRING_UTF8) {
length = njs_max(njs_utf8_length(start, size), 0);

} else {
length = size;
}

ret = njs_regexp_string_create(vm, &array->start[i], start, size,
length);
ret = njs_string_create(vm, &array->start[i], start, size);
if (njs_slow_path(ret != NJS_OK)) {
goto fail;
}
Expand Down Expand Up @@ -1316,16 +1306,6 @@ njs_regexp_exec(njs_vm_t *vm, njs_value_t *r, njs_value_t *s, unsigned flags,
}


static njs_int_t
njs_regexp_string_create(njs_vm_t *vm, njs_value_t *value, u_char *start,
uint32_t size, int32_t length)
{
length = (length >= 0) ? length : 0;

return njs_string_new(vm, value, start, size, length);
}


njs_int_t
njs_regexp_prototype_symbol_replace(njs_vm_t *vm, njs_value_t *args,
njs_uint_t nargs, njs_index_t unused, njs_value_t *retval)
Expand Down
78 changes: 0 additions & 78 deletions src/njs_string.c
Original file line number Diff line number Diff line change
Expand Up @@ -535,84 +535,6 @@ njs_string_copy(njs_value_t *dst, njs_value_t *src)
}


/*
* njs_string_validate() validates an UTF-8 string, evaluates its length,
* sets njs_string_prop_t struct.
*/

njs_int_t
njs_string_validate(njs_vm_t *vm, njs_string_prop_t *string, njs_value_t *value)
{
u_char *start;
size_t new_size, map_offset;
ssize_t size, length;
uint32_t *map;

size = value->short_string.size;

if (size != NJS_STRING_LONG) {
string->start = value->short_string.start;
length = value->short_string.length;

if (length == 0 && length != size) {
length = njs_utf8_length(value->short_string.start, size);

if (njs_slow_path(length < 0)) {
/* Invalid UTF-8 string. */
return length;
}

value->short_string.length = length;
}

} else {
string->start = value->long_string.data->start;
size = value->long_string.size;
length = value->long_string.data->length;

if (length == 0 && length != size) {
length = njs_utf8_length(string->start, size);

if (length != size) {
if (njs_slow_path(length < 0)) {
/* Invalid UTF-8 string. */
return length;
}

if (length > NJS_STRING_MAP_STRIDE) {
/*
* Reallocate the long string with offset map
* after the string.
*/
map_offset = njs_string_map_offset(size);
new_size = map_offset + njs_string_map_size(length);

start = njs_mp_alloc(vm->mem_pool, new_size);
if (njs_slow_path(start == NULL)) {
njs_memory_error(vm);
return NJS_ERROR;
}

memcpy(start, string->start, size);
string->start = start;
value->long_string.data->start = start;

map = (uint32_t *) (start + map_offset);
map[0] = 0;
}
}

value->long_string.data->length = length;
}
}

string->size = size;
string->length = length;

return length;
}


static njs_int_t
njs_string_constructor(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
njs_index_t unused, njs_value_t *retval)
Expand Down
4 changes: 1 addition & 3 deletions src/njs_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,6 @@ void njs_string_truncate(njs_value_t *value, uint32_t size, uint32_t length);
uint32_t njs_string_trim(const njs_value_t *value, njs_string_prop_t *string,
unsigned mode);
void njs_string_copy(njs_value_t *dst, njs_value_t *src);
njs_int_t njs_string_validate(njs_vm_t *vm, njs_string_prop_t *string,
njs_value_t *value);
njs_int_t njs_string_cmp(const njs_value_t *val1, const njs_value_t *val2);
void njs_string_slice_string_prop(njs_string_prop_t *dst,
const njs_string_prop_t *string, const njs_slice_prop_t *slice);
Expand Down Expand Up @@ -197,7 +195,7 @@ njs_string_calc_length(njs_utf8_t utf8, const u_char *start, size_t size)
default:
length = njs_utf8_length(start, size);

return (length >= 0) ? length : 0;
return length;
}
}

Expand Down
Loading

0 comments on commit 89e5afb

Please sign in to comment.