Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes ecma_uint32_to_utf8_string that fill \0 at the end of string #5196

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 15 additions & 19 deletions jerry-core/api/jerry-snapshot.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,22 +191,19 @@ snapshot_add_compiled_code (const ecma_compiled_code_t *compiled_code_p, /**< co

lit_utf8_size_t pattern_size = 0;

ECMA_STRING_TO_UTF8_STRING (pattern_string_p, buffer_p, buffer_size);
ECMA_STRING_TO_UTF8_STRING (pattern_string_p, buffer);

pattern_size = buffer_size;
pattern_size = buffer.size;

if (!snapshot_write_to_buffer_by_offset (snapshot_buffer_p,
snapshot_buffer_size,
&globals_p->snapshot_buffer_write_offset,
buffer_p,
buffer_size))
buffer.ptr,
buffer.size))
{
globals_p->snapshot_error = jerry_throw_sz (JERRY_ERROR_RANGE, error_buffer_too_small_p);
/* cannot return inside ECMA_FINALIZE_UTF8_STRING */
}

ECMA_FINALIZE_UTF8_STRING (buffer_p, buffer_size);

if (!ecma_is_value_empty (globals_p->snapshot_error))
{
return 0;
Expand Down Expand Up @@ -1484,15 +1481,13 @@ jerry_append_ecma_string_to_buffer (uint8_t *buffer_p, /**< buffer */
uint8_t *buffer_end_p, /**< the end of the buffer */
ecma_string_t *string_p) /**< ecma-string */
{
ECMA_STRING_TO_UTF8_STRING (string_p, str_buffer_p, str_buffer_size);
ECMA_STRING_TO_UTF8_STRING (string_p, str_buffer);

/* Append the string to the buffer. */
uint8_t *new_buffer_p = jerry_append_chars_to_buffer (buffer_p,
buffer_end_p,
(const jerry_char_t *) str_buffer_p,
(jerry_size_t) str_buffer_size);

ECMA_FINALIZE_UTF8_STRING (str_buffer_p, str_buffer_size);
(const jerry_char_t *) str_buffer.ptr,
(jerry_size_t) str_buffer.size);

return new_buffer_p;
} /* jerry_append_ecma_string_to_buffer */
Expand All @@ -1507,11 +1502,13 @@ jerry_append_number_to_buffer (uint8_t *buffer_p, /**< buffer */
uint8_t *buffer_end_p, /**< the end of the buffer */
lit_utf8_size_t number) /**< number */
{
lit_utf8_byte_t uint32_to_str_buffer[ECMA_MAX_CHARS_IN_STRINGIFIED_UINT32];
lit_utf8_byte_t uint32_to_str_buffer[ECMA_MAX_CHARS_IN_STRINGIFIED_UINT32_WITH_ZERO_TERMINATED];
lit_utf8_size_t utf8_str_size =
ecma_uint32_to_utf8_string (number, uint32_to_str_buffer, ECMA_MAX_CHARS_IN_STRINGIFIED_UINT32);
ecma_uint32_to_utf8_string (number,
uint32_to_str_buffer,
ECMA_MAX_CHARS_IN_STRINGIFIED_UINT32_WITH_ZERO_TERMINATED);

JERRY_ASSERT (utf8_str_size <= ECMA_MAX_CHARS_IN_STRINGIFIED_UINT32);
JERRY_ASSERT (utf8_str_size < ECMA_MAX_CHARS_IN_STRINGIFIED_UINT32_WITH_ZERO_TERMINATED);

return jerry_append_chars_to_buffer (buffer_p,
buffer_end_p,
Expand Down Expand Up @@ -1623,10 +1620,10 @@ jerry_get_literals_from_snapshot (const uint32_t *snapshot_p, /**< input snapsho
for (lit_utf8_size_t i = 0; i < literal_count; i++)
{
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG (" \""));
ECMA_STRING_TO_UTF8_STRING (literal_array[i], str_buffer_p, str_buffer_size);
for (lit_utf8_size_t j = 0; j < str_buffer_size; j++)
ECMA_STRING_TO_UTF8_STRING (literal_array[i], str_buffer);
for (lit_utf8_size_t j = 0; j < str_buffer.size; j++)
{
uint8_t byte = str_buffer_p[j];
uint8_t byte = str_buffer.ptr[j];
if (byte < 32 || byte > 127)
{
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG ("\\x"));
Expand All @@ -1645,7 +1642,6 @@ jerry_get_literals_from_snapshot (const uint32_t *snapshot_p, /**< input snapsho
}
}

ECMA_FINALIZE_UTF8_STRING (str_buffer_p, str_buffer_size);
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, JERRY_ZSTR_ARG ("\""));

if (i < literal_count - 1)
Expand Down
26 changes: 10 additions & 16 deletions jerry-core/api/jerryscript.c
Original file line number Diff line number Diff line change
Expand Up @@ -384,14 +384,11 @@ jerry_parse_common (void *source_p, /**< script source */
if ((JERRY_CONTEXT (debugger_flags) & JERRY_DEBUGGER_CONNECTED) && options_p != NULL
&& (options_p->options & JERRY_PARSE_HAS_SOURCE_NAME) && ecma_is_value_string (options_p->source_name))
{
ECMA_STRING_TO_UTF8_STRING (ecma_get_string_from_value (options_p->source_name),
source_name_start_p,
source_name_size);
ECMA_STRING_TO_UTF8_STRING (ecma_get_string_from_value (options_p->source_name), source_name_start);
jerry_debugger_send_string (JERRY_DEBUGGER_SOURCE_CODE_NAME,
JERRY_DEBUGGER_NO_SUBTYPE,
source_name_start_p,
source_name_size);
ECMA_FINALIZE_UTF8_STRING (source_name_start_p, source_name_size);
source_name_start.ptr,
source_name_start.size);
}
#endif /* JERRY_DEBUGGER */

Expand Down Expand Up @@ -891,12 +888,12 @@ jerry_native_module (jerry_native_module_evaluate_cb_t callback, /**< evaluation

bool valid_identifier = false;

ECMA_STRING_TO_UTF8_STRING (name_str_p, name_start_p, name_size);
ECMA_STRING_TO_UTF8_STRING (name_str_p, name_start);

if (name_size > 0)
if (name_start.size > 0)
{
const lit_utf8_byte_t *name_p = name_start_p;
const lit_utf8_byte_t *name_end_p = name_start_p + name_size;
const lit_utf8_byte_t *name_p = name_start.ptr;
const lit_utf8_byte_t *name_end_p = name_start.ptr + name_start.size;
lit_code_point_t code_point;

lit_utf8_size_t size = lit_read_code_point_from_cesu8 (name_p, name_end_p, &code_point);
Expand All @@ -922,8 +919,6 @@ jerry_native_module (jerry_native_module_evaluate_cb_t callback, /**< evaluation
}
}

ECMA_FINALIZE_UTF8_STRING (name_start_p, name_size);

if (!valid_identifier)
{
ecma_deref_object (scope_p);
Expand Down Expand Up @@ -3001,10 +2996,10 @@ jerry_string_iterate (const jerry_value_t value,
}

ecma_string_t *str_p = ecma_get_string_from_value (value);
ECMA_STRING_TO_UTF8_STRING (str_p, buffer_p, buffer_size);
ECMA_STRING_TO_UTF8_STRING (str_p, buffer);

const lit_utf8_byte_t *current_p = buffer_p;
const lit_utf8_byte_t *end_p = buffer_p + buffer_size;
const lit_utf8_byte_t *current_p = buffer.ptr;
const lit_utf8_byte_t *end_p = buffer.ptr + buffer.size;

switch (encoding)
{
Expand Down Expand Up @@ -3048,7 +3043,6 @@ jerry_string_iterate (const jerry_value_t value,
break;
}
}
ECMA_FINALIZE_UTF8_STRING (buffer_p, buffer_size);
} /* jerry_string_iterate */

/**
Expand Down
20 changes: 7 additions & 13 deletions jerry-core/debugger/debugger.c
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ jerry_debugger_copy_variables_to_string_message (uint8_t variable_type, /**< typ
const size_t max_byte_count = JERRY_DEBUGGER_SEND_MAX (uint8_t);
const size_t max_message_size = JERRY_DEBUGGER_SEND_SIZE (max_byte_count, uint8_t);

ECMA_STRING_TO_UTF8_STRING (value_str, str_buff, str_buff_size);
ECMA_STRING_TO_UTF8_STRING (value_str, str_buff);

size_t str_size = 0;
size_t str_limit = 255;
Expand Down Expand Up @@ -373,7 +373,7 @@ jerry_debugger_copy_variables_to_string_message (uint8_t variable_type, /**< typ
}
else
{
str_size = (str_buff_size > str_limit) ? str_limit : str_buff_size;
str_size = (str_buff.size > str_limit) ? str_limit : str_buff.size;
}

message_string_p->string[*buffer_pos] = (uint8_t) str_size;
Expand All @@ -384,7 +384,7 @@ jerry_debugger_copy_variables_to_string_message (uint8_t variable_type, /**< typ
if (result)
{
size_t free_bytes = max_byte_count - *buffer_pos;
const uint8_t *string_p = str_buff;
const uint8_t *string_p = str_buff.ptr;

while (str_size > free_bytes)
{
Expand All @@ -409,8 +409,6 @@ jerry_debugger_copy_variables_to_string_message (uint8_t variable_type, /**< typ
}
}

ECMA_FINALIZE_UTF8_STRING (str_buff, str_buff_size);

return result;
} /* jerry_debugger_copy_variables_to_string_message */

Expand Down Expand Up @@ -614,9 +612,8 @@ jerry_debugger_send_eval (const lit_utf8_byte_t *eval_string_p, /**< evaluated s

ecma_string_t *string_p = ecma_get_string_from_value (message);

ECMA_STRING_TO_UTF8_STRING (string_p, buffer_p, buffer_size);
jerry_debugger_send_string (JERRY_DEBUGGER_EVAL_RESULT, type, buffer_p, buffer_size);
ECMA_FINALIZE_UTF8_STRING (buffer_p, buffer_size);
ECMA_STRING_TO_UTF8_STRING (string_p, buffer);
jerry_debugger_send_string (JERRY_DEBUGGER_EVAL_RESULT, type, buffer.ptr, buffer.size);

ecma_free_value (message);

Expand Down Expand Up @@ -1525,12 +1522,9 @@ jerry_debugger_send_exception_string (ecma_value_t exception_value)
string_p = ecma_op_to_string (exception_value);
}

ECMA_STRING_TO_UTF8_STRING (string_p, string_data_p, string_size);

bool result =
jerry_debugger_send_string (JERRY_DEBUGGER_EXCEPTION_STR, JERRY_DEBUGGER_NO_SUBTYPE, string_data_p, string_size);
ECMA_STRING_TO_UTF8_STRING (string_p, str);

ECMA_FINALIZE_UTF8_STRING (string_data_p, string_size);
bool result = jerry_debugger_send_string (JERRY_DEBUGGER_EXCEPTION_STR, JERRY_DEBUGGER_NO_SUBTYPE, str.ptr, str.size);

ecma_deref_ecma_string (string_p);
return result;
Expand Down
6 changes: 6 additions & 0 deletions jerry-core/ecma/base/ecma-globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -1379,6 +1379,12 @@ typedef float ecma_number_t;
*/
#define ECMA_MAX_CHARS_IN_STRINGIFIED_UINT32 10

/**
* Maximum number of characters in string representation of ecma-uint32 plus one.
* That is the '\0' terminator
*/
#define ECMA_MAX_CHARS_IN_STRINGIFIED_UINT32_WITH_ZERO_TERMINATED (ECMA_MAX_CHARS_IN_STRINGIFIED_UINT32 + 1)

/**
* String is not a valid array index.
*/
Expand Down
10 changes: 6 additions & 4 deletions jerry-core/ecma/base/ecma-helpers-conversion.c
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,8 @@ ecma_uint32_to_utf8_string (uint32_t value, /**< value to convert */
lit_utf8_byte_t *out_buffer_p, /**< buffer for string */
lit_utf8_size_t buffer_size) /**< size of buffer */
{
lit_utf8_byte_t *buf_p = out_buffer_p + buffer_size;
lit_utf8_byte_t *buf_p_tail = out_buffer_p + buffer_size - 1;
lit_utf8_byte_t *buf_p = buf_p_tail;

do
{
Expand All @@ -675,12 +676,13 @@ ecma_uint32_to_utf8_string (uint32_t value, /**< value to convert */

JERRY_ASSERT (buf_p >= out_buffer_p);

lit_utf8_size_t bytes_copied = (lit_utf8_size_t) (out_buffer_p + buffer_size - buf_p);
lit_utf8_size_t bytes_copied = (lit_utf8_size_t) (buf_p_tail - buf_p);

if (JERRY_LIKELY (buf_p != out_buffer_p))
{
memmove (out_buffer_p, buf_p, bytes_copied);
}
buf_p[bytes_copied] = '\0';

return bytes_copied;
} /* ecma_uint32_to_utf8_string */
Expand Down Expand Up @@ -868,7 +870,7 @@ ecma_number_to_utf8_string (ecma_number_t num, /**< ecma-number */
if (((ecma_number_t) num_uint32) == num)
{
dst_p += ecma_uint32_to_utf8_string (num_uint32, dst_p, (lit_utf8_size_t) (buffer_p + buffer_size - dst_p));
JERRY_ASSERT (dst_p <= buffer_p + buffer_size);
JERRY_ASSERT (dst_p < buffer_p + buffer_size);
return (lit_utf8_size_t) (dst_p - buffer_p);
}

Expand Down Expand Up @@ -935,7 +937,7 @@ ecma_number_to_utf8_string (ecma_number_t num, /**< ecma-number */

dst_p += ecma_uint32_to_utf8_string (t, dst_p, (lit_utf8_size_t) (buffer_p + buffer_size - dst_p));

JERRY_ASSERT (dst_p <= buffer_p + buffer_size);
JERRY_ASSERT (dst_p < buffer_p + buffer_size);

return (lit_utf8_size_t) (dst_p - buffer_p);
} /* ecma_number_to_utf8_string */
Expand Down
Loading
Loading