Skip to content

Commit

Permalink
Fixes ecma_uint32_to_utf8_string that fill \0 at the end of string
Browse files Browse the repository at this point in the history
Closes: jerryscript-project#5194

JerryScript-DCO-1.0-Signed-off-by: Yonggang Luo [email protected]
  • Loading branch information
lygstate committed Dec 11, 2024
1 parent b7ce246 commit 26f67ec
Show file tree
Hide file tree
Showing 26 changed files with 326 additions and 367 deletions.
34 changes: 18 additions & 16 deletions jerry-core/api/jerry-snapshot.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,21 +187,21 @@ 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, jerry_string_sz (error_buffer_too_small_p));
/* cannot return inside ECMA_FINALIZE_UTF8_STRING */
}

ECMA_FINALIZE_UTF8_STRING (buffer_p, buffer_size);
ECMA_FINALIZE_UTF8_STRING (buffer);

if (!ecma_is_value_empty (globals_p->snapshot_error))
{
Expand Down Expand Up @@ -1481,15 +1481,15 @@ 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);
(const jerry_char_t *) str_buffer.ptr,
(jerry_size_t) str_buffer.size);

ECMA_FINALIZE_UTF8_STRING (str_buffer_p, str_buffer_size);
ECMA_FINALIZE_UTF8_STRING (str_buffer);

return new_buffer_p;
} /* jerry_append_ecma_string_to_buffer */
Expand All @@ -1504,11 +1504,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 @@ -1620,10 +1622,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 @@ -1642,7 +1644,7 @@ jerry_get_literals_from_snapshot (const uint32_t *snapshot_p, /**< input snapsho
}
}

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

if (i < literal_count - 1)
Expand Down
46 changes: 16 additions & 30 deletions jerry-core/api/jerryscript.c
Original file line number Diff line number Diff line change
Expand Up @@ -384,14 +384,12 @@ 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);
ECMA_FINALIZE_UTF8_STRING (source_name_start);
}
#endif /* JERRY_DEBUGGER */

Expand Down Expand Up @@ -892,12 +890,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 @@ -923,7 +921,7 @@ jerry_native_module (jerry_native_module_evaluate_cb_t callback, /**< evaluation
}
}

ECMA_FINALIZE_UTF8_STRING (name_start_p, name_size);
ECMA_FINALIZE_UTF8_STRING (name_start);

if (!valid_identifier)
{
Expand Down Expand Up @@ -3035,10 +3033,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 @@ -3082,7 +3080,7 @@ jerry_string_iterate (const jerry_value_t value,
break;
}
}
ECMA_FINALIZE_UTF8_STRING (buffer_p, buffer_size);
ECMA_FINALIZE_UTF8_STRING (buffer);
} /* jerry_string_iterate */

/**
Expand Down Expand Up @@ -5316,22 +5314,10 @@ jerry_log (jerry_log_level_t level, const char *format_p, ...)
case 'S':
{
jerry_value_t str = va_arg (vl, jerry_value_t);

ecma_string_t *string2_p = ecma_op_to_string (str);
lit_utf8_size_t cesu8_string2_size;
lit_utf8_size_t cesu8_string2_length;
lit_utf8_byte_t uint32_to_string_buffer[ECMA_MAX_CHARS_IN_STRINGIFIED_UINT32];
uint8_t flags = ECMA_STRING_FLAG_EMPTY;

const lit_utf8_byte_t *cesu8_string2_p = ecma_string_get_chars (string2_p,
&cesu8_string2_size,
&cesu8_string2_length,
uint32_to_string_buffer,
&flags);
jerry_log_string (cesu8_string2_p, cesu8_string2_size);
if (flags & ECMA_STRING_FLAG_MUST_BE_FREED)
{
jmem_heap_free_block ((void *) cesu8_string2_p, cesu8_string2_size);
}
ECMA_STRING_TO_UTF8_STRING_FLAG_EMPTY (string2_p, cesu8_string2);
jerry_log_string (cesu8_string2.ptr, cesu8_string2.size);

ecma_deref_ecma_string (string2_p);
break;
Expand Down
24 changes: 13 additions & 11 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,7 +409,7 @@ jerry_debugger_copy_variables_to_string_message (uint8_t variable_type, /**< typ
}
}

ECMA_FINALIZE_UTF8_STRING (str_buff, str_buff_size);
ECMA_FINALIZE_UTF8_STRING (str_buff);

return result;
} /* jerry_debugger_copy_variables_to_string_message */
Expand Down Expand Up @@ -614,9 +614,9 @@ 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_FINALIZE_UTF8_STRING (buffer);

ecma_free_value (message);

Expand Down Expand Up @@ -1527,12 +1527,14 @@ 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);
ECMA_STRING_TO_UTF8_STRING (string_p, string_data);

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

ECMA_FINALIZE_UTF8_STRING (string_data_p, string_size);
ECMA_FINALIZE_UTF8_STRING (string_data);

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 @@ -1501,6 +1501,12 @@ typedef double 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 @@ -618,7 +618,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 @@ -631,12 +632,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 @@ -824,7 +826,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 @@ -891,7 +893,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

0 comments on commit 26f67ec

Please sign in to comment.