Fix problems arising from incorrect use of various size types
E.g., * `ssize_t` was used where `lit_utf8_size_t` or `jerry_api_size_t` would have been correct, * `lit_utf8_size_t` was used where `ecma_length_t` would have been correct. Note, the patch also includes internal and public API changes: * `ecma_string_to_utf8_string` does not return negative value if output buffer is not large enough to contain the string; the buffer is expected to be large enough. (`ecma_string_get_size` can be used to retrieve the required size.) * `jerry_api_string_to_char_buffer` adapts the same logic (and `jerry_api_get_string_size` can be used to determine the required size of the buffer). Related issue: #942 JerryScript-DCO-1.0-Signed-off-by: Akos Kiss akiss@inf.u-szeged.hu
This commit is contained in:
@@ -208,8 +208,8 @@ ecma_builtin_date_parse (ecma_value_t this_arg __attr_unused___, /**< this argum
|
||||
lit_utf8_size_t date_str_size = ecma_string_get_size (date_str_p);
|
||||
MEM_DEFINE_LOCAL_ARRAY (date_start_p, date_str_size, lit_utf8_byte_t);
|
||||
|
||||
ssize_t sz = ecma_string_to_utf8_string (date_str_p, date_start_p, (ssize_t) date_str_size);
|
||||
JERRY_ASSERT (sz >= 0);
|
||||
lit_utf8_size_t sz = ecma_string_to_utf8_string (date_str_p, date_start_p, date_str_size);
|
||||
JERRY_ASSERT (sz == date_str_size);
|
||||
|
||||
lit_utf8_byte_t *date_str_curr_p = date_start_p;
|
||||
const lit_utf8_byte_t *date_str_end_p = date_start_p + date_str_size;
|
||||
|
||||
@@ -132,43 +132,37 @@ ecma_builtin_error_prototype_object_to_string (ecma_value_t this_arg) /**< this
|
||||
}
|
||||
else
|
||||
{
|
||||
const lit_utf8_size_t size = (ecma_string_get_size (name_string_p) +
|
||||
ecma_string_get_size (msg_string_p) +
|
||||
lit_get_magic_string_size (LIT_MAGIC_STRING_COLON_CHAR) +
|
||||
lit_get_magic_string_size (LIT_MAGIC_STRING_SPACE_CHAR));
|
||||
const lit_utf8_size_t name_size = ecma_string_get_size (name_string_p);
|
||||
const lit_utf8_size_t msg_size = ecma_string_get_size (msg_string_p);
|
||||
const lit_utf8_size_t colon_size = lit_get_magic_string_size (LIT_MAGIC_STRING_COLON_CHAR);
|
||||
const lit_utf8_size_t space_size = lit_get_magic_string_size (LIT_MAGIC_STRING_SPACE_CHAR);
|
||||
const lit_utf8_size_t size = name_size + msg_size + colon_size + space_size;
|
||||
|
||||
const ssize_t buffer_size = (ssize_t) size;
|
||||
ssize_t buffer_size_left = buffer_size;
|
||||
|
||||
MEM_DEFINE_LOCAL_ARRAY (ret_str_buffer, buffer_size, lit_utf8_byte_t);
|
||||
MEM_DEFINE_LOCAL_ARRAY (ret_str_buffer, size, lit_utf8_byte_t);
|
||||
lit_utf8_byte_t *ret_str_buffer_p = ret_str_buffer;
|
||||
|
||||
ssize_t bytes = ecma_string_to_utf8_string (name_string_p, ret_str_buffer_p, buffer_size_left);
|
||||
JERRY_ASSERT (bytes >= 0 && buffer_size_left - bytes >= 0);
|
||||
|
||||
buffer_size_left -= bytes;
|
||||
ret_str_buffer_p = ret_str_buffer + buffer_size - buffer_size_left;
|
||||
lit_utf8_size_t bytes = ecma_string_to_utf8_string (name_string_p, ret_str_buffer_p, name_size);
|
||||
JERRY_ASSERT (bytes == name_size);
|
||||
ret_str_buffer_p = ret_str_buffer_p + bytes;
|
||||
JERRY_ASSERT (ret_str_buffer_p <= ret_str_buffer + size);
|
||||
|
||||
ret_str_buffer_p = lit_copy_magic_string_to_buffer (LIT_MAGIC_STRING_COLON_CHAR,
|
||||
ret_str_buffer_p,
|
||||
buffer_size_left);
|
||||
buffer_size_left = buffer_size - (ret_str_buffer_p - ret_str_buffer);
|
||||
JERRY_ASSERT (buffer_size_left >= 0);
|
||||
colon_size);
|
||||
JERRY_ASSERT (ret_str_buffer_p <= ret_str_buffer + size);
|
||||
|
||||
ret_str_buffer_p = lit_copy_magic_string_to_buffer (LIT_MAGIC_STRING_SPACE_CHAR,
|
||||
ret_str_buffer_p,
|
||||
buffer_size_left);
|
||||
buffer_size_left = buffer_size - (ret_str_buffer_p - ret_str_buffer);
|
||||
JERRY_ASSERT (buffer_size_left >= 0);
|
||||
space_size);
|
||||
JERRY_ASSERT (ret_str_buffer_p <= ret_str_buffer + size);
|
||||
|
||||
bytes = ecma_string_to_utf8_string (msg_string_p, ret_str_buffer_p, buffer_size_left);
|
||||
JERRY_ASSERT (bytes >= 0 && buffer_size_left - bytes >= 0);
|
||||
|
||||
buffer_size_left -= bytes;
|
||||
JERRY_ASSERT (buffer_size_left >= 0);
|
||||
bytes = ecma_string_to_utf8_string (msg_string_p, ret_str_buffer_p, msg_size);
|
||||
JERRY_ASSERT (bytes == msg_size);
|
||||
ret_str_buffer_p = ret_str_buffer_p + bytes;
|
||||
JERRY_ASSERT (ret_str_buffer_p == ret_str_buffer + size);
|
||||
|
||||
ret_str_p = ecma_new_ecma_string_from_utf8 (ret_str_buffer,
|
||||
(jerry_api_size_t) (buffer_size - buffer_size_left));
|
||||
size);
|
||||
|
||||
MEM_FINALIZE_LOCAL_ARRAY (ret_str_buffer);
|
||||
}
|
||||
|
||||
@@ -88,8 +88,8 @@ ecma_builtin_global_object_print (ecma_value_t this_arg __attr_unused___, /**< t
|
||||
utf8_str_size,
|
||||
lit_utf8_byte_t);
|
||||
|
||||
ssize_t actual_sz = ecma_string_to_utf8_string (str_p, utf8_str_p, (ssize_t) utf8_str_size);
|
||||
JERRY_ASSERT (actual_sz == (ssize_t) utf8_str_size);
|
||||
lit_utf8_size_t actual_sz = ecma_string_to_utf8_string (str_p, utf8_str_p, utf8_str_size);
|
||||
JERRY_ASSERT (actual_sz == utf8_str_size);
|
||||
|
||||
lit_utf8_byte_t *utf8_str_curr_p = utf8_str_p;
|
||||
const lit_utf8_byte_t *utf8_str_end_p = utf8_str_p + utf8_str_size;
|
||||
@@ -212,10 +212,10 @@ ecma_builtin_global_object_parse_int (ecma_value_t this_arg __attr_unused___, /*
|
||||
{
|
||||
MEM_DEFINE_LOCAL_ARRAY (string_buff, str_size, lit_utf8_byte_t);
|
||||
|
||||
ssize_t bytes_copied = ecma_string_to_utf8_string (number_str_p,
|
||||
string_buff,
|
||||
(ssize_t) str_size);
|
||||
JERRY_ASSERT (bytes_copied >= 0);
|
||||
lit_utf8_size_t bytes_copied = ecma_string_to_utf8_string (number_str_p,
|
||||
string_buff,
|
||||
str_size);
|
||||
JERRY_ASSERT (bytes_copied == str_size);
|
||||
lit_utf8_byte_t *string_curr_p = string_buff;
|
||||
lit_utf8_byte_t *string_end_p = string_buff + str_size;
|
||||
|
||||
@@ -437,10 +437,10 @@ ecma_builtin_global_object_parse_float (ecma_value_t this_arg __attr_unused___,
|
||||
{
|
||||
MEM_DEFINE_LOCAL_ARRAY (string_buff, str_size, lit_utf8_byte_t);
|
||||
|
||||
ssize_t bytes_copied = ecma_string_to_utf8_string (number_str_p,
|
||||
string_buff,
|
||||
(ssize_t) str_size);
|
||||
JERRY_ASSERT (bytes_copied >= 0);
|
||||
lit_utf8_size_t bytes_copied = ecma_string_to_utf8_string (number_str_p,
|
||||
string_buff,
|
||||
str_size);
|
||||
JERRY_ASSERT (bytes_copied == str_size);
|
||||
|
||||
lit_utf8_byte_t *str_curr_p = string_buff;
|
||||
lit_utf8_byte_t *str_end_p = string_buff + str_size;
|
||||
@@ -771,10 +771,10 @@ ecma_builtin_global_object_decode_uri_helper (ecma_value_t uri __attr_unused___,
|
||||
input_size + 1,
|
||||
lit_utf8_byte_t);
|
||||
|
||||
ssize_t sz = ecma_string_to_utf8_string (input_string_p,
|
||||
input_start_p,
|
||||
(ssize_t) (input_size));
|
||||
JERRY_ASSERT (sz >= 0);
|
||||
lit_utf8_size_t sz = ecma_string_to_utf8_string (input_string_p,
|
||||
input_start_p,
|
||||
input_size);
|
||||
JERRY_ASSERT (sz == input_size);
|
||||
|
||||
input_start_p[input_size] = LIT_BYTE_NULL;
|
||||
|
||||
@@ -1049,10 +1049,10 @@ ecma_builtin_global_object_encode_uri_helper (ecma_value_t uri, /**< uri argumen
|
||||
input_size,
|
||||
lit_utf8_byte_t);
|
||||
|
||||
ssize_t sz = ecma_string_to_utf8_string (input_string_p,
|
||||
input_start_p,
|
||||
(ssize_t) (input_size));
|
||||
JERRY_ASSERT (sz >= 0);
|
||||
lit_utf8_size_t sz = ecma_string_to_utf8_string (input_string_p,
|
||||
input_start_p,
|
||||
input_size);
|
||||
JERRY_ASSERT (sz == input_size);
|
||||
|
||||
/*
|
||||
* The URI encoding has two major phases: first we validate the input,
|
||||
@@ -1270,10 +1270,10 @@ ecma_builtin_global_object_escape (ecma_value_t this_arg __attr_unused___, /**<
|
||||
input_size,
|
||||
lit_utf8_byte_t);
|
||||
|
||||
ssize_t sz = ecma_string_to_utf8_string (input_string_p,
|
||||
input_start_p,
|
||||
(ssize_t) (input_size));
|
||||
JERRY_ASSERT (sz >= 0);
|
||||
lit_utf8_size_t sz = ecma_string_to_utf8_string (input_string_p,
|
||||
input_start_p,
|
||||
input_size);
|
||||
JERRY_ASSERT (sz == input_size);
|
||||
|
||||
/*
|
||||
* The escape routine has two major phases: first we compute
|
||||
@@ -1390,8 +1390,8 @@ ecma_builtin_global_object_unescape (ecma_value_t this_arg __attr_unused___, /**
|
||||
|
||||
/* 3. */
|
||||
MEM_DEFINE_LOCAL_ARRAY (input_start_p, input_size, lit_utf8_byte_t);
|
||||
ssize_t sz = ecma_string_to_utf8_string (input_string_p, input_start_p, (ssize_t) (input_size));
|
||||
JERRY_ASSERT (sz >= 0);
|
||||
lit_utf8_size_t sz = ecma_string_to_utf8_string (input_string_p, input_start_p, input_size);
|
||||
JERRY_ASSERT (sz == input_size);
|
||||
|
||||
lit_utf8_byte_t *input_curr_p = input_start_p;
|
||||
lit_utf8_byte_t *input_end_p = input_start_p + input_size;
|
||||
|
||||
@@ -84,11 +84,10 @@ ecma_builtin_helper_object_to_string (const ecma_value_t this_arg) /**< this arg
|
||||
/* Building string "[object #type#]" where type is 'Undefined',
|
||||
'Null' or one of possible object's classes.
|
||||
The string with null character is maximum 19 characters long. */
|
||||
const ssize_t buffer_size = 19;
|
||||
const lit_utf8_size_t buffer_size = 19;
|
||||
MEM_DEFINE_LOCAL_ARRAY (str_buffer, buffer_size, lit_utf8_byte_t);
|
||||
|
||||
lit_utf8_byte_t *buffer_ptr = str_buffer;
|
||||
ssize_t buffer_size_left = buffer_size;
|
||||
|
||||
const lit_magic_string_id_t magic_string_ids[] =
|
||||
{
|
||||
@@ -101,13 +100,12 @@ ecma_builtin_helper_object_to_string (const ecma_value_t this_arg) /**< this arg
|
||||
|
||||
for (uint32_t i = 0; i < sizeof (magic_string_ids) / sizeof (lit_magic_string_id_t); ++i)
|
||||
{
|
||||
buffer_ptr = lit_copy_magic_string_to_buffer (magic_string_ids[i], buffer_ptr, buffer_size_left);
|
||||
buffer_size_left = buffer_size - (buffer_ptr - str_buffer);
|
||||
buffer_ptr = lit_copy_magic_string_to_buffer (magic_string_ids[i], buffer_ptr,
|
||||
(lit_utf8_size_t) ((str_buffer + buffer_size) - buffer_ptr));
|
||||
JERRY_ASSERT (buffer_ptr <= str_buffer + buffer_size);
|
||||
}
|
||||
|
||||
JERRY_ASSERT (buffer_size_left >= 0);
|
||||
|
||||
ret_string_p = ecma_new_ecma_string_from_utf8 (str_buffer, (lit_utf8_size_t) (buffer_size - buffer_size_left));
|
||||
ret_string_p = ecma_new_ecma_string_from_utf8 (str_buffer, (lit_utf8_size_t) (buffer_ptr - str_buffer));
|
||||
|
||||
MEM_FINALIZE_LOCAL_ARRAY (str_buffer);
|
||||
|
||||
@@ -588,10 +586,10 @@ ecma_builtin_helper_string_find_index (ecma_string_t *original_str_p, /**< index
|
||||
original_size,
|
||||
lit_utf8_byte_t);
|
||||
|
||||
ssize_t sz = ecma_string_to_utf8_string (original_str_p,
|
||||
original_str_utf8_p,
|
||||
(ssize_t) (original_size));
|
||||
JERRY_ASSERT (sz >= 0);
|
||||
lit_utf8_size_t sz = ecma_string_to_utf8_string (original_str_p,
|
||||
original_str_utf8_p,
|
||||
original_size);
|
||||
JERRY_ASSERT (sz == original_size);
|
||||
|
||||
ecma_length_t index = start_pos;
|
||||
|
||||
@@ -606,10 +604,10 @@ ecma_builtin_helper_string_find_index (ecma_string_t *original_str_p, /**< index
|
||||
search_size,
|
||||
lit_utf8_byte_t);
|
||||
|
||||
ssize_t sz = ecma_string_to_utf8_string (search_str_p,
|
||||
search_str_utf8_p,
|
||||
(ssize_t) (search_size));
|
||||
JERRY_ASSERT (sz >= 0);
|
||||
lit_utf8_size_t sz = ecma_string_to_utf8_string (search_str_p,
|
||||
search_str_utf8_p,
|
||||
search_size);
|
||||
JERRY_ASSERT (sz == search_size);
|
||||
|
||||
lit_utf8_byte_t *search_str_curr_p = search_str_utf8_p;
|
||||
|
||||
|
||||
@@ -710,12 +710,12 @@ ecma_builtin_json_parse (ecma_value_t this_arg __attr_unused___, /**< 'this' arg
|
||||
|
||||
ecma_string_t *string_p = ecma_get_string_from_value (string);
|
||||
ecma_length_t string_size = (uint32_t) ecma_string_get_size (string_p);
|
||||
size_t buffer_size = sizeof (lit_utf8_byte_t) * (string_size + 1);
|
||||
lit_utf8_size_t buffer_size = sizeof (lit_utf8_byte_t) * (string_size + 1);
|
||||
|
||||
MEM_DEFINE_LOCAL_ARRAY (str_start_p, buffer_size, lit_utf8_byte_t);
|
||||
|
||||
ssize_t sz = ecma_string_to_utf8_string (string_p, str_start_p, (ssize_t) buffer_size);
|
||||
JERRY_ASSERT (sz == (ssize_t) string_size);
|
||||
lit_utf8_size_t sz = ecma_string_to_utf8_string (string_p, str_start_p, buffer_size);
|
||||
JERRY_ASSERT (sz == string_size);
|
||||
|
||||
str_start_p[string_size] = LIT_BYTE_NULL;
|
||||
|
||||
@@ -1068,11 +1068,11 @@ ecma_builtin_json_quote (ecma_string_t *string_p) /**< string that should be quo
|
||||
|
||||
MEM_DEFINE_LOCAL_ARRAY (string_buff, string_size, lit_utf8_byte_t);
|
||||
|
||||
ssize_t bytes_copied = ecma_string_to_utf8_string (string_p,
|
||||
string_buff,
|
||||
(ssize_t) string_size);
|
||||
lit_utf8_size_t bytes_copied = ecma_string_to_utf8_string (string_p,
|
||||
string_buff,
|
||||
string_size);
|
||||
|
||||
JERRY_ASSERT (bytes_copied > 0 || !string_size);
|
||||
JERRY_ASSERT (bytes_copied == string_size);
|
||||
|
||||
lit_utf8_byte_t *str_p = string_buff;
|
||||
const lit_utf8_byte_t *str_end_p = str_p + string_size;
|
||||
|
||||
@@ -1204,10 +1204,10 @@ ecma_builtin_string_prototype_object_replace_main (ecma_builtin_replace_search_c
|
||||
replace_size,
|
||||
lit_utf8_byte_t);
|
||||
|
||||
ssize_t sz = ecma_string_to_utf8_string (replace_string_p,
|
||||
replace_start_p,
|
||||
(ssize_t) (replace_size));
|
||||
JERRY_ASSERT (sz >= 0);
|
||||
lit_utf8_size_t sz = ecma_string_to_utf8_string (replace_string_p,
|
||||
replace_start_p,
|
||||
replace_size);
|
||||
JERRY_ASSERT (sz == replace_size);
|
||||
|
||||
context_p->replace_string_p = replace_string_p;
|
||||
context_p->replace_str_curr_p = replace_start_p;
|
||||
@@ -2079,10 +2079,10 @@ ecma_builtin_string_prototype_object_conversion_helper (ecma_value_t this_arg, /
|
||||
input_size,
|
||||
lit_utf8_byte_t);
|
||||
|
||||
ssize_t sz = ecma_string_to_utf8_string (input_string_p,
|
||||
input_start_p,
|
||||
(ssize_t) (input_size));
|
||||
JERRY_ASSERT (sz >= 0);
|
||||
lit_utf8_size_t sz = ecma_string_to_utf8_string (input_string_p,
|
||||
input_start_p,
|
||||
input_size);
|
||||
JERRY_ASSERT (sz == input_size);
|
||||
|
||||
/*
|
||||
* The URI encoding has two major phases: first we compute
|
||||
@@ -2097,8 +2097,8 @@ ecma_builtin_string_prototype_object_conversion_helper (ecma_value_t this_arg, /
|
||||
{
|
||||
ecma_char_t character = lit_utf8_read_next (&input_str_curr_p);
|
||||
ecma_char_t character_buffer[LIT_MAXIMUM_OTHER_CASE_LENGTH];
|
||||
ecma_length_t character_length;
|
||||
lit_utf8_byte_t utf8_byte_buffer[LIT_CESU8_MAX_BYTES_IN_CODE_POINT];
|
||||
lit_utf8_size_t character_length;
|
||||
|
||||
if (lower_case)
|
||||
{
|
||||
@@ -2115,7 +2115,7 @@ ecma_builtin_string_prototype_object_conversion_helper (ecma_value_t this_arg, /
|
||||
|
||||
JERRY_ASSERT (character_length >= 1 && character_length <= LIT_MAXIMUM_OTHER_CASE_LENGTH);
|
||||
|
||||
for (lit_utf8_size_t i = 0; i < character_length; i++)
|
||||
for (ecma_length_t i = 0; i < character_length; i++)
|
||||
{
|
||||
output_length += lit_code_unit_to_utf8 (character_buffer[i], utf8_byte_buffer);
|
||||
}
|
||||
@@ -2136,7 +2136,7 @@ ecma_builtin_string_prototype_object_conversion_helper (ecma_value_t this_arg, /
|
||||
{
|
||||
ecma_char_t character = lit_utf8_read_next (&input_str_curr_p);
|
||||
ecma_char_t character_buffer[LIT_MAXIMUM_OTHER_CASE_LENGTH];
|
||||
lit_utf8_size_t character_length;
|
||||
ecma_length_t character_length;
|
||||
|
||||
if (lower_case)
|
||||
{
|
||||
@@ -2153,7 +2153,7 @@ ecma_builtin_string_prototype_object_conversion_helper (ecma_value_t this_arg, /
|
||||
|
||||
JERRY_ASSERT (character_length >= 1 && character_length <= LIT_MAXIMUM_OTHER_CASE_LENGTH);
|
||||
|
||||
for (lit_utf8_size_t i = 0; i < character_length; i++)
|
||||
for (ecma_length_t i = 0; i < character_length; i++)
|
||||
{
|
||||
output_char_p += lit_code_unit_to_utf8 (character_buffer[i], output_char_p);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user